Esempio n. 1
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)
        {
            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();
                }

                result.AddProgressCallback((p) =>
                {
                    UnityEngine.Debug.Log("ApiClientProgress: " + p);
                    if (!result.IsCompleted && !result.IsCanceled)
                    {
                        result.SetProgress(p);
                    }
                });

                Observable.FromCoroutine(() => DoRequest(result, request)).Subscribe();
            })
            .Catch(error =>
            {
                result.SetException(error);
            });

            return(result);
        }