Hello im working on a task at the moment, and so far ive been able to retrieve the cities from the following API:
https://docs.openaq.org/v2/cities
What i would like to do is send parameters to the API - https://docs.openaq.org and display the air quality results for a given city. I am quite new to retrieving data from an external API so i am a bit unsure on how i would be able to do this.
Could someone please give me some advice?
Heres the function i currently have for retrieving all City data from https://docs.openaq.org/v2/cities
public async Task<IEnumerable<City>> GetAllCityData()
{
IEnumerable<City> city = null;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://docs.openaq.org/");
var responseTask = client.GetAsync("v2/cities");
//responseTask.Wait();
await responseTask;
var result = responseTask.Result;
if (result.IsSuccessStatusCode)
{
var readData = result.Content.ReadAsStringAsync();
await readData;
//using System.Text.Json;
var jsonresult = JsonConvert.DeserializeObject<RootModel>(readData.Result);
city = jsonresult.results;
}
else
{
city = Enumerable.Empty<City>();
ModelStateDictionary modelState = new ModelStateDictionary();
modelState.AddModelError(string.Empty, "Server error has occured");
}
}
return city;
}
Thankyou!