diff --git a/CFCoreAPI/Endpoints/APIBase.cs b/CFCoreAPI/Endpoints/APIBase.cs new file mode 100644 index 0000000..11412fc --- /dev/null +++ b/CFCoreAPI/Endpoints/APIBase.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/CFCoreAPI/Endpoints/Games/GamesAPI.cs b/CFCoreAPI/Endpoints/Games/GamesAPI.cs new file mode 100644 index 0000000..1e6d010 --- /dev/null +++ b/CFCoreAPI/Endpoints/Games/GamesAPI.cs @@ -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 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(contentStream); + } + return result; + } + + public async Task 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(contentStream); + } + return result; + } + + public async Task 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(contentStream))!; + } + return result; + } + + public async Task 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(contentStream))!; + } + return result; + } + } +} \ No newline at end of file diff --git a/CFCoreAPI/Endpoints/Games/Responses/GameResponse.cs b/CFCoreAPI/Endpoints/Games/Responses/GameResponse.cs new file mode 100644 index 0000000..3010446 --- /dev/null +++ b/CFCoreAPI/Endpoints/Games/Responses/GameResponse.cs @@ -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; } + } + } + + +} \ No newline at end of file diff --git a/CFCoreAPI/Endpoints/Games/Responses/GamesResponse.cs b/CFCoreAPI/Endpoints/Games/Responses/GamesResponse.cs new file mode 100644 index 0000000..3ceb4ae --- /dev/null +++ b/CFCoreAPI/Endpoints/Games/Responses/GamesResponse.cs @@ -0,0 +1,95 @@ +using System.Text.Json.Serialization; + +namespace CFCoreAPI.Endpoints.Games.Responses +{ + + public struct GamesResponse + { + [JsonConstructor] + public GamesResponse( + List data, + PaginationModel pagination + ) + { + this.Data = data; + this.Pagination = pagination; + } + + public IReadOnlyList 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; } + } + } + + +} \ No newline at end of file diff --git a/CFCoreAPI/Endpoints/Games/Responses/VersionTypesResponse.cs b/CFCoreAPI/Endpoints/Games/Responses/VersionTypesResponse.cs new file mode 100644 index 0000000..6056a08 --- /dev/null +++ b/CFCoreAPI/Endpoints/Games/Responses/VersionTypesResponse.cs @@ -0,0 +1,42 @@ +using System.Text.Json.Serialization; + +namespace CFCoreAPI.Endpoints.Games.Responses +{ + // Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse); + + public class VersionTypesResponse + { + [JsonConstructor] + public VersionTypesResponse( + List data + ) + { + this.Data = data; + } + + public IReadOnlyList 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; } + } + } + + +} \ No newline at end of file diff --git a/CFCoreAPI/Endpoints/Games/Responses/VersionsResponse.cs b/CFCoreAPI/Endpoints/Games/Responses/VersionsResponse.cs new file mode 100644 index 0000000..b2967fc --- /dev/null +++ b/CFCoreAPI/Endpoints/Games/Responses/VersionsResponse.cs @@ -0,0 +1,35 @@ +using System.Text.Json.Serialization; + +namespace CFCoreAPI.Endpoints.Games.Responses +{ + + public class VersionsResponse + { + [JsonConstructor] + public VersionsResponse( + List data + ) + { + this.Data = data; + } + + public IReadOnlyList Data { get; } + public class DataModel + { + [JsonConstructor] + public DataModel( + int type, + List versions + ) + { + this.Type = type; + this.Versions = versions; + } + + public int Type { get; } + public IReadOnlyList Versions { get; } + } + } + + +} \ No newline at end of file