예제 #1
0
        protected override void ParseResponseHeaders(xNet.HttpResponse response)
        {
            var iterator = response.EnumerateHeaders();

            while (iterator.MoveNext())
            {
                var header = iterator.Current;
                if (String.Compare(header.Key, "X-RateLimit-Limit", true) == 0)
                {
                    XRateLimit = Convert.ToInt32(header.Value);
                }
                else if (String.Compare(header.Key, "X-RateLimit-Reset", true) == 0)
                {
                    XRateLimitReset = Convert.ToInt32(header.Value);
                }
                else if (String.Compare(header.Key, "X-RateLimit-Remaining", true) == 0)
                {
                    XRateLimitRemaining = Convert.ToInt32(header.Value);
                }
                else if (String.Compare(header.Key, "Retry-After", true) == 0)
                {
                    RetryAfterSeconds = Convert.ToInt32(header.Value);
                }
            }
        }
예제 #2
0
        public ApiResult UserQuery(string path, HttpMethod httpMethod, Dictionary <string, string> headers = null, string content = "", bool jsonContent = false)
        {
            xNet.HttpResponse response    = null;
            string            contentType = null;

            path = BaseUri + path;

            if (!(httpMethod == HttpMethod.Get || httpMethod == HttpMethod.Post ||
                  httpMethod == HttpMethod.Delete || httpMethod == HttpMethod.Put))
            {
                throw new ArgumentException("Unsupported http method " + httpMethod);
            }
            if (httpMethod != HttpMethod.Get && content.Length == 0)
            {
                throw new ArgumentException("Empty content.");
            }

            if (headers != null && headers.Count == 0)
            {
                throw new ArgumentException("Empty headers.");
            }

            if (headers != null)
            {
                foreach (var h in headers)
                {
                    _request.AddHeader(h.Key, h.Value);
                }
            }
            if (httpMethod != HttpMethod.Get)
            {
                contentType = jsonContent ? "application/json" : "application/x-www-form-urlencoded";
            }
            lock (_obj)
            {
                try
                {
                    if (httpMethod == HttpMethod.Get)
                    {
                        response = _request.Get(path);
                    }

                    else if (httpMethod == HttpMethod.Post)
                    {
                        response = _request.Post(path, content, contentType);
                    }

                    else if (httpMethod == HttpMethod.Delete)
                    {
                        response = _request.Delete(path, content, contentType);
                    }

                    else if (httpMethod == HttpMethod.Put)
                    {
                        response = _request.Put(path, content, contentType);
                    }
                }
                catch (xNet.HttpException ex)
                {
                    return(new ApiResult((int)ex.HttpStatusCode, ex.InnerMessage));
                }
            }
            content = response.ToString();
            ParseResponseHeaders(response);

            return(new ApiResult((int)response.StatusCode, content));
        }
예제 #3
0
 protected virtual void ParseResponseHeaders(xNet.HttpResponse response)
 {
 }