IGDB base client to query the API & return the response (async func)
continuous-integration/drone/push Build is failing Details

API-Integration
Rémi LAVERGNE 10 months ago
parent 0356f5c9dd
commit c7a6ff5684

@ -0,0 +1,47 @@
using Microsoft.Extensions.Options;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Runtime;
using System.Text.Json;
using System.Threading.Tasks;
namespace Models.API
{
public class IGDBClient
{
private readonly HttpClient _httpClient;
private readonly AuthService _authService;
private readonly IGDBSettings _settings;
public IGDBClient(HttpClient httpClient, AuthService authService, IGDBSettings settings)
{
_httpClient = httpClient;
_authService = authService;
_settings = settings;
}
public async Task<string> GetGamesAsync()
{
var authResponse = await _authService.GetAccessTokenAsync(_settings.ClientId, _settings.ClientSecret);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResponse.AccessToken);
_httpClient.DefaultRequestHeaders.Add("Client-ID", _settings.ClientId);
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.igdb.com/v4/games");
request.Content = new StringContent("fields name,genres.name; limit 10;", System.Text.Encoding.UTF8, "application/json");
var response = await _httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Request error: {response.StatusCode}, Content: {errorContent}");
throw new HttpRequestException($"Response status code: {response.StatusCode} ({errorContent})");
}
var responseContent = await response.Content.ReadAsStringAsync();
return responseContent;
}
}
}
Loading…
Cancel
Save