I need to refactor this method to make it work better.
This method works flawlessly when calling an external API Rest
return list. However, it gives an exception when the external API return object Rest returns an element only.
public List<T> ChamarAPIListagem<T>(string chamada)
{
HttpResponseMessage response = client.GetAsync(chamada).Result;
if (response.IsSuccessStatusCode)
{
var dados = response.Content.ReadAsAsync<IEnumerable<T>>().Result;
return dados.ToList();
}
return null;
}
The problem is when the API returns an object only. It gives exception error:
JsonSerializationException: Can not deserialize the current JSON object (e.g. {"name": "value")) into type 'System.Collections.Generic.IEnumerable'1 [AppSpot.Ano]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly.
Although not very relevant, it follows an example of the return in% with% of% of external%;
An example of an API return would be this:
[
{"codigo": "320-1", "name": "Zer", "key": "320"},
{"codigo": "201-1", "name": "201", "key": "201"},
{"codigo": "201-1", "name": "201", "key": "201"},
{"codigo": "201-1", "name": "201", "key": "201"},
]