Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Created January 10, 2013 23:32
Show Gist options
  • Select an option

  • Save tugberkugurlu/4506711 to your computer and use it in GitHub Desktop.

Select an option

Save tugberkugurlu/4506711 to your computer and use it in GitHub Desktop.
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class HttpResponseMessageExtensions {
internal static async Task<HttpApiResponseMessage<TEntity>> GetHttpApiResponseAsync<TEntity>(this Task<HttpResponseMessage> responseTask) {
HttpResponseMessage response = await responseTask.ConfigureAwait(false);
HttpApiResponseMessage<TEntity> apiResponse = await response.GetHttpApiResponseAsync<TEntity>().ConfigureAwait(false);
return apiResponse;
}
internal static async Task<HttpApiResponseMessage> GetHttpApiResponseAsync(this Task<HttpResponseMessage> responseTask) {
HttpResponseMessage response = await responseTask.ConfigureAwait(false);
var apiResponse = await response.GetHttpApiResponseAsync();
return apiResponse;
}
internal static async Task<HttpApiResponseMessage<TEntity>> GetHttpApiResponseAsync<TEntity>(this HttpResponseMessage response) {
// TODO: We might not get a success status code here
// but it might still be a reasonable response. For example, 400 response
// which possiblly contains a message body.
// Structure the HttpApiResponseMessage and HttpApiResponseMessage<T> that way
// and handle it here accordingly.
if (response.IsSuccessStatusCode) {
TEntity content = await response.Content.ReadAsAsync<TEntity>().ConfigureAwait(false);
return response.GetHttpApiResponse(content);
}
else if (response.StatusCode == HttpStatusCode.BadRequest) {
JToken httpError = await response.Content.ReadAsAsync<JToken>().ConfigureAwait(false);
return response.GetHttpApiResponse<TEntity>(httpError);
}
return response.GetHttpApiResponse<TEntity>();
}
internal static async Task<HttpApiResponseMessage> GetHttpApiResponseAsync(this HttpResponseMessage response) {
if (response.StatusCode == HttpStatusCode.BadRequest) {
JToken httpError = await response.Content.ReadAsAsync<JToken>().ConfigureAwait(false);
return new HttpApiResponseMessage(response, httpError);
}
return new HttpApiResponseMessage(response);
}
internal static HttpApiResponseMessage<TEntity> GetHttpApiResponse<TEntity>(this HttpResponseMessage response) {
return new HttpApiResponseMessage<TEntity>(response);
}
internal static HttpApiResponseMessage<TEntity> GetHttpApiResponse<TEntity>(this HttpResponseMessage response, TEntity entity) {
return new HttpApiResponseMessage<TEntity>(response, entity);
}
internal static HttpApiResponseMessage<TEntity> GetHttpApiResponse<TEntity>(this HttpResponseMessage response, JToken httpError) {
return new HttpApiResponseMessage<TEntity>(response, httpError);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment