/// <summary> /// Create a BoxCastResponse based on the given HttpResponseMessage /// </summary> /// <param name="httpResponse"></param> /// <returns></returns> public static async Task <BoxCastResponse <T> > CreateResponse(HttpResponseMessage httpResponse) { var boxCastResponse = new BoxCastResponse <T>(); string json = await httpResponse.Content.ReadAsStringAsync(); //populate consistent response data boxCastResponse.ETag = httpResponse.Headers.ETag?.Tag; boxCastResponse.ResponseCode = httpResponse.StatusCode; boxCastResponse.ResponsePhrase = httpResponse.ReasonPhrase; IEnumerable <string> headerValues = null; httpResponse.Headers.TryGetValues("X-RateLimit-Limit", out headerValues); if (headerValues?.First() != null) { boxCastResponse.RateLimit.Limit = Convert.ToInt32(headerValues.First()); } httpResponse.Headers.TryGetValues("X-RateLimit-Remaining", out headerValues); if (headerValues?.First() != null) { boxCastResponse.RateLimit.Remaining = Convert.ToInt32(headerValues.First()); } httpResponse.Headers.TryGetValues("X-RateLimit-Reset", out headerValues); if (headerValues?.First() != null) { DateTime reset; DateTime.TryParse(headerValues.First(), out reset); boxCastResponse.RateLimit.Reset = reset; } httpResponse.Headers.TryGetValues("X-Pagination", out headerValues); if (headerValues?.First() != null) { string paginationJson = headerValues.First(); boxCastResponse.Pagination = JsonConvert.DeserializeObject <PaginationModel>(paginationJson); } //dump all header data into the headers dictionary. boxCastResponse.Headers = httpResponse.Headers.ToDictionary(x => x.Key, x => x.Value); //deserialize json content boxCastResponse.Content = JsonConvert.DeserializeObject <T>(json); return(boxCastResponse); }
/// <summary> /// Handle responses for all HTTP requests /// </summary> /// <typeparam name="T"></typeparam> /// <param name="httpResponse"></param> /// <returns></returns> private static async Task <BoxCastResponse <T> > handleResponse <T>(HttpResponseMessage httpResponse) { if (httpResponse.IsSuccessStatusCode) { return(await BoxCastResponse <T> .CreateResponse(httpResponse)); } else { BoxCastResponse <ErrorModel> errorResponse = null; try { errorResponse = await BoxCastResponse <ErrorModel> .CreateResponse(httpResponse); } catch { throw new WebException((int)httpResponse.StatusCode + " - " + httpResponse.ReasonPhrase); } throw new BoxCastAPIException(errorResponse); } }
public BoxCastAPIException(BoxCastResponse <ErrorModel> error) { BoxCastError = error; }