예제 #1
0
        private RestResult <T> TryParseJson <T>()
        {
            RestResult <T> result = GetRestResult <T>();

            // try parse an object
            try {
                result.AnObject = JsonUtility.FromJson <T>(Request.downloadHandler.text);
            } catch (Exception e) {
                result.IsError      = true;
                result.ErrorMessage = "Failed to parse object of type: " + typeof(T).ToString() + " Exception message: " + e.Message;
            }
            return(result);
        }
예제 #2
0
        private RestResult <N> TryParseJsonNestedArray <T, N>(string namedArray) where N : INestedResults <T>, new()
        {
            RestResult <N> result = GetRestResult <N>();

            // try parse an object
            try {
                result.AnObject = JsonHelper.FromJsonNestedArray <T, N>(Request.downloadHandler.text, namedArray); //JsonUtility.FromJson<N>(request.downloadHandler.text);
            } catch (Exception e) {
                result.IsError      = true;
                result.ErrorMessage = "Failed to parse object of type: " + typeof(N).ToString() + " Exception message: " + e.Message;
            }
            return(result);
        }
예제 #3
0
        /// <summary>
        /// Shared method to return response result whether an object or array of objects
        /// </summary>
        private RestResult <T> TryParseJsonArray <T>()
        {
            RestResult <T> result = GetRestResult <T>();

            // try parse an array of objects
            try {
                result.AnArrayOfObjects = JsonHelper.FromJsonArray <T>(Request.downloadHandler.text);
            } catch (Exception e) {
                result.IsError      = true;
                result.ErrorMessage = "Failed to parse an array of objects of type: " + typeof(T).ToString() + " Exception message: " + e.Message;
            }
            return(result);
        }
예제 #4
0
        /// <summary>
        /// To be used with a callback which passes the response with result including status success or error code, request url and any body text.
        /// </summary>
        /// <param name="callback">Callback.</param>
        public void Result(Action <RestResponse> callback = null)
        {
            RestResult result = GetRestResult(false);

            if (result.IsError)
            {
                Debug.LogWarning("Response error status:" + result.StatusCode + " code:" + Request.responseCode + " error:" + result.ErrorMessage + " request url:" + Request.url);
                callback(new RestResponse(result.ErrorMessage, result.StatusCode, Request.url, Request.downloadHandler.text));
            }
            else
            {
                callback(new RestResponse(result.StatusCode, Request.url, Request.downloadHandler.text));
            }
        }
예제 #5
0
        /// Work-around for nested array
        public void ParseJsonNestedArray <T, N>(string namedArray, Action <IRestResponse <N> > callback = null) where N : INestedResults <T>, new()
        {
            RestResult <N> result = TryParseJsonNestedArray <T, N>(namedArray);

            if (result.IsError)
            {
                Debug.LogWarning("Response error status:" + result.StatusCode + " code:" + Request.responseCode + " error:" + result.ErrorMessage + " request url:" + Request.url);
                callback(new RestResponse <N>(result.ErrorMessage, result.StatusCode, Request.url, Request.downloadHandler.text));
            }
            else
            {
                callback(new RestResponse <N>(result.StatusCode, Request.url, Request.downloadHandler.text, result.AnObject));
            }
            this.Dispose();
        }
예제 #6
0
        public void ParseXML <T>(Action <IRestResponse <T> > callback = null) where T : class
        {
            RestResult <T> result = TrySerializeXml <T>();

            if (result.IsError)
            {
                Debug.LogWarning("Response error status:" + result.StatusCode + " code:" + Request.responseCode + " error:" + result.ErrorMessage + " request url:" + Request.url);
                callback(new RestResponse <T>(result.ErrorMessage, result.StatusCode, Request.url, Request.downloadHandler.text));
            }
            else
            {
                callback(new RestResponse <T>(result.StatusCode, Request.url, Request.downloadHandler.text, result.AnObject));
            }
            this.Dispose();
        }
예제 #7
0
        private RestResult GetRestResult(bool expectedBodyContent = true)
        {
            HttpStatusCode statusCode = (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), Request.responseCode.ToString());
            RestResult     result     = new RestResult(statusCode);

            if (result.IsError)
            {
                result.ErrorMessage = "Response failed with status: " + statusCode.ToString();
                return(result);
            }

            if (expectedBodyContent && string.IsNullOrEmpty(Request.downloadHandler.text))
            {
                result.IsError      = true;
                result.ErrorMessage = "Response has empty body";
                return(result);
            }
            return(result);
        }
예제 #8
0
        /// <summary>
        /// Return response body as bytes.
        /// </summary>
        /// <param name="callback">Callback with response type: IRestResponse<byte[]></param>
        public IRestResponse <byte[]> GetBytes(Action <IRestResponse <byte[]> > callback = null)
        {
            RestResult            result = GetRestResult(false);
            RestResponse <byte[]> response;

            if (result.IsError)
            {
                response = new RestResponse <byte[]>(result.ErrorMessage, result.StatusCode, Request.url, Request.downloadHandler.text);
            }
            else
            {
                response = new RestResponse <byte[]>(result.StatusCode, Request.url, null, Request.downloadHandler.data);
            }
            if (callback != null)
            {
                callback(response);
            }
            this.Dispose();
            return(response);
        }
