Added featured endpoint.

This commit is contained in:
mmuffins 2018-05-23 22:58:09 +02:00
parent b2a28190dc
commit bd85216992
5 changed files with 73 additions and 25 deletions

View File

@ -22,7 +22,14 @@ namespace SteamStorefrontAPI.Classes
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{ {
if (reader.TokenType == JsonToken.Null) return null;
var value = reader.Value.ToString(); var value = reader.Value.ToString();
if(value.Length < 2)
{
return double.Parse($".{value}");
}
return double.Parse(value.Insert(value.Length - 2, ".")); return double.Parse(value.Insert(value.Length - 2, "."));
} }

View File

@ -12,7 +12,7 @@ namespace SteamStorefrontAPI.Classes
public class SteamFeatured public class SteamFeatured
{ {
[JsonProperty("large_capsules")] [JsonProperty("large_capsules")]
public List<object> LargeCapsules { get; set; } public List<FeaturedApp> LargeCapsules { get; set; }
[JsonProperty("featured_win")] [JsonProperty("featured_win")]
public List<FeaturedApp> FeaturedWin { get; set; } public List<FeaturedApp> FeaturedWin { get; set; }
@ -26,30 +26,23 @@ namespace SteamStorefrontAPI.Classes
[JsonProperty("layout")] [JsonProperty("layout")]
public string Layout { get; set; } public string Layout { get; set; }
[JsonProperty("status")] //[JsonProperty("status")]
public int Status { get; set; } //public int Status { get; set; }
public static SteamFeatured FromJson(string json) => JsonConvert.DeserializeObject<SteamFeatured>(json, Converter.Settings); public static SteamFeatured FromJson(string json)
}
public static class Serialize
{
public static string ToJson(this SteamFeatured self) => JsonConvert.SerializeObject(self, Converter.Settings);
}
static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{ {
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None, var serializerSettings = new JsonSerializerSettings
Converters = { {
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters = {
new ControllerSupportConverter(), new ControllerSupportConverter(),
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
}, },
}; };
}
return JsonConvert.DeserializeObject<SteamFeatured>(json, serializerSettings);
}
}
} }

View File

@ -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 Featured
{
private static HttpClient client = new HttpClient();
private const string steamBaseUri = "https://store.steampowered.com/api/featured";
public static async Task<SteamFeatured> GetAsync()
{
return await GetAsync(null, null);
}
public static async Task<SteamFeatured> GetAsync(string CountryCode)
{
return await GetAsync(CountryCode, null);
}
public static async Task<SteamFeatured> 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 = JToken.Parse(result);
if (jsonData["status"].ToString() != "1") { return null; }
return SteamFeatured.FromJson(result);
}
}
}

View File

@ -44,6 +44,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Classes\Converters.cs" /> <Compile Include="Classes\Converters.cs" />
<Compile Include="Endpoints\Featured.cs" />
<Compile Include="Endpoints\AppDetails.cs" /> <Compile Include="Endpoints\AppDetails.cs" />
<Compile Include="Classes\appdetails\LinuxRequirements.cs" /> <Compile Include="Classes\appdetails\LinuxRequirements.cs" />
<Compile Include="Classes\appdetails\MacRequirements.cs" /> <Compile Include="Classes\appdetails\MacRequirements.cs" />

View File

@ -19,11 +19,13 @@ namespace SteamStorefrontConsole
static async Task GetGame() static async Task GetGame()
{ {
var steamApp = Task.Run(async () => await AppDetails.GetAsync(637670)).Result; //var steamApp = await AppDetails.GetAsync(637670);
//var steamApp = Task.Run(async () => await AppDetails.GetAsync(443790)).Result; //var steamApp = await AppDetails.GetAsync(443790);
//var steamApp = await AppDetails.GetAsync(460810, "JP"); //var steamApp = await AppDetails.GetAsync(460810, "JP");
Console.WriteLine(steamApp); //var featured = await Featured.GetAsync();
//Console.WriteLine(featured);
} }
} }
} }