I have the following code that makes a call to an external API, processes the response and returns a string. The problem is it is taking a very long time to process. I can't do anything to the API, but I was just wondering if there is a better way of doing the rest of the code.
foreach(var item in products)
{
item.site = await _testRepo.GetSiteLink(item.Id, item.baseLink);
}
And the GetSiteLink method in a different class.
public async Task<string> GetSiteLink(int itemId, string baseLink)
{
var encodedUrl = HttpUtility.UrlEncode(baseLink);
var response = await _client.GetAsync($"apilink/{itemId}/?ulp={encodedUrl}");
if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
var parsedResponse = JsonConvert.DeserializeObject<string[]>(responseContent);
return HttpUtility.UrlDecode(parsedResponse[0]);
}
return "";
}
IDisposable
:using var response = await ...
. It would allow to reuse the opened server connections faster. \$\endgroup\$