Cześć, nigdy nie dotykałem nic wcześniej z API i chciałbym się zwalidować czy przypadkiem nie obieram ziemniaków
szczególnie z tymi generykami
public static class API_Urls
{
/// https?
public const string
searchPlayer_by_Name = "https://randomAPI.com/name/",
searchGames_by_Id = "https://randomAPI.com/id/";
}
public class APIController : Controller
{
public const string api_key = "xxx";
public async Task<T> sendRequestAsync<T>(string url, object param)
{
url += $"{param}?api_key={api_key}";
var client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url);
HttpContent responsecontent = response.Content;
string request_result = await responsecontent.ReadAsStringAsync();
var output = JsonDeserializer<T>.FromJson(request_result);
return output;
}
public IActionResult LoadGames(string name)
{
long id = sendRequestAsync <Player> (API_Urls.searchPlayer_by_Name, name) .Result.PlayerId;
Games games = sendRequestAsync <Games> (API_Urls.searchGames_by_Id, id) .Result;
ViewBag.Id = id;
ViewBag.Games = games.Matches;
return View();
}
}
I dlaczego takie pobieranie jsonów byłoby złe?
string htmlCode;
using (WebClient client = new WebClient())
{
htmlCode = client.DownloadString("http://somesite.com/default.json");
}
... string to json, etc...
Żeby uniknąć przejechania się na limicie requestów, to po prostu mam sobie zliczać ich ilość w ostatniej np. minucie czy to jest złe/słabe i powinienem używać czegoś typu WebApiThrottle?