Skip to content

Commit b92deec

Browse files
authored
feat: Add /dumps endpoint (#34)
1 parent 324a7be commit b92deec

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

IGDB.Tests/Dumps.cs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using IGDB.Models;
5+
using RestEase;
6+
using Xunit;
7+
8+
namespace IGDB.Tests
9+
{
10+
public class Dumps
11+
{
12+
IGDBClient _api;
13+
14+
public Dumps()
15+
{
16+
_api = new IGDB.IGDBClient(
17+
Environment.GetEnvironmentVariable("IGDB_CLIENT_ID"),
18+
Environment.GetEnvironmentVariable("IGDB_CLIENT_SECRET")
19+
);
20+
}
21+
22+
[Fact]
23+
public async Task ShouldReturnDumpsList()
24+
{
25+
var dumps = await _api.GetDataDumpsAsync();
26+
27+
Assert.NotNull(dumps);
28+
Assert.True(dumps.Length > 10);
29+
}
30+
31+
[Fact]
32+
public async Task ShouldReturnGamesEndpointDump()
33+
{
34+
var gameDump = await _api.GetDataDumpEndpointAsync("games");
35+
36+
Assert.NotNull(gameDump);
37+
Assert.NotNull(gameDump.S3Url);
38+
}
39+
}
40+
}

IGDB/IGDBApi.cs

+55
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Threading.Tasks;
45
using IGDB.Models;
56
using Newtonsoft.Json;
@@ -37,6 +38,18 @@ public interface IGDBApi
3738
/// <returns>Array of IGDB models of the specified type</returns>
3839
[Post("/{endpoint}/count")]
3940
Task<CountResponse> CountAsync([Path] string endpoint, [Body] string query = null);
41+
42+
/// <summary>
43+
/// Retrieves list of available data dumps (IGDB Partners only)
44+
/// </summary>
45+
[Get("/dumps")]
46+
Task<DataDump[]> GetDataDumpsAsync();
47+
48+
/// <summary>
49+
/// Retrieves the download URL of a data dump (IGDB Partners only). Use the S3Url to download the dump (link expires after 5 minutes).
50+
/// </summary>
51+
[Get("/dumps/{endpoint}")]
52+
Task<DataDumpEndpoint> GetDataDumpForEndpointAsync([Path] string endpoint);
4053
}
4154

4255
public sealed class IGDBClient
@@ -150,6 +163,48 @@ public async Task<CountResponse> CountAsync(string endpoint, string query = null
150163
}
151164
}
152165

166+
public async Task<DataDump[]> GetDataDumpsAsync()
167+
{
168+
try
169+
{
170+
return await _api.GetDataDumpsAsync();
171+
}
172+
catch (ApiException apiEx)
173+
{
174+
// Acquire new token and retry request (once)
175+
if (IsInvalidTokenResponse(apiEx))
176+
{
177+
await _tokenManager.RefreshTokenAsync();
178+
179+
return await _api.GetDataDumpsAsync();
180+
}
181+
182+
// Pass up any other exceptions
183+
throw apiEx;
184+
}
185+
}
186+
187+
public async Task<DataDumpEndpoint> GetDataDumpEndpointAsync(string endpoint)
188+
{
189+
try
190+
{
191+
return await _api.GetDataDumpForEndpointAsync(endpoint);
192+
}
193+
catch (ApiException apiEx)
194+
{
195+
// Acquire new token and retry request (once)
196+
if (IsInvalidTokenResponse(apiEx))
197+
{
198+
await _tokenManager.RefreshTokenAsync();
199+
200+
return await _api.GetDataDumpForEndpointAsync(endpoint);
201+
}
202+
203+
// Pass up any other exceptions
204+
throw apiEx;
205+
}
206+
}
207+
153208
/// <summary>
154209
/// Whether or not an ApiException represents an invalid_token response.
155210
/// </summary>

IGDB/Models/DataDump.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace IGDB.Models
4+
{
5+
public class DataDump
6+
{
7+
public string Endpoint { get; set; }
8+
public string FileName { get; set; }
9+
public DateTimeOffset UpdatedAt { get; set; }
10+
}
11+
}

IGDB/Models/DataDumpEndpoint.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace IGDB.Models
5+
{
6+
public class DataDumpEndpoint
7+
{
8+
public string S3Url { get; set; }
9+
public string Endpoint { get; set; }
10+
public string FileName { get; set; }
11+
public long SizeBytes { get; set; }
12+
public DateTimeOffset UpdatedAt { get; set; }
13+
public string SchemaVersion { get; set; }
14+
15+
/// <summary>
16+
/// Dictionary representing the schema of the CSV file. Tied to SchemaVersion.
17+
/// </summary>
18+
/// <remarks>
19+
/// Example:
20+
/// - id: "LONG"
21+
/// - name: "STRING"
22+
/// - created_at: "TIMESTAMP"
23+
/// - franchises: "LONG[]"
24+
/// - total_rating: "DOUBLE"
25+
/// - total_rating_count: "INTEGER"
26+
/// </remarks>
27+
public Dictionary<string, string> Schema { get; set; }
28+
}
29+
}

0 commit comments

Comments
 (0)