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); }
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); }
/// <summary> /// Makes a 'GET' request to the server using a URI /// </summary> /// <typeparam name="T">The type of object the response should be deserialized ot</typeparam> /// <returns>An object with response data</returns> private async Task <GitHubResponse <T> > Get <T>(GitHubRequest githubRequest) where T : new() { var url = new StringBuilder().Append(githubRequest.Url); if (githubRequest.Args != null) { url.Append(ToQueryString(ObjectToDictionaryConverter.Convert(githubRequest.Args).ToArray())); } var absoluteUrl = url.ToString(); // If there is no cache, just directly execute and parse. Nothing more if (Cache == null) { using (var request = new HttpRequestMessage(HttpMethod.Get, absoluteUrl)) { using (var requestResponse = await ExecuteRequest(request).ConfigureAwait(false)) { return(await ParseResponse <T>(requestResponse).ConfigureAwait(false)); } } } // Attempt to get the cached response GitHubResponse <T> cachedResponse = null; if (githubRequest.RequestFromCache || githubRequest.CheckIfModified) { try { cachedResponse = Cache.Get <GitHubResponse <T> >(absoluteUrl); if (githubRequest.RequestFromCache && cachedResponse != null) { cachedResponse.WasCached = true; return(cachedResponse); } } catch { } } using (var request = new HttpRequestMessage(HttpMethod.Get, absoluteUrl)) { var etag = (githubRequest.CheckIfModified && cachedResponse != null) ? cachedResponse.ETag : null; if (etag != null) { request.Headers.Add("If-None-Match", string.Format("\"{0}\"", etag)); } using (var response = await ExecuteRequest(request).ConfigureAwait(false)) { var parsedResponse = await ParseResponse <T>(response).ConfigureAwait(false); if (githubRequest.CacheResponse) { Cache.Set(absoluteUrl, parsedResponse); } return(parsedResponse); } } }