示例#1
0
        public Task <HTTP.Response> Perform(
            string URL,
            HTTP.Method method         = Method.GET,
            HTTP.Parameters parameters = null,
            HTTP.Headers headers       = null,
            HTTP.Encoder encoder       = null)
        {
            return(Task <HTTP.Response> .Run(async() =>
            {
                var client = new System.Net.Http.HttpClient();
                var request = this.Request(URL, method, parameters, headers, encoder);

                return new HTTP.Response(await client.SendAsync(request));
            }));
        }
示例#2
0
        private System.Net.Http.HttpRequestMessage Request(string URL,
                                                           HTTP.Method method,
                                                           HTTP.Parameters parameters,
                                                           HTTP.Headers headers,
                                                           HTTP.Encoder encoder)
        {
            var request = new HttpRequestMessage();

            request.Method     = method.Method();
            request.RequestUri = this.ApplyURL(URL);

            this.ApplyEncode(request, method, parameters, encoder);
            this.ApplyHeaders(request, headers);

            return(request);
        }
示例#3
0
        private void ApplyEncode(System.Net.Http.HttpRequestMessage request,
                                 HTTP.Method method,
                                 HTTP.Parameters parameters,
                                 HTTP.Encoder encoder)
        {
            HTTP.Encoder requestEncoder = encoder ?? URLEncoder.Instance;

            if ((parameters?.Count ?? 0) != 0 &&
                method == Method.GET)
            {
                requestEncoder = URLEncoder.Instance;
            }

            if (requestEncoder != null &&
                (parameters?.Count ?? 0) != 0)
            {
                requestEncoder.Encode(request, parameters);
            }
        }
示例#4
0
        internal static System.Net.Http.HttpMethod Method(this HTTP.Method m)
        {
            switch (m)
            {
            case HTTP.Method.POST:
                return(HttpMethod.Post);

            case HTTP.Method.PUT:
                return(HttpMethod.Put);

            case HTTP.Method.OPTIONS:
                return(HttpMethod.Options);

            case HTTP.Method.HEAD:
                return(HttpMethod.Head);

            case HTTP.Method.DELETE:
                return(HttpMethod.Delete);

            default:
                return(HttpMethod.Get);
            }
        }
示例#5
0
        private void Api(string query, HTTP.Method method, HTTP.Format format, Dictionary <string, string> args, OKRequestCallback callback, bool useSession = true)
        {
            args.Add(ParamApplicationKey, appKey);
            args.Add(ParamMethod, query);
            args.Add(ParamFormat, format.ToString());
            args.Add(ParamPlatform, GetPlatform().ToUpper());

            // Check if target API requires SdkToken.
            if (OKMethod.RequiresSdkToken(query))
            {
                args.Add(ParamSdkToken, unitySessionKey);
            }

            // Override useSession for some API requests that fail if called within session.
            if (!OKMethod.RequiresSession(query))
            {
                useSession = false;
            }

            string url = useSession ? GetApiUrl(args) : GetApiNoSessionUrl(args);

            new HTTP.Request(url, method, format).Send(request =>
            {
                //check for error
                Hashtable obj = request.response.Object;
                if (obj != null)
                {
                    if (obj.ContainsKey("error_code"))
                    {
                        string errorCode = obj["error_code"].ToString();
                        string errorMsg  = obj["error_msg"].ToString();
                        switch (errorCode)
                        {
                        case "100":
                            if (errorMsg == "PARAM : Missed required parameter: access_token")
                            {
                                Debug.Log("Missing access token - trying to auto refresh session");
                                RefreshAuth(refreshed => {
                                    Debug.Log("REFRESHED: " + refreshed);
                                });
                            }
                            break;

                        case "102":
                            Debug.Log("Session expired - trying to auto refresh session");
                            RefreshAuth(refreshed => {
                                Debug.Log("REFRESHED: " + refreshed);
                            });
                            break;

                        case "103":
                            Debug.Log("Invalid session key - trying to auto refresh session");
                            RefreshAuth(refreshed => {
                                Debug.Log("REFRESHED: " + refreshed);
                            });
                            break;

                        default:
                            Debug.LogWarning(query + " failed -> " + request.response.Error);
                            callback(request.response);
                            break;
                        }
                        return;
                    }
                }

                if (callback != null)
                {
                    callback(request.response);
                }
            });
        }
示例#6
0
 public void Api(string query, HTTP.Method method, Dictionary <string, string> args, OKRequestCallback callback, bool useSession = true)
 {
     Api(query, method, httpFormat, args, callback, useSession);
 }