Skip to content

Commit

Permalink
chore: Added start of IGDBClient to grab game data
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimple committed Feb 2, 2024
1 parent 37d05df commit f377159
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions CFDiscordBot/IGDBClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace CFDiscordBot
{
public class IGDBClient
{
HttpClient _hc;
string _clientId;
string _clientSecret;

AccessToken token;

public IGDBClient(string client_id, string client_secret, HttpClient client)

Check warning on line 14 in CFDiscordBot/IGDBClient.cs

View workflow job for this annotation

GitHub Actions / generate

Non-nullable field 'token' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_clientId = client_id;
_clientSecret = client_secret;

_hc = client;
_hc.BaseAddress = new Uri("https://api.igdb.com/");
}

public async Task<JsonElement> SearchGamesAsync(string query)
{
await GetTwitchToken();

var response = await _hc.PostAsync("/v4/games/", new StringContent(query));

return JsonSerializer.Deserialize<JsonElement>(await response.Content.ReadAsStringAsync());
}

private async Task GetTwitchToken()
{
if (token == null || (token.Expires - DateTimeOffset.Now).TotalMinutes < 5)
{
var response = await _hc.PostAsync($"https://id.twitch.tv/oauth2/token?client_id={_clientId}&client_secret={_clientSecret}&grant_type=client_credentials", null);
var str = await response.Content.ReadAsStringAsync();
token = JsonSerializer.Deserialize<AccessToken>(str);

Check warning on line 38 in CFDiscordBot/IGDBClient.cs

View workflow job for this annotation

GitHub Actions / generate

Possible null reference assignment.
}

_hc.DefaultRequestHeaders.Remove("Authorization");
_hc.DefaultRequestHeaders.Remove("Client-ID");

_hc.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", $"Bearer {token.Token}");

Check warning on line 44 in CFDiscordBot/IGDBClient.cs

View workflow job for this annotation

GitHub Actions / generate

Dereference of a possibly null reference.
_hc.DefaultRequestHeaders.TryAddWithoutValidation("Client-ID", _clientId);
}

private class AccessToken
{
[JsonPropertyName("access_token")]
public string Token { get; set; }

Check warning on line 51 in CFDiscordBot/IGDBClient.cs

View workflow job for this annotation

GitHub Actions / generate

Non-nullable property 'Token' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
[JsonPropertyName("token_type")]
public string Type { get; set; }

Check warning on line 53 in CFDiscordBot/IGDBClient.cs

View workflow job for this annotation

GitHub Actions / generate

Non-nullable property 'Type' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
[JsonPropertyName("expires_in")]
public long ExpiresIn { get; set; }
private DateTimeOffset? _expires { get; set; }
public DateTimeOffset Expires
{
get
{
if (_expires == null)
{
_expires = DateTimeOffset.Now.AddSeconds(ExpiresIn);
}

return _expires.Value;
}
}
}
}
}

0 comments on commit f377159

Please sign in to comment.