I am trying to complement the data that I got from an API with another.
For example, if we take a look at this JSON, we do not see any currency data, or languages are spoken or currency, etc. So in my class, I implemented the extra fields.
I created a class service that is responsible to get all my data, but for some reason, I don't see my data being populated anywhere.
public class AirportServices
{
private const string AIRPORTS_API = "https://gist.githubusercontent.com/tdreyno/4278655/raw/7b0762c09b519f40397e4c3e100b097d861f5588/airports.json";
private readonly HttpClient Client;
public AirportServices()
{
Client = new HttpClient();
}
List<Airport>? airports = new();
public async Task<List<Airport>?> GetAirportsAsync()
{
var AirportRresponse = await Client.GetAsync(AIRPORTS_API);
if (AirportRresponse.IsSuccessStatusCode)
{
airports = await AirportRresponse.Content.ReadFromJsonAsync<List<Airport>>();
foreach (var item in airports!)
{
item.ISO2 = await GetCountryCodeAsync($"https://countrycode.dev/api/countries/{item.country?.Replace(" ","%20")}");
}
} // I put a breakpoint here, but it doesn't hit
return null;
}
public async Task<string?> GetCountryCodeAsync(string url)
{
var response = await Client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
var array = JsonConvert.DeserializeObject<JArray>(json);
foreach (JObject item in array!)
{
return item["ISO2"]?.ToString();
}
}
return null;
}
}
I am using a ViewModel, that is tied to my UI and I am using syncfution AutoComplete control to receive the data. The strange part is that if I do not call the second API the for each loop, to populate the country code, everything works
private async void GetAirportList()
{
var temppList = await Services.GetAirportsAsync();
var airports = new ObservableCollection<Airport>();// this doesn't get hit
foreach (var item in temppList!)
{
airports.Add(item);
}
AirportsList = airports;
}
Thanks in advance for the help.
Obviously, the GetCountryCode Api gives us more data, but this is just a test to try and get the country code.
What I want to achieve is to fill this data
//New properties
public string? ISO2
{
get; set;
}
public string? continent
{
get; set;
}
public string? currency
{
get; set;
}
public string? languages
{
get; set;
}
//End of a new properties
this is my app https://github.com/eduardoagr/Travel
<syncfusion:SfTextBoxExt
Grid.Column="1"
Grid.Row="1"
VerticalAlignment="Center"
ShowDropDownButton="True"
SearchItemPath="city"
AutoCompleteMode="Suggest"
SelectedItem="{Binding SelectedItem}"
HighlightedTextColor="Red"
TextHighlightMode="FirstOccurrence"
AutoCompleteSource="{Binding AirportsList}"
>
<syncfusion:SfTextBoxExt.AutoCompleteItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="12" Source="{Binding flag}" />
<TextBlock Text="{Binding city}" />
</StackPanel>
</DataTemplate>
</syncfusion:SfTextBoxExt.AutoCompleteItemTemplate>
</syncfusion:SfTextBoxExt>