This is something that could be really noob, but I really would like to understand. I´m doing a console application that makes a request to Swapi API (the star wars api) and, catch all the starships available on the API. When I hit their API (https://swapi.co/api/starships), the response return a field called "next", that has the other part of the starships that they have (the API is making the pagination on each request)
Well, I´ve made a class that do the first request to their API, deserialize the object, and inside of a while, I verify if the field "next" is null and do the rest of the requests with the field "next"
This is my code
public class RequestToSwapiApi : IRequestToSwapiApi
{
private string Json;
private string ApiURL
{
get
{
return "https://swapi.co/api/starships/";
}
}
public async Task<RootObject> ReturnTheStarShipModels()
{
var request = new HttpClient();
this.Json = await request.GetStringAsync(this.ApiURL);
var rootObject = JsonConvert.DeserializeObject<RootObject>(this.Json);
while (rootObject.Next != null)
{
this.Json = await request.GetStringAsync(rootObject.Next);
var newDeserializeObject = JsonConvert.DeserializeObject<RootObject>(this.Json);
if (newDeserializeObject.Results != null)
{
rootObject.Results.AddRange(newDeserializeObject.Results);
}
rootObject.Next = newDeserializeObject.Next;
}
return rootObject;
}
}
I know that if I use a block of code "using HttpClient", I will avoid the necessity of using the disposable. But, If I use that block code "using HttpClient" inside of a loop the way I´m doing above, the object that I´m creating will be reusable? Or it will be created, and disposable foreach time that the loop occours? The way I´m doing it could cause performance issues?