Api dosent get called

Eduardo Gomez 3,426 Reputation points
2022-07-14T18:29:04.357+00:00

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>  
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,781 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,546 Reputation points Microsoft Vendor
    2022-07-18T03:14:48.063+00:00

    Api is called . There is no control for data filling in your code. I added the control DataGrid, which can display some data in json.
    Xaml:

     <Grid.RowDefinitions>  
                    <RowDefinition Height="Auto" />  
                    <RowDefinition Height="Auto" />  
                    <RowDefinition Height="Auto" />  
                    <RowDefinition Height="Auto" />  
                </Grid.RowDefinitions>  
      
      <DataGrid x:Name="dg" ItemsSource="{Binding AirportsList}"  Height="300" Grid.Column="1" Grid.Row="3"/>  
    

    The result:
    221619-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.