예제 #9
0
        /// <summary>
        /// Return response body as plain text.
        /// </summary>
        /// <param name="callback">Callback with response type: IRestResponse<string></param>
        public IRestResponse <T> GetText <T>(Action <IRestResponse <T> > callback = null)
        {
            RestResult <string> result = GetRestResult <string>();
            RestResponse <T>    response;

            if (result.IsError)
            {
                response = new RestResponse <T>(result.ErrorMessage, result.StatusCode, Request.url, Request.downloadHandler.text);
            }
            else
            {
                response = new RestResponse <T>(result.StatusCode, Request.url, Request.downloadHandler.text);
            }
            if (callback != null)
            {
                callback(response);
            }
            this.Dispose();
            return(response);
        }
예제 #10
0
        public IRestResponse <AssetBundle> GetAssetBundle(Action <IRestResponse <AssetBundle> > callback = null)
        {
            RestResult result = GetRestResult(false);
            RestResponse <AssetBundle> response;

            if (result.IsError)
            {
                response = new RestResponse <AssetBundle>(result.ErrorMessage, result.StatusCode, Request.url, Request.downloadHandler.text);
            }
            else
            {
                AssetBundle assetBundle = ((DownloadHandlerAssetBundle)Request.downloadHandler).assetBundle;
                response = new RestResponse <AssetBundle>(result.StatusCode, Request.url, null, assetBundle);
            }
            if (callback != null)
            {
                callback(response);
            }
            this.Dispose();
            return(response);
        }
예제 #11
0
        public IRestResponse <AudioClip> GetAudioClip(Action <IRestResponse <AudioClip> > callback = null)
        {
            RestResult result = GetRestResult(false);
            RestResponse <AudioClip> response;

            if (result.IsError)
            {
                response = new RestResponse <AudioClip>(result.ErrorMessage, result.StatusCode, Request.url, Request.downloadHandler.text);
            }
            else
            {
                AudioClip audioClip = ((DownloadHandlerAudioClip)Request.downloadHandler).audioClip;
                response = new RestResponse <AudioClip>(result.StatusCode, Request.url, null, audioClip);
            }
            if (callback != null)
            {
                callback(response);
            }
            this.Dispose();
            return(response);
        }
예제 #12
0
        public RestResponse GetText(Action <RestResponse> callback = null, bool expectedBodyContent = true)
        {
            RestResult   result = GetRestResult(expectedBodyContent);
            RestResponse response;

            if (result.IsError)
            {
                Debug.LogWarning("Response error status:" + result.StatusCode + " code:" + Request.responseCode + " error:" + result.ErrorMessage + " request url:" + Request.url);
                response = new RestResponse(result.ErrorMessage, result.StatusCode, Request.url, Request.downloadHandler.text);
            }
            else
            {
                response = new RestResponse(result.StatusCode, Request.url, Request.downloadHandler.text);
            }
            if (callback != null)
            {
                callback(response);
            }
            this.Dispose();
            return(response);
        }
예제 #13
0
        public RestResponse <T> ParseXML <T>(Action <IRestResponse <T> > callback = null)
        {
            RestResult <T>   result = TrySerializeXml <T>();
            RestResponse <T> response;

            if (result.IsError)
            {
                Debug.LogWarning("Response error status:" + result.StatusCode + " code:" + Request.responseCode + " error:" + result.ErrorMessage + " request url:" + Request.url);
                response = new RestResponse <T>(result.ErrorMessage, result.StatusCode, Request.url, Request.downloadHandler.text);
            }
            else
            {
                response = new RestResponse <T>(result.StatusCode, Request.url, Request.downloadHandler.text, result.AnObject);
            }
            if (callback != null)
            {
                callback(response);
            }
            this.Dispose();
            return(response);
        }
예제 #14
0
        private RestResult <T> TrySerializeXml <T>()
        {
            RestResult <T> result = GetRestResult <T>();

            // return early if there was a status / data error other than Forbidden
            if (result.IsError && result.StatusCode == HttpStatusCode.Forbidden)
            {
                Debug.LogWarning("Authentication Failed: " + Request.downloadHandler.text);
                return(result);
            }
            else if (result.IsError)
            {
                return(result);
            }
            // otherwise try and serialize XML response text to an object
            try {
                result.AnObject = XmlHelper.FromXml <T>(Request.downloadHandler.text);
            } catch (Exception e) {
                result.IsError      = true;
                result.ErrorMessage = "Failed to parse object of type: " + typeof(T).ToString() + " Exception message: " + e.Message;
            }
            return(result);
        }