public Promise Relogin() { IDispatcher dispatcher = GameDispatcher.Dispatcher; Deferred deferred = new Deferred(); if (!IsReloginCapable()) { throw new Exception("Relogin is not capable.No saved refresh token found"); } string authenticationId = InfoResolver.Resolve <BacktoryInfo>().AuthenticationId; string authenticationClientKey = InfoResolver.Resolve <BacktoryInfo>().AuthenticationClientKey; string url = "https://api.backtory.com/auth/login"; ThreadPool.QueueUserWorkItem(state => { try { BacktoryAccessData backtoryAccessData = ServiceLocator.Resolve <IStorageService>().ResolveData <BacktoryAccessData>(); string result = MultiPartCall(url, new Dictionary <string, string> { { "refresh_token", backtoryAccessData.RefreshToken }, }, new Dictionary <string, string> { { "X-Backtory-Authentication-Id", authenticationId }, { "X-Backtory-Authentication-Key", authenticationClientKey }, { "X-Backtory-Authentication-Refresh", "1" } }); LoginResponse loginResponse = JsonConvert.DeserializeObject <LoginResponse>(result); dispatcher.Dispach(() => { if (string.IsNullOrEmpty(loginResponse.AccessToken)) { deferred.Reject(); } else { backtoryAccessData = ServiceLocator.Resolve <IStorageService>().ResolveData <BacktoryAccessData>(); backtoryAccessData.AccessToken = loginResponse.AccessToken; backtoryAccessData.RefreshToken = loginResponse.RefreshToken; backtoryAccessData.TokenType = loginResponse.TokenType; ServiceLocator.Resolve <IStorageService>().UpdateData(backtoryAccessData); deferred.Resolve(); } }); } catch (Exception) { dispatcher.Dispach(() => { deferred.Reject(); }); } }); return(deferred.Promise()); }
public Promise Login(string username, string password) { IDispatcher dispatcher = GameDispatcher.Dispatcher; Deferred deferred = new Deferred(); string authenticationId = InfoResolver.Resolve <BacktoryInfo>().AuthenticationId; string authenticationClientKey = InfoResolver.Resolve <BacktoryInfo>().AuthenticationClientKey; string url = "https://api.backtory.com/auth/login"; ThreadPool.QueueUserWorkItem(state => { try { string result = MultiPartCall(url, new Dictionary <string, string> { { "username", username }, { "password", password } }, new Dictionary <string, string> { { "X-Backtory-Authentication-Id", authenticationId }, { "X-Backtory-Authentication-Key", authenticationClientKey } }); LoginResponse loginResponse = JsonConvert.DeserializeObject <LoginResponse>(result); dispatcher.Dispach(() => { if (string.IsNullOrEmpty(loginResponse.AccessToken)) { deferred.Reject(); } else { BacktoryAccessData backtoryAccessData = ServiceLocator.Resolve <IStorageService>().ResolveData <BacktoryAccessData>() ?? new BacktoryAccessData(); backtoryAccessData.AccessToken = loginResponse.AccessToken; backtoryAccessData.RefreshToken = loginResponse.RefreshToken; backtoryAccessData.TokenType = loginResponse.TokenType; ServiceLocator.Resolve <IStorageService>().UpdateData(backtoryAccessData); deferred.Resolve(); } }); } catch (Exception) { dispatcher.Dispach(() => { deferred.Reject(); }); } }); return(deferred.Promise()); }
public Promise <T, ICallError> Call <T>(string methodName, object requestBody) { IDispatcher dispatcher = GameDispatcher.Dispatcher; Deferred <T, ICallError> deferred = new Deferred <T, ICallError>(); string url = string.Format("https://api.backtory.com/cloud-code/{0}/{1}", InfoResolver.Resolve <BacktoryInfo>().CloudId, methodName); if (!string.IsNullOrEmpty(BacktoryCloudUrl.Url)) { url = new Uri(new Uri(BacktoryCloudUrl.Url), new Uri(string.Format("/{0}", methodName))).ToString(); } string authorization = string.Empty; if (ServiceLocator.Resolve <IStorageService>().ContainsData <BacktoryAccessData>()) { BacktoryAccessData backtoryAccessData = ServiceLocator.Resolve <IStorageService>().ResolveData <BacktoryAccessData>(); authorization = string.Format("{0} {1}", backtoryAccessData.TokenType, backtoryAccessData.AccessToken); } string body = JsonConvert.SerializeObject(requestBody); if (body == "null") { body = "{}"; } ThreadPool.QueueUserWorkItem(state => { try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); webRequest.KeepAlive = true; webRequest.Method = "POST"; webRequest.ContentType = "application/json; charset=utf-8"; if (!string.IsNullOrEmpty(authorization)) { webRequest.Headers.Add("Authorization", authorization); } byte[] resoponse = Encoding.UTF8.GetBytes(body); webRequest.ContentLength = resoponse.Length; using (System.IO.Stream requestStream = webRequest.GetRequestStream()) { requestStream.Write(resoponse, 0, resoponse.Length); } using (WebResponse webResponse = webRequest.GetResponse()) { using (System.IO.Stream responseStream = webResponse.GetResponseStream()) { using (StreamReader reader = new StreamReader(responseStream)) { string readToEnd = reader.ReadToEnd(); dispatcher.Dispach(() => { try { T result = JsonConvert.DeserializeObject <T>(readToEnd); deferred.Resolve(result); } catch (Exception) { deferred.Reject(new BactkoryCallError(HttpStatusCode.Continue, CallErrorType.MethodConversionFailed)); } }); } } } dispatcher.Dispach(() => { deferred.Resolve(default(T)); }); } catch (WebException we) { dispatcher.Dispach(() => { HttpWebResponse httpWebResponse = we.Response as HttpWebResponse; if (httpWebResponse != null) { deferred.Reject(new BactkoryCallError(httpWebResponse.StatusCode, CallErrorType.Other)); } deferred.Reject(new BactkoryCallError(HttpStatusCode.Continue, CallErrorType.Other)); }); } catch (Exception) { deferred.Reject(new BactkoryCallError(HttpStatusCode.Continue, CallErrorType.Other)); } }); return(deferred.Promise()); }