Skip to content

Commit

Permalink
feat: Add /dumps endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kamranayub committed May 24, 2024
1 parent 324a7be commit fed5018
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
40 changes: 40 additions & 0 deletions IGDB.Tests/Dumps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using IGDB.Models;
using RestEase;
using Xunit;

namespace IGDB.Tests
{
public class Dumps
{
IGDBClient _api;

public Dumps()
{
_api = new IGDB.IGDBClient(
Environment.GetEnvironmentVariable("IGDB_CLIENT_ID"),
Environment.GetEnvironmentVariable("IGDB_CLIENT_SECRET")
);
}

[Fact]
public async Task ShouldReturnDumpsList()
{
var dumps = await _api.GetDataDumpsAsync();

Assert.NotNull(dumps);
Assert.True(dumps.Length > 10);
}

[Fact]
public async Task ShouldReturnGamesEndpointDump()
{
var gameDump = await _api.GetDataDumpEndpointAsync("games");

Assert.NotNull(gameDump);
Assert.NotNull(gameDump.S3Url);
}
}
}
55 changes: 55 additions & 0 deletions IGDB/IGDBApi.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using IGDB.Models;
using Newtonsoft.Json;
Expand Down Expand Up @@ -37,6 +38,18 @@ public interface IGDBApi
/// <returns>Array of IGDB models of the specified type</returns>
[Post("/{endpoint}/count")]
Task<CountResponse> CountAsync([Path] string endpoint, [Body] string query = null);

/// <summary>
/// Retrieves list of available data dumps (IGDB Partners only)
/// </summary>
[Get("/dumps")]
Task<DataDump[]> GetDataDumpsAsync();

/// <summary>
/// Retrieves the download URL of a data dump (IGDB Partners only). Use the S3Url to download the dump (link expires after 5 minutes).
/// </summary>
[Get("/dumps/{endpoint}")]
Task<DataDumpEndpoint> GetDataDumpForEndpointAsync([Path] string endpoint);
}

public sealed class IGDBClient
Expand Down Expand Up @@ -150,6 +163,48 @@ public async Task<CountResponse> CountAsync(string endpoint, string query = null
}
}

public async Task<DataDump[]> GetDataDumpsAsync()
{
try
{
return await _api.GetDataDumpsAsync();
}
catch (ApiException apiEx)
{
// Acquire new token and retry request (once)
if (IsInvalidTokenResponse(apiEx))
{
await _tokenManager.RefreshTokenAsync();

return await _api.GetDataDumpsAsync();
}

// Pass up any other exceptions
throw apiEx;
}
}

public async Task<DataDumpEndpoint> GetDataDumpEndpointAsync(string endpoint)
{
try
{
return await _api.GetDataDumpForEndpointAsync(endpoint);
}
catch (ApiException apiEx)
{
// Acquire new token and retry request (once)
if (IsInvalidTokenResponse(apiEx))
{
await _tokenManager.RefreshTokenAsync();

return await _api.GetDataDumpForEndpointAsync(endpoint);
}

// Pass up any other exceptions
throw apiEx;
}
}

/// <summary>
/// Whether or not an ApiException represents an invalid_token response.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions IGDB/Models/DataDump.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace IGDB.Models
{
public class DataDump
{
public string Endpoint { get; set; }
public string FileName { get; set; }
public DateTimeOffset UpdatedAt { get; set; }
}
}
29 changes: 29 additions & 0 deletions IGDB/Models/DataDumpEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;

namespace IGDB.Models
{
public class DataDumpEndpoint
{
public string S3Url { get; set; }
public string Endpoint { get; set; }
public string FileName { get; set; }
public long SizeBytes { get; set; }
public DateTimeOffset UpdatedAt { get; set; }
public string SchemaVersion { get; set; }

/// <summary>
/// Dictionary representing the schema of the CSV file. Tied to SchemaVersion.
/// </summary>
/// <remarks>
/// Example:
/// - id: "LONG"
/// - name: "STRING"
/// - created_at: "TIMESTAMP"
/// - franchises: "LONG[]"
/// - total_rating: "DOUBLE"
/// - total_rating_count: "INTEGER"
/// </remarks>
public Dictionary<string, string> Schema { get; set; }
}
}

0 comments on commit fed5018

Please sign in to comment.