Ejemplo n.º 1
0
        private static async Task <GitHubResponse> ParseResponse(HttpResponseMessage response)
        {
            var ghr = new GitHubResponse {
                StatusCode = (int)response.StatusCode
            };

            foreach (var h in response.Headers)
            {
                if (h.Key.Equals("X-RateLimit-Limit"))
                {
                    ghr.RateLimitLimit = Convert.ToInt32(h.Value.First());
                }
                else if (h.Key.Equals("X-RateLimit-Remaining"))
                {
                    ghr.RateLimitRemaining = Convert.ToInt32(h.Value.First());
                }
                else if (h.Key.Equals("ETag"))
                {
                    ghr.ETag = h.Value.First().Replace("\"", "");
                }
            }

            var content = await response.Content.ReadAsStringAsync();

            if (response.StatusCode < (HttpStatusCode)200 || response.StatusCode >= (HttpStatusCode)300)
            {
                throw StatusCodeException.FactoryCreate(response, content);
            }

            return(ghr);
        }
Ejemplo n.º 2
0
        private static async Task <GitHubResponse <T> > ParseResponse <T>(HttpResponseMessage response) where T : new()
        {
            var ghr = new GitHubResponse <T> {
                StatusCode = (int)response.StatusCode
            };

            foreach (var h in response.Headers)
            {
                if (h.Key.Equals("X-RateLimit-Limit"))
                {
                    ghr.RateLimitLimit = Convert.ToInt32(h.Value.First());
                }
                else if (h.Key.Equals("X-RateLimit-Remaining"))
                {
                    ghr.RateLimitRemaining = Convert.ToInt32(h.Value.First());
                }
                else if (h.Key.Equals("ETag"))
                {
                    ghr.ETag = h.Value.First().Replace("\"", "").Trim();
                }
                else if (h.Key.Equals("Link"))
                {
                    var s = h.Value.First().Split(',');
                    foreach (var link in s)
                    {
                        var splitted = link.Split(';');
                        var url      = splitted[0].Trim();
                        var what     = splitted[1].Trim();
                        what = what.Substring(5);
                        what = what.Substring(0, what.Length - 1);
                        url  = url.Substring(1);
                        url  = url.Substring(0, url.Length - 1);

                        if (what.Equals("next"))
                        {
                            ghr.More = GitHubRequest.Get <T>(url);
                        }
                    }
                }
            }

            // Booleans have a special definition in the github responses.
            // They typically represent a status code Not Found = false
            // or 204 = true and 205 (reset content)
            if (typeof(T) == typeof(bool) && (ghr.StatusCode == 204 || ghr.StatusCode == 205 || ghr.StatusCode == 404))
            {
                var b = ghr.StatusCode == 204 || ghr.StatusCode == 205;
                ghr.Data = (T)(object)(b);
                return(ghr);
            }

            var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (response.StatusCode < (HttpStatusCode)200 || response.StatusCode >= (HttpStatusCode)300)
            {
                throw StatusCodeException.FactoryCreate(response, content);
            }

            ghr.Data = Serializer.Deserialize <T>(content);

            return(ghr);
        }