Created
January 10, 2013 23:32
-
-
Save tugberkugurlu/4506711 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [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