Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add /dumps endpoint #34

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
}
}
Loading