Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #118 from aspriddell/endpoints-cleanup
Browse files Browse the repository at this point in the history
Cleanup Endpoints and other classes
  • Loading branch information
aspriddell authored Oct 13, 2020
2 parents 614606c + f7eab85 commit 30c0996
Show file tree
Hide file tree
Showing 19 changed files with 153 additions and 106 deletions.
2 changes: 1 addition & 1 deletion DragonFruit.Six.API.Tests.Common/Dragon6DemoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected override T ValidateAndProcess<T>(HttpResponseMessage response, HttpReq
catch (UbisoftErrorException)
{
//pretend to be ubisoft club and try again
AppId = UbisoftIdentifiers.Websites[UbisoftService.UbisoftClub];
AppId = UbisoftService.UbisoftClub.AppId();
return Perform<T>(request);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ public static class AccountInfoDeserializer
{
public static IEnumerable<AccountInfo> DeserializeAccountInfo(this JObject jObject)
{
var json = JArray.FromObject(jObject[Misc.Profile]);
var accountsJson = jObject[Misc.Profile];

foreach (var jToken in json)
if (accountsJson == null)
yield break;

foreach (var jToken in JArray.FromObject(accountsJson))
{
var item = (JObject)jToken;
yield return new AccountInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using DragonFruit.Common.Data.Extensions;
using DragonFruit.Six.API.Data.Containers;
using DragonFruit.Six.API.Data.Strings;
using DragonFruit.Six.API.Helpers;
using Newtonsoft.Json.Linq;

namespace DragonFruit.Six.API.Data.Deserializers
Expand All @@ -15,22 +16,24 @@ public static class AccountLoginInfoDeserializer
{
public static IEnumerable<AccountLoginInfo> DeserializeAccountLoginInfo(this JObject jObject)
{
var data = (JArray)jObject["applications"];
var platformLookup = Endpoints.GameIds.ToDictionary(x => x.Value, x => x.Key);
var data = jObject["applications"] as JArray;

if (data == null)
yield break;

foreach (var jToken in data)
{
var entry = (JObject)jToken;
yield return new AccountLoginInfo
{
Guid = entry.GetString(Login.Guid),
SessionCount = entry.GetUInt(Login.Sessions),
Platform = UbisoftIdentifiers.GameIds.SingleOrDefault(x => x.Value.Equals(entry.GetString(Login.PlatformId), StringComparison.OrdinalIgnoreCase)).Key,
Activity = new ActivityDateContainer
{
First = DateTimeOffset.Parse(entry.GetString(Login.FirstLogin), References.Culture),
Last = DateTimeOffset.Parse(entry.GetString(Login.LastLogin), References.Culture)
},
SessionCount = entry.GetUInt(Login.Sessions),
Platform = platformLookup[entry.GetString(Login.PlatformId)]
}
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ public static class GeneralStatsDeserializer
{
public static GeneralStats DeserializeGeneralStatsFor(this JObject jObject, string guid)
{
if (jObject == null)
{
return new GeneralStats();
}
// try to get the user but if there is nothing return null
var json = jObject[Misc.Results]?[guid] as JObject;

var json = (JObject)jObject[Misc.Results][guid];
if (json == null)
return null;

var result = new GeneralStats
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ public static class OperatorStatsDeserializer
{
public static IEnumerable<OperatorStats> DeserializeOperatorStatsFor(this JObject jObject, string guid, IEnumerable<OperatorStats> data)
{
var json = (JObject)jObject[Misc.Results]![guid];
var json = jObject[Misc.Results]?[guid] as JObject;

if (json == null)
yield break;

foreach (var op in data.Select(x => x.Clone()))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ public static class PlayerLevelStatsDeserializer
{
public static IEnumerable<PlayerLevelStats> DeserializePlayerLevelStats(this JObject jObject)
{
foreach (var element in JArray.FromObject(jObject["player_profiles"]))
var profiles = jObject["player_profiles"];

if (profiles == null)
yield break;

foreach (var profile in JArray.FromObject(profiles))
{
var result = element.ToObject<PlayerLevelStats>();
result.Guid = (string)element["profile_id"];
var result = profile.ToObject<PlayerLevelStats>();

// todo do we need a null check?
result!.Guid = (string)profile["profile_id"];
yield return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public static class SeasonStatsDeserializer
{
public static SeasonStats DeserializeSeasonStatsFor(this JObject jObject, string guid)
{
var json = (JObject)jObject[Misc.Players][guid];
var json = jObject[Misc.Players]?[guid] as JObject;

if (json == null)
return null;

return new SeasonStats
{
Expand Down
10 changes: 7 additions & 3 deletions DragonFruit.Six.API/Data/Deserializers/TokenDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ public static class TokenDeserializer
public static UbisoftToken DeserializeToken(this JObject jObject)
{
var token = jObject.ToObject<UbisoftToken>();

if (token == null)
return null;

token.Account = new AccountInfo
{
Platform = PlatformParser.PlatformEnumFor(jObject.GetString(Accounts.PlatformIdentifier, "uplay")),
PlayerName = jObject.GetString(Accounts.Name),
Identifiers = new UserIdentifierContainer
{
Platform = jObject.GetString(Accounts.ProfileIdentifier)
},
Platform = PlatformParser.PlatformEnumFor(jObject.GetString(Accounts.PlatformIdentifier, "uplay")),
PlayerName = jObject.GetString(Accounts.Name),
}
};

return token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public static class WeaponStatsDeserializer
{
public static IEnumerable<WeaponStats> DeserializeWeaponStatsFor(this JObject jObject, string guid)
{
var json = (JObject)jObject[Misc.Results][guid];
var json = jObject[Misc.Results]?[guid] as JObject;

if (json == null)
yield break;

foreach (var index in References.WeaponClasses.Keys)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static AccountLoginInfo GetLoginInfo<T>(this T client, AccountInfo accoun

public static IEnumerable<AccountLoginInfo> GetLoginInfo<T>(this T client, IEnumerable<AccountInfo> accounts) where T : Dragon6Client
{
var data = client.Perform<JObject>(new AccountLoginInfoRequest(Endpoints.GameIds.Select(x => x.Value), accounts));
var data = client.Perform<JObject>(new AccountLoginInfoRequest(accounts));
return data.DeserializeAccountLoginInfo();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace DragonFruit.Six.API.Data.Extensions
{
public static class GeneralStatsExtentions
{
public static GeneralStats GetStats<T>(this T client, AccountInfo account) where T : Dragon6Client
public static GeneralStats GetStats<T>(this T client, AccountInfo account) where T : Dragon6Client
=> GetStats(client, new[] { account }).First();

public static IEnumerable<GeneralStats> GetStats<T>(this T client, IEnumerable<AccountInfo> accounts) where T : Dragon6Client
Expand Down
27 changes: 7 additions & 20 deletions DragonFruit.Six.API/Data/Requests/AccountLoginInfoRequests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,21 @@
using System.Linq;
using DragonFruit.Common.Data.Parameters;
using DragonFruit.Six.API.Data.Requests.Base;
using DragonFruit.Six.API.Helpers;

namespace DragonFruit.Six.API.Data.Requests
{
public sealed class AccountLoginInfoRequest : UbiApiRequest
public class AccountLoginInfoRequest : UbiApiRequest
{
public AccountLoginInfoRequest()
public AccountLoginInfoRequest(AccountInfo profile)
: this(new[] { profile })
{
}

public AccountLoginInfoRequest(string appId, AccountInfo profileId)
: this(new[] { appId }, new[] { profileId })
public AccountLoginInfoRequest(IEnumerable<AccountInfo> profiles)
{
}

public AccountLoginInfoRequest(string appId, IEnumerable<AccountInfo> profileIds)
: this(new[] { appId }, profileIds)
{
}

public AccountLoginInfoRequest(IEnumerable<string> appIds, AccountInfo profileId)
: this(appIds, new[] { profileId })
{
}

public AccountLoginInfoRequest(IEnumerable<string> appIds, IEnumerable<AccountInfo> profileIds)
{
AppIds = appIds;
Accounts = profileIds;
Accounts = profiles;
AppIds = UbisoftIdentifiers.GameIds.Values;
}

public override string Path => $"{Endpoints.IdServer}/applications";
Expand Down
3 changes: 1 addition & 2 deletions DragonFruit.Six.API/Data/Requests/Base/BasicStatsRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under Apache-2. Please refer to the LICENSE file for more info

using System.Collections.Generic;
using System.Linq;
using DragonFruit.Common.Data.Parameters;

namespace DragonFruit.Six.API.Data.Requests.Base
Expand All @@ -12,7 +11,7 @@ namespace DragonFruit.Six.API.Data.Requests.Base
/// </summary>
public class BasicStatsRequest : PlatformSpecificRequest
{
public override string Path => Endpoints.Stats[Accounts.First().Platform];
public override string Path => Platform.StatsEndpoint();

public BasicStatsRequest(AccountInfo account)
: base(new[] { account })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace DragonFruit.Six.API.Data.Requests
{
public sealed class PlayerLevelStatsRequest : PlatformSpecificRequest
{
public override string Path => Endpoints.ProfileInfo[Platform];
public override string Path => Platform.ProfileStatsEndpoint();

protected override bool RequireAuth => true;

Expand Down
2 changes: 1 addition & 1 deletion DragonFruit.Six.API/Data/Requests/SeasonStatsRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace DragonFruit.Six.API.Data.Requests
{
public sealed class SeasonStatsRequest : PlatformSpecificRequest
{
public override string Path => Endpoints.RankedStats[Platform];
public override string Path => Platform.SeasonalStatsEndpoint();

public SeasonStatsRequest(IEnumerable<AccountInfo> accounts)
: base(accounts)
Expand Down
2 changes: 1 addition & 1 deletion DragonFruit.Six.API/Dragon6Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected Dragon6Client(string userAgent, string appId)
protected Dragon6Client()
{
Serializer = new ApiJsonSerializer(References.Culture);
AppId = UbisoftIdentifiers.Websites[UbisoftService.RainbowSix];
AppId = UbisoftService.RainbowSix.AppId();

if (string.IsNullOrEmpty(UserAgent))
{
Expand Down
65 changes: 20 additions & 45 deletions DragonFruit.Six.API/Endpoints.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,38 @@
// Dragon6 API Copyright 2020 DragonFruit Network <inbox@dragonfruit.network>
// Licensed under Apache-2. Please refer to the LICENSE file for more info

using System.Collections.Generic;
using DragonFruit.Six.API.Data;
using System;
using DragonFruit.Six.API.Enums;
using DragonFruit.Six.API.Helpers;

namespace DragonFruit.Six.API
{
public static class Endpoints
{
/// <summary>
/// Public API endpoint
/// </summary>
public static readonly string BaseEndpoint = "https://public-ubiservices.ubi.com";

public static readonly string StatsBase = BaseEndpoint + "/v1/spaces";
public static readonly string IdServer = BaseEndpoint + "/v3/profiles";

/// <summary>
/// Used for <see cref="AccountLoginInfo"/>
/// Public-facing endpoint for generating access tokens
/// </summary>
public static readonly Dictionary<Platform, string> GameIds =
new Dictionary<Platform, string>
{
[Platform.PSN] = "fb4cc4c9-2063-461d-a1e8-84a7d36525fc",
[Platform.XB1] = "4008612d-3baf-49e4-957a-33066726a7bc",
[Platform.PC] = "e3d5ea9e-50bd-43b7-88bf-39794f4e3d40"
};
public static readonly string IdServer = BaseEndpoint + "/v3/profiles";

/// <summary>
/// Game ids used for getting stats
/// Url of the platform-specific "sandbox", the place where all the stats are acquired from
/// </summary>
public static readonly Dictionary<Platform, string> GameSpaceIds =
new Dictionary<Platform, string>
{
[Platform.PSN] = "05bfb3f7-6c21-4c42-be1f-97a33fb5cf66",
[Platform.XB1] = "98a601e5-ca91-4440-b1c5-753f601a2c90",
[Platform.PC] = "5172a557-50b5-4665-b7db-e3f2e8c5041d"
};

public static readonly Dictionary<Platform, string> Stats =
new Dictionary<Platform, string>
{
[Platform.PSN] = $"{StatsBase}/{GameSpaceIds[Platform.PSN]}/sandboxes/OSBOR_PS4_LNCH_A/playerstats2/statistics",
[Platform.XB1] = $"{StatsBase}/{GameSpaceIds[Platform.XB1]}/sandboxes/OSBOR_XBOXONE_LNCH_A/playerstats2/statistics",
[Platform.PC] = $"{StatsBase}/{GameSpaceIds[Platform.PC]}/sandboxes/OSBOR_PC_LNCH_A/playerstats2/statistics"
};

public static readonly Dictionary<Platform, string> RankedStats =
new Dictionary<Platform, string>
{
[Platform.PSN] = $"{StatsBase}/{GameSpaceIds[Platform.PSN]}/sandboxes/OSBOR_PS4_LNCH_A/r6karma/players",
[Platform.XB1] = $"{StatsBase}/{GameSpaceIds[Platform.XB1]}/sandboxes/OSBOR_XBOXONE_LNCH_A/r6karma/players",
[Platform.PC] = $"{StatsBase}/{GameSpaceIds[Platform.PC]}/sandboxes/OSBOR_PC_LNCH_A/r6karma/players"
};

public static readonly Dictionary<Platform, string> ProfileInfo =
new Dictionary<Platform, string>
{
[Platform.PSN] = $"{StatsBase}/{GameSpaceIds[Platform.PSN]}/sandboxes/OSBOR_PS4_LNCH_A/r6playerprofile/playerprofile/progressions",
[Platform.XB1] = $"{StatsBase}/{GameSpaceIds[Platform.XB1]}/sandboxes/OSBOR_XBOXONE_LNCH_A/r6playerprofile/playerprofile/progressions",
[Platform.PC] = $"{StatsBase}/{GameSpaceIds[Platform.PC]}/sandboxes/OSBOR_PC_LNCH_A/r6playerprofile/playerprofile/progressions"
};
private static string SandboxUrlFor(this Platform platform) => $"{BaseEndpoint}/v1/spaces/{platform.GameSpaceId()}/sandboxes" + platform switch
{
Platform.PSN => "/OSBOR_PS4_LNCH_A",
Platform.XB1 => "/OSBOR_XBOXONE_LNCH_A",
Platform.PC => "/OSBOR_PC_LNCH_A",

_ => throw new ArgumentOutOfRangeException()
};

public static string StatsEndpoint(this Platform platform) => $"{SandboxUrlFor(platform)}/playerstats2/statistics";
public static string SeasonalStatsEndpoint(this Platform platform) => $"{SandboxUrlFor(platform)}/r6karma/players";
public static string ProfileStatsEndpoint(this Platform platform) => $"{SandboxUrlFor(platform)}/r6playerprofile/playerprofile/progressions";
}
}
Loading

0 comments on commit 30c0996

Please sign in to comment.