From 884027869b50d4edc05b83bcd2db99012775114d Mon Sep 17 00:00:00 2001 From: mmuffins Date: Thu, 24 May 2018 21:04:14 +0200 Subject: [PATCH] Added Packageinfo endpoint. --- .../{LinuxRequirements.cs => Requirements.cs} | 6 +- .../Classes/appdetails/SteamApp.cs | 15 +++-- .../Classes/{ => common}/Converters.cs | 36 ++++++++++++ .../{appdetails => common}/Platforms.cs | 0 .../{appdetails => common}/PriceOverview.cs | 4 ++ .../{appdetails => common}/ReleaseDate.cs | 0 .../FullGamepadSupport.cs} | 9 +-- .../PackageApp.cs} | 10 ++-- .../Classes/packagedetails/PackageInfo.cs | 56 +++++++++++++++++++ .../Endpoints/PackageDetails.cs | 45 +++++++++++++++ SteamStorefrontAPI/SteamStorefrontAPI.csproj | 16 +++--- SteamStorefrontConsole/Program.cs | 14 +++-- 12 files changed, 180 insertions(+), 31 deletions(-) rename SteamStorefrontAPI/Classes/appdetails/{LinuxRequirements.cs => Requirements.cs} (64%) rename SteamStorefrontAPI/Classes/{ => common}/Converters.cs (74%) rename SteamStorefrontAPI/Classes/{appdetails => common}/Platforms.cs (100%) rename SteamStorefrontAPI/Classes/{appdetails => common}/PriceOverview.cs (79%) rename SteamStorefrontAPI/Classes/{appdetails => common}/ReleaseDate.cs (100%) rename SteamStorefrontAPI/Classes/{appdetails/MacRequirements.cs => packagedetails/FullGamepadSupport.cs} (56%) rename SteamStorefrontAPI/Classes/{appdetails/PcRequirements.cs => packagedetails/PackageApp.cs} (56%) create mode 100644 SteamStorefrontAPI/Classes/packagedetails/PackageInfo.cs create mode 100644 SteamStorefrontAPI/Endpoints/PackageDetails.cs diff --git a/SteamStorefrontAPI/Classes/appdetails/LinuxRequirements.cs b/SteamStorefrontAPI/Classes/appdetails/Requirements.cs similarity index 64% rename from SteamStorefrontAPI/Classes/appdetails/LinuxRequirements.cs rename to SteamStorefrontAPI/Classes/appdetails/Requirements.cs index 8b433e0..3141eee 100644 --- a/SteamStorefrontAPI/Classes/appdetails/LinuxRequirements.cs +++ b/SteamStorefrontAPI/Classes/appdetails/Requirements.cs @@ -9,12 +9,12 @@ using Newtonsoft.Json.Converters; namespace SteamStorefrontAPI.Classes { - public class LinuxRequirements + public class Requirements { - [JsonProperty("minimum")] + [JsonProperty("minimum", NullValueHandling = NullValueHandling.Ignore)] public string Minimum { get; set; } - [JsonProperty("recommended")] + [JsonProperty("recommended", NullValueHandling = NullValueHandling.Ignore)] public string Recommended { get; set; } } } diff --git a/SteamStorefrontAPI/Classes/appdetails/SteamApp.cs b/SteamStorefrontAPI/Classes/appdetails/SteamApp.cs index c71ccdf..0c9105e 100644 --- a/SteamStorefrontAPI/Classes/appdetails/SteamApp.cs +++ b/SteamStorefrontAPI/Classes/appdetails/SteamApp.cs @@ -45,14 +45,17 @@ namespace SteamStorefrontAPI.Classes [JsonProperty("website")] public string Website { get; set; } - [JsonProperty("pc_requirements")] - public PcRequirements PcRequirements { get; set; } + [JsonProperty("pc_requirements", NullValueHandling = NullValueHandling.Ignore)] + [JsonConverter(typeof(RequirementsConverter))] + public Requirements PcRequirements { get; set; } - [JsonProperty("mac_requirements")] - public List MacRequirements { get; set; } + [JsonProperty("mac_requirements", NullValueHandling = NullValueHandling.Ignore)] + [JsonConverter(typeof(RequirementsConverter))] + public Requirements MacRequirements { get; set; } - [JsonProperty("linux_requirements")] - public List LinuxRequirements { get; set; } + [JsonProperty("linux_requirements", NullValueHandling = NullValueHandling.Ignore)] + [JsonConverter(typeof(RequirementsConverter))] + public Requirements LinuxRequirements { get; set; } [JsonProperty("legal_notice")] public string LegalNotice { get; set; } diff --git a/SteamStorefrontAPI/Classes/Converters.cs b/SteamStorefrontAPI/Classes/common/Converters.cs similarity index 74% rename from SteamStorefrontAPI/Classes/Converters.cs rename to SteamStorefrontAPI/Classes/common/Converters.cs index 67a5537..b938f70 100644 --- a/SteamStorefrontAPI/Classes/Converters.cs +++ b/SteamStorefrontAPI/Classes/common/Converters.cs @@ -65,6 +65,7 @@ namespace SteamStorefrontAPI.Classes return convertedValue; } + //TODO: fix the controller converter return null; } @@ -93,6 +94,7 @@ namespace SteamStorefrontAPI.Classes return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(parsedValue); } + //TODO: fix the epoch converter return null; } @@ -107,4 +109,38 @@ namespace SteamStorefrontAPI.Classes } } + // Returns a Requirements object if the provided data is valid, otherwise returns null + public class RequirementsConverter : JsonConverter + { + public override bool CanRead + { + get => true; + } + + public override bool CanWrite + { + get => false; + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + if(reader.TokenType != JsonToken.StartObject) return null; + + var value = serializer.Deserialize(reader); + + //TODO: Fix the requirements converter + return null; + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + throw new NotImplementedException(); + } + + public override bool CanConvert(Type objectType) + { + throw new NotImplementedException(); + } + } + } diff --git a/SteamStorefrontAPI/Classes/appdetails/Platforms.cs b/SteamStorefrontAPI/Classes/common/Platforms.cs similarity index 100% rename from SteamStorefrontAPI/Classes/appdetails/Platforms.cs rename to SteamStorefrontAPI/Classes/common/Platforms.cs diff --git a/SteamStorefrontAPI/Classes/appdetails/PriceOverview.cs b/SteamStorefrontAPI/Classes/common/PriceOverview.cs similarity index 79% rename from SteamStorefrontAPI/Classes/appdetails/PriceOverview.cs rename to SteamStorefrontAPI/Classes/common/PriceOverview.cs index bc4c856..b2cca4c 100644 --- a/SteamStorefrontAPI/Classes/appdetails/PriceOverview.cs +++ b/SteamStorefrontAPI/Classes/common/PriceOverview.cs @@ -24,5 +24,9 @@ namespace SteamStorefrontAPI.Classes [JsonProperty("discount_percent")] public int DiscountPercent { get; set; } + + [JsonProperty("individual", NullValueHandling = NullValueHandling.Ignore)] + [JsonConverter(typeof(SteamPriceStringConverter))] + public double Individual { get; set; } } } diff --git a/SteamStorefrontAPI/Classes/appdetails/ReleaseDate.cs b/SteamStorefrontAPI/Classes/common/ReleaseDate.cs similarity index 100% rename from SteamStorefrontAPI/Classes/appdetails/ReleaseDate.cs rename to SteamStorefrontAPI/Classes/common/ReleaseDate.cs diff --git a/SteamStorefrontAPI/Classes/appdetails/MacRequirements.cs b/SteamStorefrontAPI/Classes/packagedetails/FullGamepadSupport.cs similarity index 56% rename from SteamStorefrontAPI/Classes/appdetails/MacRequirements.cs rename to SteamStorefrontAPI/Classes/packagedetails/FullGamepadSupport.cs index 277a06c..c4cc1ed 100644 --- a/SteamStorefrontAPI/Classes/appdetails/MacRequirements.cs +++ b/SteamStorefrontAPI/Classes/packagedetails/FullGamepadSupport.cs @@ -9,12 +9,9 @@ using Newtonsoft.Json.Converters; namespace SteamStorefrontAPI.Classes { - public class MacRequirements + public class FullGamepadSupport { - [JsonProperty("minimum")] - public string Minimum { get; set; } - - [JsonProperty("recommended")] - public string Recommended { get; set; } + [JsonProperty("full_gamepad")] + public bool FullGamepad { get; set; } } } diff --git a/SteamStorefrontAPI/Classes/appdetails/PcRequirements.cs b/SteamStorefrontAPI/Classes/packagedetails/PackageApp.cs similarity index 56% rename from SteamStorefrontAPI/Classes/appdetails/PcRequirements.cs rename to SteamStorefrontAPI/Classes/packagedetails/PackageApp.cs index 0d0d257..dc6cd13 100644 --- a/SteamStorefrontAPI/Classes/appdetails/PcRequirements.cs +++ b/SteamStorefrontAPI/Classes/packagedetails/PackageApp.cs @@ -9,12 +9,12 @@ using Newtonsoft.Json.Converters; namespace SteamStorefrontAPI.Classes { - public class PcRequirements + public partial class PackageApp { - [JsonProperty("minimum")] - public string Minimum { get; set; } + [JsonProperty("id")] + public int Id { get; set; } - [JsonProperty("recommended")] - public string Recommended { get; set; } + [JsonProperty("name")] + public string Name { get; set; } } } diff --git a/SteamStorefrontAPI/Classes/packagedetails/PackageInfo.cs b/SteamStorefrontAPI/Classes/packagedetails/PackageInfo.cs new file mode 100644 index 0000000..f0e0a15 --- /dev/null +++ b/SteamStorefrontAPI/Classes/packagedetails/PackageInfo.cs @@ -0,0 +1,56 @@ +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 PackageInfo + { + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("purchase_text")] + public string PurchaseText { get; set; } + + [JsonProperty("page_image")] + public string PageImage { get; set; } + + [JsonProperty("small_logo")] + public string SmallLogo { get; set; } + + [JsonProperty("apps")] + public List Apps { get; set; } + + [JsonProperty("price")] + public PriceOverview Price { get; set; } + + [JsonProperty("platforms")] + public Platforms Platforms { get; set; } + + [JsonProperty("controller")] + public FullGamepadSupport Controller { get; set; } + + [JsonProperty("release_date")] + public ReleaseDate ReleaseDate { get; set; } + + public static SteamApp 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/PackageDetails.cs b/SteamStorefrontAPI/Endpoints/PackageDetails.cs new file mode 100644 index 0000000..4d952e2 --- /dev/null +++ b/SteamStorefrontAPI/Endpoints/PackageDetails.cs @@ -0,0 +1,45 @@ +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 PackageDetails + { + private static HttpClient client = new HttpClient(); + private const string steamBaseUri = "http://store.steampowered.com/api/packagedetails"; + + public static async Task GetAsync(int PackageId) + { + return await GetAsync(PackageId, null, null); + } + + public static async Task GetAsync(int PackageId, string CountryCode) + { + return await GetAsync(PackageId, CountryCode, null); + } + + public static async Task GetAsync(int PackageId, string CountryCode, string Language) + { + string steamUri = $"{steamBaseUri}?packageids={PackageId}"; + 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(); + + // The actual payload is wrapped, drill down to the third level to get to it + var jsonData = JToken.Parse(result).First.First; + if (!bool.Parse(jsonData["success"].ToString())) { return null; } + + return jsonData["data"].ToObject(); + } + } +} diff --git a/SteamStorefrontAPI/SteamStorefrontAPI.csproj b/SteamStorefrontAPI/SteamStorefrontAPI.csproj index 95bcddf..a7ea9a1 100644 --- a/SteamStorefrontAPI/SteamStorefrontAPI.csproj +++ b/SteamStorefrontAPI/SteamStorefrontAPI.csproj @@ -44,17 +44,19 @@ - + + + + + - - - + - - - + + + diff --git a/SteamStorefrontConsole/Program.cs b/SteamStorefrontConsole/Program.cs index 43cca07..02f5072 100644 --- a/SteamStorefrontConsole/Program.cs +++ b/SteamStorefrontConsole/Program.cs @@ -19,12 +19,18 @@ namespace SteamStorefrontConsole static async Task GetGame() { - //var steamApp = await AppDetails.GetAsync(637670); - //var steamApp = await AppDetails.GetAsync(443790); - //var steamApp = await AppDetails.GetAsync(460810, "JP"); + //var steamApp1 = await AppDetails.GetAsync(637670); + //var steamApp2 = await AppDetails.GetAsync(443790); + //var steamApp3 = await AppDetails.GetAsync(460810, "JP"); + //var steamApp4 = await AppDetails.GetAsync(322330); + + var package1 = await PackageDetails.GetAsync(68179); + var package2 = await PackageDetails.GetAsync(68179, "JP"); + var package3 = await PackageDetails.GetAsync(235158); + var package4 = await PackageDetails.GetAsync(235158, "US"); //var featured = await Featured.GetAsync(); - var featuredCategories = await FeaturedCategories.GetAsync(); + //var featuredCategories = await FeaturedCategories.GetAsync(); //Console.WriteLine(featured); }