Skip to content

Instantly share code, notes, and snippets.

@nukedbit
Created July 28, 2014 09:07
Show Gist options
  • Save nukedbit/ec1bb7428d60d651b2b6 to your computer and use it in GitHub Desktop.
Save nukedbit/ec1bb7428d60d651b2b6 to your computer and use it in GitHub Desktop.
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