예제 #1
0
        private static BacktoryResponse <T> RawResponseToBacktoryResponse <T>(IRestResponse response) where T : class
        {
            BacktoryResponse <T> result;

            if (response.ErrorException != null || !response.IsSuccessful())
            {
                if ((int)response.StatusCode == (int)BacktoryHttpStatusCode.Unauthorized)
                {
                    response = Handle401StatusCode(response);
                }
                var    errorResponse = TryToReadErrorBody(response);
                string message       = errorResponse != null ? errorResponse.ErrorDescription : null;
                result = BacktoryResponse <T> .Error((int)response.StatusCode, message ?? response.ErrorMessage);
            }
            else
            {
                T res = response.Content as T;                 // try to cast content to T. Only true when T has defined as string.
                // if res is not null, it is raw response string which we need when response type has defined as string.
                // if is null, we can deserialize the content to T
                result = BacktoryResponse <T> .Success((int)response.StatusCode, res ?? FromJson <T>(response.Content));
            }
            Debug.Log("Receiving response of: " + typeof(T).Name + " with code: " + result.Code + "\ncause: " + response.ErrorMessage);
            Debug.Log(response.ResponseUri);
            return(result);
        }
예제 #2
0
        internal static BacktoryResponse <T> ExecuteRequest <T>(IRestRequest request) where T : class
        {
            var response = RestClient.Execute(request);
            BacktoryResponse <T> result = RawResponseToBacktoryResponse <T>(response);

            return(result);
        }
예제 #3
0
        //internal static BacktoryResponse<T> ExecuteRequest<T>(RestRequest request) where T : class, new()
        //{
        //    var response = RestClient.Execute<T>(request);
        //    BacktoryResponse<T> result = RawResponseToBacktoryResponse(response);
        //    return result;
        //}

        internal static void ExecuteRequestAsync <T>(IRestRequest request, Action <BacktoryResponse <T> > callback) where T : class
        {
            RestClient.ExecuteAsync(request, response =>
            {
                // will be executed in background thread
                BacktoryResponse <T> result = RawResponseToBacktoryResponse <T>(response);

                // avoiding NullReferenceException on requests with null callback like logout
                if (callback != null)
                {
                    BacktoryManager.Instance.Invoke(() => callback(result));
                }
            });
        }