Ejemplo n.º 1
0
        ///// <summary>
        ///// Handles the HTTP response based on <see cref="P:Restsharp.IRestResponse.ErrorException"/>
        ///// and dispatches the result (wrapped in a BacktoryReponse) on the Unity main thread.
        ///// </summary>
        ///// <returns>action able to convert rest sharp responses to backtory responses and dispatching them</returns>
        //internal static Action<IRestResponse<T>> ResponseDispatcher<T>(Action<) where T : class
        //{
        //    return (response) =>
        //    {
        //        var result = response.ErrorException != null ?
        //            BacktoryResponse<T>.error((int)response.StatusCode, response.ErrorException) :
        //            BacktoryResponse<T>.success((int)response.StatusCode, response.Data);
        //        Debug.Log("Receiving login response");

        //        Dispatcher.Instance.Invoke(() => action(result));
        //    };
        //}

        internal static BacktoryResponse <T> Execute <T>(RestRequest request) where T : class, new()
        {
            var response = RestClient.Execute <T>(request);
            BacktoryResponse <T> result = RawResponseToBacktoryResponse(response);

            return(result);
        }
 public static BacktoryResponse <TRANSFORMED> Error <RAW, TRANSFORMED>(BacktoryResponse <RAW> backtoryResponse)
     where TRANSFORMED : class
     where RAW : class
 {
     return(new BacktoryResponse <TRANSFORMED>(backtoryResponse.Code,
                                               backtoryResponse.Message, /*backtoryResponse.Message*/ null, false));
 }
Ejemplo n.º 3
0
        internal static void ExecuteAsync <T>(RestRequest request, Action <BacktoryResponse <T> > callback) where T : class, new()
        {
            RestClient.ExecuteAsync <T>(request, response =>
            {
                // will be executed in background thread
                BacktoryResponse <T> result = RawResponseToBacktoryResponse(response);

                // avoiding NullReferenceException on requests with null callback like logout
                if (callback != null)
                {
                    BacktoryManager.Instance.Invoke(() => callback(result));
                }
            });
        }
Ejemplo n.º 4
0
        private static BacktoryResponse <T> RawResponseToBacktoryResponse <T>(IRestResponse <T> response) where T : class, new()
        {
            BacktoryResponse <T> result;

            if (response.ErrorException != null || !response.IsSuccessful())
            {
                if ((int)response.StatusCode == (int)BacktoryHttpStatusCode.Unauthorized)
                {
                    response = Handle401StatusCode(response);
                }
                result = BacktoryResponse <T> .Error((int)response.StatusCode, response.ErrorMessage);
            }
            else
            {
                result = BacktoryResponse <T> .Success((int)response.StatusCode, response.Data);
            }
            Debug.Log("Receiving response of: " + typeof(T).Name + " with code: " + result.Code + "\ncause: " + response.ErrorMessage);
            Debug.Log(response.ResponseUri);
            return(result);
        }