Created
July 28, 2014 09:07
-
-
Save nukedbit/ec1bb7428d60d651b2b6 to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json.Linq; | |
namespace AssetVersion.Sdk.HttpClients | |
{ | |
public abstract class HttpClientSyncronous<T> where T : class | |
{ | |
private readonly HttpClient _client; | |
protected HttpClientSyncronous(HttpClient client) | |
{ | |
_client = client; | |
} | |
protected abstract void BuildHeaders(HttpRequestMessage request, IDictionary<String, String> args = null); | |
public virtual T Get(Uri relativeUri, IDictionary<String, String> args = null) | |
{ | |
var request = new HttpRequestMessage(HttpMethod.Get, relativeUri); | |
BuildHeaders(request, args); | |
var response = _client.GetAsync(relativeUri) | |
.ContinueWith(t => | |
{ | |
var message = t.Result; | |
return ProcessResponseMessage(message); | |
}) | |
.Unwrap() | |
.ConfigureAwait(false) | |
.GetAwaiter() | |
.GetResult(); | |
return response; | |
} | |
protected virtual Task<T> ProcessResponseMessage(HttpResponseMessage message) | |
{ | |
var r = message.Content | |
.ReadAsAsync<JObject>() | |
.ContinueWith(t1 => GetResult(t1.Result, message)); | |
return r; | |
} | |
protected abstract T GetResult(JObject jObject, HttpResponseMessage message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment