Added untested endpoints for Games part of CFCore web API.

This commit is contained in:
Harrison Deng 2022-05-19 22:10:59 -05:00
parent 85d2c2cbda
commit 4fcbc53daa
6 changed files with 327 additions and 0 deletions

View File

@ -0,0 +1,13 @@
using System.Diagnostics;
using System.Net.Http;
namespace CFCoreAPI.Endpoints
{
public abstract class APIBase
{
protected HttpClient httpClient;
public APIBase(HttpClient httpClient)
{
this.httpClient = httpClient;
}
}
}

View File

@ -0,0 +1,73 @@
using System.Net.Http;
using CFCoreAPI.Endpoints;
using Microsoft.AspNetCore.WebUtilities;
using CFCoreAPI.Endpoints.Games.Responses;
using System.Text.Json;
namespace CFCoreAPI.Endpoints.Games
{
public class GamesAPI : APIBase
{
public GamesAPI(HttpClient httpClient) : base(httpClient)
{
}
// TODO: Likely should move generic calls to extension method or private helper method.
public async Task<GamesResponse> GetGamesAsync(int? index = null, int? pageSize = null)
{
string url = "/v1/games";
if (index != null) url = QueryHelpers.AddQueryString(url, "index", index.ToString());
if (pageSize != null) url = QueryHelpers.AddQueryString(url, "pageSize", pageSize.ToString());
HttpResponseMessage response = await httpClient.GetAsync(url);
GamesResponse result;
using (Stream contentStream = await response.Content.ReadAsStreamAsync())
{
result = await JsonSerializer.DeserializeAsync<GamesResponse>(contentStream);
}
return result;
}
public async Task<GameResponse> GetGameAsync(string gameId)
{
string url = "/v1/games/{0}";
string.Format(url, gameId);
HttpResponseMessage response = await httpClient.GetAsync(url);
GameResponse result;
using (Stream contentStream = await response.Content.ReadAsStreamAsync())
{
result = await JsonSerializer.DeserializeAsync<GameResponse>(contentStream);
}
return result;
}
public async Task<VersionsResponse> GetVersionsAsync(string gameId)
{
string url = "/v1/games/{0}/versions";
string.Format(url, gameId);
HttpResponseMessage response = await httpClient.GetAsync(url);
VersionsResponse result;
using (Stream contentStream = await response.Content.ReadAsStreamAsync())
{
result = (await JsonSerializer.DeserializeAsync<VersionsResponse>(contentStream))!;
}
return result;
}
public async Task<VersionTypesResponse> GetVersionTypesAsync(string gameId)
{
string url = "/v1/games/{0}/version-types";
string.Format(url, gameId);
HttpResponseMessage response = await httpClient.GetAsync(url);
VersionTypesResponse result;
using (Stream contentStream = await response.Content.ReadAsStreamAsync())
{
result = (await JsonSerializer.DeserializeAsync<VersionTypesResponse>(contentStream))!;
}
return result;
}
}
}

View File

@ -0,0 +1,69 @@
using System.Text.Json.Serialization;
namespace CFCoreAPI.Endpoints.Games.Responses
{
public struct GameResponse
{
[JsonConstructor]
public GameResponse(
DataModel data
)
{
this.Data = data;
}
public DataModel Data { get; }
public struct AssetsModel
{
[JsonConstructor]
public AssetsModel(
string iconUrl,
string tileUrl,
string coverUrl
)
{
this.IconUrl = iconUrl;
this.TileUrl = tileUrl;
this.CoverUrl = coverUrl;
}
public string IconUrl { get; }
public string TileUrl { get; }
public string CoverUrl { get; }
}
public struct DataModel
{
[JsonConstructor]
public DataModel(
int id,
string name,
string slug,
DateTime dateModified,
AssetsModel assets,
int status,
int apiStatus
)
{
this.Id = id;
this.Name = name;
this.Slug = slug;
this.DateModified = dateModified;
this.Assets = assets;
this.Status = status;
this.ApiStatus = apiStatus;
}
public int Id { get; }
public string Name { get; }
public string Slug { get; }
public DateTime DateModified { get; }
public AssetsModel Assets { get; }
public int Status { get; }
public int ApiStatus { get; }
}
}
}

View File

@ -0,0 +1,95 @@
using System.Text.Json.Serialization;
namespace CFCoreAPI.Endpoints.Games.Responses
{
public struct GamesResponse
{
[JsonConstructor]
public GamesResponse(
List<DataModel> data,
PaginationModel pagination
)
{
this.Data = data;
this.Pagination = pagination;
}
public IReadOnlyList<DataModel> Data { get; }
public PaginationModel Pagination { get; }
public struct AssetsModel
{
[JsonConstructor]
public AssetsModel(
string iconUrl,
string tileUrl,
string coverUrl
)
{
this.IconUrl = iconUrl;
this.TileUrl = tileUrl;
this.CoverUrl = coverUrl;
}
public string IconUrl { get; }
public string TileUrl { get; }
public string CoverUrl { get; }
}
public struct DataModel
{
[JsonConstructor]
public DataModel(
int id,
string name,
string slug,
DateTime dateModified,
AssetsModel assets,
int status,
int apiStatus
)
{
this.Id = id;
this.Name = name;
this.Slug = slug;
this.DateModified = dateModified;
this.Assets = assets;
this.Status = status;
this.ApiStatus = apiStatus;
}
public int Id { get; }
public string Name { get; }
public string Slug { get; }
public DateTime DateModified { get; }
public AssetsModel Assets { get; }
public int Status { get; }
public int ApiStatus { get; }
}
public struct PaginationModel
{
[JsonConstructor]
public PaginationModel(
int index,
int pageSize,
int resultCount,
int totalCount
)
{
this.Index = index;
this.PageSize = pageSize;
this.ResultCount = resultCount;
this.TotalCount = totalCount;
}
public int Index { get; }
public int PageSize { get; }
public int ResultCount { get; }
public int TotalCount { get; }
}
}
}

View File

@ -0,0 +1,42 @@
using System.Text.Json.Serialization;
namespace CFCoreAPI.Endpoints.Games.Responses
{
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class VersionTypesResponse
{
[JsonConstructor]
public VersionTypesResponse(
List<DataModel> data
)
{
this.Data = data;
}
public IReadOnlyList<DataModel> Data { get; }
public class DataModel
{
[JsonConstructor]
public DataModel(
int id,
int gameId,
string name,
string slug
)
{
this.Id = id;
this.GameId = gameId;
this.Name = name;
this.Slug = slug;
}
public int Id { get; }
public int GameId { get; }
public string Name { get; }
public string Slug { get; }
}
}
}

View File

@ -0,0 +1,35 @@
using System.Text.Json.Serialization;
namespace CFCoreAPI.Endpoints.Games.Responses
{
public class VersionsResponse
{
[JsonConstructor]
public VersionsResponse(
List<DataModel> data
)
{
this.Data = data;
}
public IReadOnlyList<DataModel> Data { get; }
public class DataModel
{
[JsonConstructor]
public DataModel(
int type,
List<string> versions
)
{
this.Type = type;
this.Versions = versions;
}
public int Type { get; }
public IReadOnlyList<string> Versions { get; }
}
}
}