Home > Enterprise >  Return a List of Results from an API Call
Return a List of Results from an API Call

Time:04-12

I am creating an app that calls a recipe API with user-selected parameters and then returns a List of results. However, I am having trouble returning more than one result. Here is the code I have:

//this is the recipe model
public class RecipeModel
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Image { get; set; }
}


//this is the Result model 
//I had to make a model of a model because the result is a nested JSON response
public class RecipeResultModel
{
    public List<RecipeModel> Results { get; set; }
}

//this is the method of making the API call
public static async Task<List<RecipeModel>> LoadRecipes(string queryString)
{
    var url = $"{ queryString }";

    using (var response = await ApiHelper.ApiClient.GetAsync(url))
    {
        if (response.IsSuccessStatusCode)
        {
            RecipeResultModel result = await response.Content.ReadAsAsync<RecipeResultModel>();

            return result.Results;
        }
        else
        {
            throw new Exception(response.ReasonPhrase);               
        }
    }
}

//here is method that displays the Recipe results
private async Task ShowResults()
{
    var recipeInfo = await RecipeProcessor.LoadRecipes(RecipeUrl);

    foreach (var recipe in recipeInfo)
    {
        var uriSource = new Uri(recipe.Image, UriKind.Absolute);

        still need wired up to form
            recipeImage.Source = new BitmapImage(uriSource);

        recipeResult.Text = recipe.Title;
    }
}

I can successfully get a single result back from ShowResults(), however I am needing a List.

CodePudding user response:

The return type of Task on `ShowResults()' means that the function only returns the state of the task -- if it finished or not.

If you want to get a list back from an async Function, you need to add the type you want back after the Task in the function definition, like you did on the static LoadRecipes method.

private async Task<List<TheTypeIWant>> ShowResults()
{
    var recipeInfo = await RecipeProcessor.LoadRecipes(RecipeUrl);

    /*create a list of recipes*/

    foreach (var recipe in recipeInfo)
    {
        var uriSource = new Uri(recipe.Image, UriKind.Absolute);
        recipeResult.Text = recipe.Title;

        /*add the recipe to the list*/     
    }

    return /*return the list of recipes*/;
}

You need to replace the stuff in comments with the appropriate types for whatever you're trying to do. It's not clear from the question.

You could just return RecipeResults from the static LoadResults() function and the ShowResults() Function

CodePudding user response:

When you have a response from an Aysnc call you need to specify the .Result at the end.

See this .Result appended to the call line:

RecipeResultModel result = await response.Content.ReadAsAsync<RecipeResultModel>().Result;
return result.Results;
  • Related