Пример #1
0
        private IEnumerator MakeRequestCoroutine <T>(WebAction <T> action, WebActionData actionData, Promise <Result <T, WebError> > responsePromise)
        {
            string url = WebApiHelper.BuildUrl(action, actionData);

            if (verboseMode)
            {
                Debug.LogFormat("*[WebApi.Request]* [{0}] \"{1}\"\n{2}", action.Method, url, actionData.values.ToStringFull());
            }

            UnityWebRequest webRequest;

            var httpRequestResult = WebApiHelper.CreateRequest(url, action, actionData, serializer);

            if (!httpRequestResult.Ok.TryGet(out webRequest))
            {
                WebApiHelper.RespondToResult(this, action, httpRequestResult.Select(r => ""), responsePromise);
                yield break;
            }

            yield return(UnityWebRequestHelper.SendWebRequest(webRequest));

            bool success = UnityWebRequestHelper.IsSuccess(webRequest);

            string text;

            if (success)
            {
                text = webRequest.downloadHandler.text;
            }
            else
            {
                text = webRequest.error;
            }

            if (verboseMode)
            {
                Debug.LogFormat("*[WebApi.Response]* [{0}] \"{1}\"\n{2}", action.Method, url, text);
            }

            if (success)
            {
                WebApiHelper.RespondToResult(
                    this,
                    action,
                    new Result <string, WebError>(text),
                    responsePromise);
            }
            else
            {
                WebApiHelper.RespondToResult(
                    this,
                    action,
                    new Result <string, WebError>(new WebError(WebError.Type.Request, text, webRequest.responseCode)),
                    responsePromise);
            }
        }
Пример #2
0
        public static void RespondToResult <T>(WebApi api, WebAction <T> action, Result <string, WebError> result, Promise <Result <T, WebError> > responsePromise)
        {
            responsePromise.Complete(action.ConvertResult(result));

            WebError error;

            if (api.verboseMode && result.Error.TryGet(out error))
            {
                Debug.LogErrorFormat("Response error: \"{0}\"", error);
            }
        }
Пример #3
0
 internal void MakeRequest <T>(WebAction <T> action, WebActionData actionData, Promise <Result <T, WebError> > responsePromise)
 {
     StartCoroutine(MakeRequestCoroutine <T>(action, actionData, responsePromise));
 }