From bf8f22a3d688727e44f626f9581d95e22300bf48 Mon Sep 17 00:00:00 2001 From: Kalakoi Date: Fri, 21 Jun 2019 14:17:23 -0400 Subject: [PATCH] Add files via upload --- src/Kalakoi.Xbox.OpenXBL.csproj | 75 +++++++ src/XboxConnection.cs | 357 ++++++++++++++++++++++++++++++++ src/packages.config | 4 + 3 files changed, 436 insertions(+) create mode 100644 src/Kalakoi.Xbox.OpenXBL.csproj create mode 100644 src/XboxConnection.cs create mode 100644 src/packages.config diff --git a/src/Kalakoi.Xbox.OpenXBL.csproj b/src/Kalakoi.Xbox.OpenXBL.csproj new file mode 100644 index 0000000..d8eef41 --- /dev/null +++ b/src/Kalakoi.Xbox.OpenXBL.csproj @@ -0,0 +1,75 @@ + + + + + Debug + AnyCPU + {C1A3C6A1-4817-42C7-986A-2F51F223CFEA} + Library + Properties + Kalakoi.Xbox.OpenXBL + Kalakoi.Xbox.OpenXBL + v4.7.1 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll + True + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/XboxConnection.cs b/src/XboxConnection.cs new file mode 100644 index 0000000..49ba2f8 --- /dev/null +++ b/src/XboxConnection.cs @@ -0,0 +1,357 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Serialization; + +namespace Kalakoi.Xbox.OpenXBL +{ + public static class XboxConnection + { + public const string BASEURI = "https://xbl.io/api/v2"; + + public static string APIKEY = string.Empty; + + public static void SetApiKey(string Key) + { + APIKEY = Key; + } + + public static async Task XuidFromGamertagAsync(string Gamertag) + { + string Endpoint = string.Format("/friends/search?gt={0}", Gamertag); + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = await RestServices.GetResponseAsync(RequestAddress, APIKEY).ConfigureAwait(false); + XboxProfile p = new XboxProfile(Response); + return p.ID.ToString(); + } + + public static string XuidFromGamertag(string Gamertag) + { + string Endpoint = string.Format("/friends/search?gt={0}", Gamertag); + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + XboxProfile p = new XboxProfile(Response); + return p.ID.ToString(); + } + + public static async Task GetProfileAsync() + { + string Endpoint = "/account"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = await RestServices.GetResponseAsync(RequestAddress, APIKEY).ConfigureAwait(false); + return new XboxProfile(Response); + } + + public static XboxProfile GetProfile() + { + string Endpoint = "/account"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + return new XboxProfile(Response); + } + + public static async Task GetProfileAsync(string Gamertag) + { + string Endpoint = string.Format("/friends/search?gt={0}", Gamertag); + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = await RestServices.GetResponseAsync(RequestAddress, APIKEY).ConfigureAwait(false); + return new XboxProfile(Response); + } + + public static XboxProfile GetProfile(string Gamertag) + { + string Endpoint = string.Format("/friends/search?gt={0}", Gamertag); + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + return new XboxProfile(Response); + } + + public static async Task> GetFriendsAsync() + { + string Endpoint = "/friends"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = await RestServices.GetResponseAsync(RequestAddress, APIKEY).ConfigureAwait(false); + List Return = new List(); + List tokens = new List(JObject.Parse(Response).SelectTokens("people")); + foreach (JToken t in tokens.Children()) + Return.Add(new Friend(Person.DeserializeJSON(t))); + return Return; + } + + public static IEnumerable GetFriends() + { + string Endpoint = "/friends"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + List tokens = new List(JObject.Parse(Response).SelectTokens("people")); + foreach (JToken t in tokens.Children()) + yield return new Friend(Person.DeserializeJSON(t)); + } + + public static async Task> GetFriendsAsync(string xuid) + { + string Endpoint = "/friends?xuid="; + Uri RequestAddress = new Uri(string.Format("{0}{1}{2}", BASEURI, Endpoint, xuid)); + string Response = await RestServices.GetResponseAsync(RequestAddress, APIKEY).ConfigureAwait(false); + List Return = new List(); + List tokens = new List(JObject.Parse(Response).SelectTokens("people")); + foreach (JToken t in tokens.Children()) + Return.Add(new Friend(Person.DeserializeJSON(t))); + return Return; + } + + public static IEnumerable GetFriends(string xuid) + { + string Endpoint = "/friends?xuid="; + Uri RequestAddress = new Uri(string.Format("{0}{1}{2}", BASEURI, Endpoint, xuid)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + List tokens = new List(JObject.Parse(Response).SelectTokens("people")); + foreach (JToken t in tokens.Children()) + yield return new Friend(Person.DeserializeJSON(t)); + } + + public static async Task AddFriendAsync(string xuid) + { + string Endpoint = string.Format("/friends/add/{0}", xuid); + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = await RestServices.GetResponseAsync(RequestAddress, APIKEY).ConfigureAwait(false); + } + + public static void AddFriend(string xuid) + { + string Endpoint = string.Format("/friends/add/{0}", xuid); + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + } + + public static async Task RemoveFriendAsync(string xuid) + { + string Endpoint = string.Format("/friends/remove/{0}", xuid); + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = await RestServices.GetResponseAsync(RequestAddress, APIKEY).ConfigureAwait(false); + } + + public static void RemoveFriend(string xuid) + { + string Endpoint = string.Format("/friends/remove/{0}", xuid); + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + } + + public static async Task FavoriteFriendAsync(string xuid) + { + string Endpoint = "/friends/favorite"; + //string Data = string.Format("{\"xuids\":[{0}]}", xuid); + string Data = "{\"xuids\":[" + xuid + "]}"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = await RestServices.GetPostResponseAsync(RequestAddress, Data, APIKEY).ConfigureAwait(false); + } + + public static void FavoriteFriend(string xuid) + { + string Endpoint = "/friends/favorite"; + //string Data = string.Format("{\"xuids\":[{0}]}", xuid); + string Data = "{\"xuids\":[" + xuid + "]}"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetPostResponse(RequestAddress, Data, APIKEY); + } + + public static async Task UnfavoriteFriendAsync(string xuid) + { + string Endpoint = "/friends/favorite/remove"; + //string Data = string.Format("{\"xuids\":[{0}]}", xuid); + string Data = "{\"xuids\":[" + xuid + "]}"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = await RestServices.GetPostResponseAsync(RequestAddress, Data, APIKEY).ConfigureAwait(false); + } + + public static void UnfavoriteFriend(string xuid) + { + string Endpoint = "/friends/favorite/remove"; + //string Data = string.Format("{\"xuids\":[{0}]}", xuid); + string Data = "{\"xuids\":[" + xuid +"]}"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetPostResponse(RequestAddress, Data, APIKEY); + } + + public static async Task> GetRecentPlayersAsync() + { + string Endpoint = "/recent-players"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = await RestServices.GetResponseAsync(RequestAddress, APIKEY).ConfigureAwait(false); + List Return = new List(); + List tokens = new List(JObject.Parse(Response).SelectTokens("people")); + foreach (JToken t in tokens.Children()) + Return.Add(new Friend(Person.DeserializeJSON(t))); + return Return; + } + + public static IEnumerable GetRecentPlayers() + { + string Endpoint = "/recent-players"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + List tokens = new List(JObject.Parse(Response).SelectTokens("people")); + foreach (JToken t in tokens.Children()) + yield return new Friend(Person.DeserializeJSON(t)); + } + + public static async Task> GetConversationsAsync() + { + string Endpoint = "/conversations"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = await RestServices.GetResponseAsync(RequestAddress, APIKEY).ConfigureAwait(false); + List tokens = new List(JObject.Parse(Response).SelectTokens("results")); + List l = new List(); + foreach (JToken t in tokens.Children()) + l.Add(ConversationSummary.DeserializeJSON(t)); + return l; + } + + public static IEnumerable GetConversations() + { + string Endpoint = "/conversations"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + List tokens = new List(JObject.Parse(Response).SelectTokens("results")); + foreach (JToken t in tokens.Children()) + yield return ConversationSummary.DeserializeJSON(t); + } + + public static async Task GetConversationAsync(string xuid) + { + string Endpoint = string.Format("/conversations/{0}", xuid); + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = await RestServices.GetResponseAsync(RequestAddress, APIKEY).ConfigureAwait(false); + return Conversation.DeserializeJSON(Response); + } + + public static Conversation GetConversation(string xuid) + { + string Endpoint = string.Format("/conversations/{0}", xuid); + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + return Conversation.DeserializeJSON(Response); + } + + public static async Task SendMessageAsync(string Gamertag, string Message) + { + string Endpoint = "/conversations"; + //string Data = string.Format("{\"to\":\"{0}\",\"message\":\"{1}\"}", Gamertag, Message); + string Data = "{\"to\":\"" + Gamertag + "\",\"message\":\"" + Message + "\"}"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = await RestServices.GetPostResponseAsync(RequestAddress, Data, APIKEY).ConfigureAwait(false); + } + + public static void SendMessage(string Gamertag, string Message) + { + string Endpoint = "/conversations"; + //string Data = string.Format("{\"to\":\"{0}\",\"message\":\"{1}\"}", Gamertag, Message); + string Data = "{\"to\":\"" + Gamertag + "\",\"message\":\"" + Message + "\"}"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetPostResponse(RequestAddress, Data, APIKEY); + } + + public static async Task> GetAlertsAsync() + { + string Endpoint = "/alerts"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = await RestServices.GetResponseAsync(RequestAddress, APIKEY).ConfigureAwait(false); + List tokens = new List(JObject.Parse(Response).SelectTokens("alerts")); + List l = new List(); + foreach (JToken t in tokens.Children()) + l.Add(Alert.DeserializeJSON(t)); + return l; + } + + public static IEnumerable GetAlerts() + { + string Endpoint = "/alerts"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + List tokens = new List(JObject.Parse(Response).SelectTokens("alerts")); + List l = new List(); + foreach (JToken t in tokens.Children()) + yield return Alert.DeserializeJSON(t); + } + + public static string GetFeed() + { + string Endpoint = "/activity/feed"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + return Response; + } + + public static string GetHistory() + { + string Endpoint = "/activity/history"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + return Response; + } + + public static async Task GetSummaryAsync() + { + string Endpoint = "/player/summary"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = await RestServices.GetResponseAsync(RequestAddress, APIKEY).ConfigureAwait(false); + return new Friend(Response); + } + + public static Friend GetSummary() + { + string Endpoint = "/player/summary"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + return new Friend(Response); + } + + public static string GetClips() + { + string Endpoint = "/dvr/gameclips"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + return Response; + } + + public static string GetScreenshots() + { + string Endpoint = "/dvr/screenshots"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + return Response; + } + + public static string GetAchievements() + { + string Endpoint = "/achievements"; + Uri RequestAddress = new Uri(string.Format("{0}{1}", BASEURI, Endpoint)); + string Response = RestServices.GetResponse(RequestAddress, APIKEY); + return Response; + } + + //TODO: + //GET /account/{xuid} + //GET /friends?xuid={xuid} + //GET /presence + //GET /{xuid}/presence + //POST /generate/gamertag Payload: {"Algorithm":1,"Count":3,"Locale":"en-US","Seed":""} + //POST /clubs/recommendations + //GET /clubs/owned + //POST /clubs/create Payload: {"name":"Hello World", "type":"[public/private/hidden]"} + //GET /clubs/find?q={query} + //POST /clubs/reserve Payload: {"name":"Hello World"} + //GET /activity/feed + //POST /activity/feed + //GET /activity/history + //GET /dvr/gameclips + //GET /dvr/screenshots + //GET /achievements + //GET /achievements/{titleid} + } +} diff --git a/src/packages.config b/src/packages.config new file mode 100644 index 0000000..93ffd08 --- /dev/null +++ b/src/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file