From 975b6991fadde859d9905d005a0cc8856bd9bd17 Mon Sep 17 00:00:00 2001 From: mmuffins Date: Thu, 24 May 2018 20:13:39 +0200 Subject: [PATCH] Added FeaturedCategories endpoint. --- SteamStorefrontAPI/Classes/common/AppInfo.cs | 6 +++ .../Classes/{appdetails => common}/Genre.cs | 0 .../featuredcategories/FeaturedCategory.cs | 48 +++++++++++++++++ .../Endpoints/FeaturedCategories.cs | 53 +++++++++++++++++++ SteamStorefrontAPI/SteamStorefrontAPI.csproj | 4 +- SteamStorefrontConsole/Program.cs | 3 +- 6 files changed, 112 insertions(+), 2 deletions(-) rename SteamStorefrontAPI/Classes/{appdetails => common}/Genre.cs (100%) create mode 100644 SteamStorefrontAPI/Classes/featuredcategories/FeaturedCategory.cs create mode 100644 SteamStorefrontAPI/Endpoints/FeaturedCategories.cs diff --git a/SteamStorefrontAPI/Classes/common/AppInfo.cs b/SteamStorefrontAPI/Classes/common/AppInfo.cs index 6b820e8..fab2361 100644 --- a/SteamStorefrontAPI/Classes/common/AppInfo.cs +++ b/SteamStorefrontAPI/Classes/common/AppInfo.cs @@ -67,5 +67,11 @@ namespace SteamStorefrontAPI.Classes [JsonProperty("controller_support", NullValueHandling = NullValueHandling.Ignore)] [JsonConverter(typeof(ControllerSupportConverter))] public ControllerSupport? ControllerSupport { get; set; } + + [JsonProperty("headline", NullValueHandling = NullValueHandling.Ignore)] + public string Headline { get; set; } + + [JsonProperty("purchase_package", NullValueHandling = NullValueHandling.Ignore)] + public string PurchasePackage { get; set; } } } diff --git a/SteamStorefrontAPI/Classes/appdetails/Genre.cs b/SteamStorefrontAPI/Classes/common/Genre.cs similarity index 100% rename from SteamStorefrontAPI/Classes/appdetails/Genre.cs rename to SteamStorefrontAPI/Classes/common/Genre.cs diff --git a/SteamStorefrontAPI/Classes/featuredcategories/FeaturedCategory.cs b/SteamStorefrontAPI/Classes/featuredcategories/FeaturedCategory.cs new file mode 100644 index 0000000..422c465 --- /dev/null +++ b/SteamStorefrontAPI/Classes/featuredcategories/FeaturedCategory.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Globalization; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; + +namespace SteamStorefrontAPI.Classes +{ + public class FeaturedCategory + { + [JsonProperty("id")] + public string Id { get; set; } + + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("items")] + public List Items { get; set; } + + [JsonProperty("header_image")] + public string HeaderImage { get; set; } + + [JsonProperty("body")] + public string Body { get; set; } + + [JsonProperty("url")] + public string Url { get; set; } + + public static FeaturedCategory FromJson(string json) + { + var serializerSettings = new JsonSerializerSettings + { + MetadataPropertyHandling = MetadataPropertyHandling.Ignore, + DateParseHandling = DateParseHandling.None, + Converters = { + new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } + }, + }; + + return JsonConvert.DeserializeObject(json, serializerSettings); + } + } + + +} diff --git a/SteamStorefrontAPI/Endpoints/FeaturedCategories.cs b/SteamStorefrontAPI/Endpoints/FeaturedCategories.cs new file mode 100644 index 0000000..ec07e45 --- /dev/null +++ b/SteamStorefrontAPI/Endpoints/FeaturedCategories.cs @@ -0,0 +1,53 @@ +using Newtonsoft.Json.Linq; +using SteamStorefrontAPI.Classes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace SteamStorefrontAPI +{ + public static class FeaturedCategories + { + private static HttpClient client = new HttpClient(); + private const string steamBaseUri = "https://store.steampowered.com/api/featuredcategories"; + + public static async Task> GetAsync() + { + return await GetAsync(null, null); + } + + public static async Task> GetAsync(string CountryCode) + { + return await GetAsync(CountryCode, null); + } + + public static async Task> GetAsync(string CountryCode, string Language) + { + string steamUri = steamBaseUri; + steamUri = CountryCode is null ? steamUri : $"{steamUri}&cc={CountryCode}"; + steamUri = Language is null ? steamUri : $"{steamUri}&l={Language}"; + + var response = await client.GetAsync(steamUri); + if (!response.IsSuccessStatusCode) { return null; } + + var result = await response.Content.ReadAsStringAsync(); + + var jsonData = JObject.Parse(result); + if (jsonData["status"].ToString() != "1") { return null; } + + var featuredCatList = new List(); + + jsonData.Remove("status"); + + foreach (var item in jsonData.Children()) + { + featuredCatList.Add(item.First.ToObject()); + } + + return featuredCatList; + } + } +} diff --git a/SteamStorefrontAPI/SteamStorefrontAPI.csproj b/SteamStorefrontAPI/SteamStorefrontAPI.csproj index 125e7f5..95bcddf 100644 --- a/SteamStorefrontAPI/SteamStorefrontAPI.csproj +++ b/SteamStorefrontAPI/SteamStorefrontAPI.csproj @@ -43,7 +43,9 @@ + + @@ -55,7 +57,7 @@ - + diff --git a/SteamStorefrontConsole/Program.cs b/SteamStorefrontConsole/Program.cs index d0fcb78..43cca07 100644 --- a/SteamStorefrontConsole/Program.cs +++ b/SteamStorefrontConsole/Program.cs @@ -23,7 +23,8 @@ namespace SteamStorefrontConsole //var steamApp = await AppDetails.GetAsync(443790); //var steamApp = await AppDetails.GetAsync(460810, "JP"); - var featured = await Featured.GetAsync(); + //var featured = await Featured.GetAsync(); + var featuredCategories = await FeaturedCategories.GetAsync(); //Console.WriteLine(featured); }