// deserializes a GET response into an object private static void Get <T>(string uri, OnRestSuccess <T> onSuccess = null, OnRestFail onFail = null, bool sendToken = true) { GetString(uri, (string text) => { if (onSuccess != null) { Debug.Log(text); T obj = JsonConvert.DeserializeObject <T>(text); onSuccess(obj); } }, onFail, sendToken); }
// this builds a GET request, sends it, // awaits for a response, and then triggers // either onSuccess or onFail private static async void GetString(string uri, OnRestSuccess onSuccess = null, OnRestFail onFail = null, bool sendToken = true) { if (sendToken && token != null && token.expires_in == 0) { return; // we lost the token } UnityWebRequest request = UnityWebRequest.Get(uri); request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded"); if (sendToken && token != null) { request.SetRequestHeader("Authorization", $"Bearer {token.access_token}"); } UnityWebRequestAsyncOperation task = request.SendWebRequest(); while (!task.isDone) { await Task.Yield(); } RouteResponse(request, onSuccess, onFail); }
public static void Get_PuzzleCollectionList(Ordering order, OnRestSuccess <ResponsePuzzleCollection> onSuccess, OnRestFail onFail = null) { string ordering = "-rating"; if (order == Ordering.HighestDifficulty) { ordering = "-min_rank,-max_rank"; } if (order == Ordering.LowestDifficulty) { ordering = "min_rank,max_rank"; } if (order == Ordering.Newest) { ordering = "-created"; } if (order == Ordering.Oldest) { ordering = "+created"; } Get <ResponsePuzzleCollection>($"{pathAPI}puzzles/collections?ordering={ordering}", onSuccess, (string error) => { }); }
public static void Get_PuzzleList(OnRestSuccess <ResponsePuzzleList> onSuccess, OnRestFail onFail = null) { Get <ResponsePuzzleList>($"{pathAPI}puzzles", onSuccess, onFail); }
public static void Get_FriendsList(OnRestSuccess <ResponseFriendsList> onSuccess, OnRestFail onFail = null) { Get <ResponseFriendsList>($"{pathAPI}me/friends", onSuccess, onFail); }
public static void Get_GamesList(OnRestSuccess <ResponseGameList> onSuccess, OnRestFail onFail = null) { Get <ResponseGameList>($"{pathAPI}me/games", onSuccess, onFail); }
public static void Get_MyProfile(OnRestSuccess <ResponseMyProfile> onSuccess, OnRestFail onFail = null) { Get <ResponseMyProfile>($"{pathAPI}me", onSuccess, onFail); }
public static void Login(string username, string password, OnRestSuccess onSuccess, OnRestFail onFail = null) { List <IMultipartFormSection> data = MakePostData(); data.Add("client_id", clientID); data.Add("client_secret", clientSecret); data.Add("grant_type", "password"); data.Add("username", username); data.Add("password", password); Post <OAuthToken>(pathAuth, data, (t) => { token = t; Debug.Log(token); onSuccess(""); }, onFail, false); }
// deserializes a POST response into an object private static void Post <T>(string uri, List <IMultipartFormSection> data = null, OnRestSuccess <T> onSuccess = null, OnRestFail onFail = null, bool sendToken = true) { PostString(uri, data, (string text) => { if (onSuccess != null) { T obj = JsonConvert.DeserializeObject <T>(text); onSuccess(obj); } }, onFail, sendToken); }
// this builds a POST request, sends it, // awaits for a response, and then triggers // either onSuccess or onFail private static async void PostString(string uri, List <IMultipartFormSection> data = null, OnRestSuccess onSuccess = null, OnRestFail onFail = null, bool sendToken = true, string method = "POST") { if (sendToken && token.expires_in == 0) { return; // we lost the token } if (data == null) { data = MakePostData(); } byte[] boundary = UnityWebRequest.GenerateBoundary(); UnityWebRequest request = UnityWebRequest.Post(uri, data, boundary); request.method = method; if (sendToken) { request.SetRequestHeader("Authorization", $"Bearer {token.access_token}"); } UnityWebRequestAsyncOperation task = request.SendWebRequest(); while (!task.isDone) { await Task.Yield(); } RouteResponse(request, onSuccess, onFail); }
// analyzes the result of a web request // and calls either onSuccess or onFail private static void RouteResponse(UnityWebRequest request, OnRestSuccess onSuccess, OnRestFail onFail) { switch (request.result) { case UnityWebRequest.Result.Success: if (onSuccess != null) { onSuccess(request.downloadHandler.text); } break; case UnityWebRequest.Result.InProgress: // this shouldn't happen? Debug.Log("waiting... [REST in progress]"); break; case UnityWebRequest.Result.ConnectionError: case UnityWebRequest.Result.ProtocolError: case UnityWebRequest.Result.DataProcessingError: Debug.LogError($"{request.error}"); if (onFail != null) { onFail(request.error); } break; } }
public static void Get_PuzzleRate(int puzzle_id, OnRestSuccess <ResponsePuzzleRate> onSuccess, OnRestFail onFail = null) { Get <ResponsePuzzleRate>($"{pathAPI}puzzles/{puzzle_id}/rate", onSuccess, onFail); }
// this loads the id / name of each puzzle in a collection public static void Get_PuzzleCollectionSummary(int puzzle_id, OnRestSuccess <ResponsePuzzleSummary.Puzzle[]> onSuccess, OnRestFail onFail = null) { Get <ResponsePuzzleSummary.Puzzle[]>($"{pathAPI}puzzles/{puzzle_id}/collection_summary", onSuccess, onFail); }