Пример #1
0
        private static IAsyncOperation <AuthorizationInfo> DoAuthorizationRequest(string clientId, UnityWebRequest uwr)
        {
            var wrapper = new AsyncCompletionSource <AuthorizationInfo>();

            RequestsUtil.DoRequest <AuthorizationInfo>(uwr)
            .Then(authInfo =>
            {
                authInfo.ClientId = clientId;
                wrapper.SetResult(authInfo);
                return(wrapper);
            })
            .Catch(ex =>
            {
                if (uwr.isHttpError)
                {
                    var apiEx = (ApiException)ex;
                    var msg   = (string)apiEx.ErrorContent;
                    try
                    {
                        var authError = JsonConvert.DeserializeObject <AuthorizationError>(msg);
                        msg           = authError.ErrorDescription;
                    }
                    catch { }
                    wrapper.SetException(new ApiException((int)uwr.responseCode, msg));
                }
                else
                {
                    wrapper.SetException(ex);
                }
            })
            .AddProgressCallback(wrapper.SetProgress);

            return(wrapper);
        }
Пример #2
0
        /// <summary>
        /// Makes the HTTP request (Sync).
        /// </summary>
        /// <param name="path">URL path.</param>
        /// <param name="method">HTTP method.</param>
        /// <param name="queryParams">Query parameters.</param>
        /// <param name="postBody">HTTP body (POST request).</param>
        /// <param name="headerParams">Header parameters.</param>
        /// <param name="formParams">Form parameters.</param>
        /// <param name="fileParams">File parameters.</param>
        /// <param name="authSettings">Authentication settings.</param>
        /// <returns>IAsyncOperation<UnityWebRequest></returns>
        public IAsyncOperation <UnityWebRequest> CallApi(String path, string method, Dictionary <String, String> queryParams, String postBody,
                                                         Dictionary <String, String> headerParams, Dictionary <String, String> formParams,
                                                         Dictionary <String, String> fileParams, String[] authSettings, bool inBackground = false)
        {
            UnityWebRequest request = null;
            var             result  = new AsyncCompletionSource <UnityWebRequest>();

            switch (method)
            {
            case UnityWebRequest.kHttpVerbGET:
                request = UnityWebRequest.Get(BasePath + path);
                break;

            case UnityWebRequest.kHttpVerbPOST:
                request = UnityWebRequest.Post(BasePath + path, formParams);
                break;

            case UnityWebRequest.kHttpVerbPUT:
                request = UnityWebRequest.Put(BasePath + path, postBody);
                break;

            case UnityWebRequest.kHttpVerbDELETE:
                request = UnityWebRequest.Delete(BasePath + path);
                break;

            default:
                throw new ApiException(500, "Method not available: " + method);
            }

            UpdateParamsForAuth(queryParams, headerParams, authSettings, true)
            .Then(() =>
            {
                // add default header, if any
                foreach (var defaultHeader in _defaultHeaderMap)
                {
                    request.SetRequestHeader(defaultHeader.Key, defaultHeader.Value);
                }

                // add header parameter, if any
                foreach (var param in headerParams)
                {
                    request.SetRequestHeader(param.Key, param.Value);
                }

                if (queryParams.Count > 0 || formParams.Count > 0 && method != UnityWebRequest.kHttpVerbPOST)
                {
                    request.url += "?";
                }

                // add query parameter, if any
                if (queryParams.Count > 0)
                {
                    request.url += String.Join("&", queryParams.Select(kv => kv.Key + "=" + kv.Value).ToArray());
                }

                foreach (var param in fileParams)
                {
                    throw new ApiException(500, "Uploading files is not implemented.");
                }

                // add form parameter, if any
                if (formParams.Count > 0 && method != UnityWebRequest.kHttpVerbPOST)
                {
                    request.url += String.Join("&", formParams.Select(kv => kv.Key + "=" + kv.Value).ToArray());
                }

                // add file parameter, if any
                foreach (var param in fileParams)
                {
                    throw new ApiException(500, "Uploading files is not implemented.");
                }

                if (postBody != null)
                {
                    // http body (model) parameter
                    var bodyBytes = System.Text.Encoding.UTF8.GetBytes(postBody);
                    request.SetRequestHeader("Content-Type", "application/json");
                    request.uploadHandler   = new UploadHandlerRaw(bodyBytes);
                    request.downloadHandler = new DownloadHandlerBuffer();
                }

                if (inBackground)
                {
                    RequestsUtil.DoRequestInBackground(request).Wrap(result);
                }
                else
                {
                    RequestsUtil.DoRequest(request).Wrap(result);
                }
            })
            .Catch(error =>
            {
                result.SetException(error);
            });

            return(result);
        }