Beispiel #1
0
        public Promise <Result <T, WebError> > Request(params object[] values)
        {
            if (values == null || ParamNames.Length != values.Length)
            {
                throw new WrongNumberOfParamsException(this, Controller);
            }

            int headerCount = data.headers.Match(
                some: h => h.Length,
                none: () => 0
                );

            if (HeaderNames.Length != headerCount)
            {
                throw new WrongNumberOfParamsException(this, Controller);
            }

            data.values = values;

            var responsePromise = new Promise <Result <T, WebError> >();

            Controller.Api.MakeRequest(this, data, responsePromise);

            data = new WebActionData();

            return(responsePromise);
        }
Beispiel #2
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);
            }
        }
Beispiel #3
0
        public static string BuildUrl(IWebAction action, WebActionData actionData)
        {
            var controller = action.Controller;
            var api        = controller.Api;

            var uriBuilder = new StringBuilder();

            uriBuilder.Append(api.url);

            if (!api.url.EndsWith("/"))
            {
                uriBuilder.Append('/');
            }

            uriBuilder.Append(controller.Name);

            if (!string.IsNullOrEmpty(controller.Name))
            {
                uriBuilder.Append('/');
            }

            uriBuilder.Append(action.Name);

            if (!string.IsNullOrEmpty(action.Name))
            {
                uriBuilder.Append('/');
            }

            // Url params
            object[] values = actionData.values;
            for (int i = 0; i < values.Length; i++)
            {
                if (string.IsNullOrEmpty(action.ParamNames[i]))
                {
                    continue;
                }

                string encodedValue = UnityWebRequest.EscapeURL(System.Convert.ToString(values[i]));

                if (!string.IsNullOrEmpty(encodedValue))
                {
                    uriBuilder.Append(encodedValue);
                    uriBuilder.Append('/');
                }
            }

            // Remove last '/'
            uriBuilder.Remove(uriBuilder.Length - 1, 1);

            if (values.Length > 0)
            {
                // Get params
                if (action.Method == WebMethod.GET)
                {
                    uriBuilder.Append('?');

                    for (int i = 0; i < values.Length; i++)
                    {
                        uriBuilder.Append(UnityWebRequest.EscapeURL(action.ParamNames[i]));
                        uriBuilder.Append('=');
                        uriBuilder.Append(UnityWebRequest.EscapeURL(System.Convert.ToString(values[i])));
                        uriBuilder.Append('&');
                    }

                    if (uriBuilder.Length > 0)
                    {
                        uriBuilder.Remove(uriBuilder.Length - 1, 1);
                    }
                }
            }

            return(uriBuilder.ToString());
        }
Beispiel #4
0
        public static Result <UnityWebRequest, WebError> CreateRequest(string url, IWebAction action, WebActionData actionData, IWebSerializer serializer)
        {
            if (action.Method == WebMethod.POST)
            {
                object[] values = actionData.values;

                var form = new Dictionary <string, object>();
                for (int i = 0; i < values.Length; i++)
                {
                    form.Add(action.ParamNames[i], values[i]);
                }

                return(serializer.Serialize(form).Select(postData =>
                {
                    var request = UnityWebRequest.Post(url, postData);
                    serializer.OnBeforeRequest(request);
                    return request;
                }));
            }
            else
            {
                var request = UnityWebRequest.Get(url);
                serializer.OnBeforeRequest(request);
                return(request);
            }
        }
Beispiel #5
0
 internal void MakeRequest <T>(WebAction <T> action, WebActionData actionData, Promise <Result <T, WebError> > responsePromise)
 {
     StartCoroutine(MakeRequestCoroutine <T>(action, actionData, responsePromise));
 }