Skip to content

Instantly share code, notes, and snippets.

@mii9000
Last active August 29, 2015 14:06
Show Gist options
  • Save mii9000/25ba1008b090e872f15c to your computer and use it in GitHub Desktop.
Save mii9000/25ba1008b090e872f15c to your computer and use it in GitHub Desktop.
Thin convention based WebAPI repository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Net.Http;
using System.Threading.Tasks;
using System.Net.Http.Headers;
namespace WebApp
{
public class GenericApiRepository : IDisposable
{
private bool disposed = false;
private string _ServerAddress
{
get { return WebConfigurationManager.AppSettings["ApiServer"].ToString(); }
}
private string _ApiUri;
public string ApiUri
{
get { return "api/" + _ApiUri; }
private set { _ApiUri = value; }
}
private HttpClient client = null;
#region ctors
public GenericApiRepository(string apientity)
{
_ApiUri = apientity;
client = GetHttpClient();
}
public GenericApiRepository()
{
client = GetHttpClient();
}
#endregion
#region Public Sync Wrappers
public List<dynamic> GetAll()
{
return GetAll<List<dynamic>>(ApiUri).Result;
}
public List<dynamic> GetAll(string uri)
{
return GetAll<List<dynamic>>("api/" + uri).Result;
}
public dynamic Get(int id)
{
return GetAsync<dynamic>(ApiUri, id).Result;
}
public dynamic Get(string uri, int id)
{
return GetAsync<dynamic>("api/" + uri, id).Result;
}
public dynamic Post(object createObj)
{
return PostAsync<dynamic>(ApiUri, createObj).Result;
}
public dynamic Post(object createObj, string uri)
{
return PostAsync<dynamic>(uri, createObj).Result;
}
public int Put(int id, object updateObj)
{
return PutAsync<dynamic>(ApiUri, id, updateObj).Result;
}
public int Put(int id, object updateObj, string uri)
{
return PutAsync<dynamic>(uri, id, updateObj).Result;
}
public void Delete(int id)
{
DeleteAsync(ApiUri, id);
}
public void Delete(int id, string uri)
{
DeleteAsync(uri, id);
}
#endregion
#region Private Async Workers
private async Task<T> GetAllAsync<T>(string uri)
{
var response = await client.GetAsync(uri).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
return response.Content.ReadAsAsync<T>().Result;
throw new ResponseException(response.StatusCode);
}
private async Task<T> GetAsync<T>(string uri, int id)
{
var response = await client.GetAsync(uri + "/" + id).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
return response.Content.ReadAsAsync<T>().Result;
throw new ResponseException(response.StatusCode);
}
private async Task<T> PostAsync<T>(string uri, object createObj)
{
var response = await client.PostAsJsonAsync(uri, createObj).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsAsync<T>().Result;
}
throw new ResponseException(response.StatusCode);
}
private async Task<T> PutAsync<T>(string uri, int id, object updateObj)
{
var response = await client.PutAsJsonAsync(uri + "/" + id, updateObj).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
return response.Content.ReadAsAsync<T>().Result;
throw new ResponseException(response.StatusCode);
}
private async void DeleteAsync(string uri, int id)
{
var response = await client.DeleteAsync(uri + "/" + id).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
throw new ResponseException(response.StatusCode);
}
#endregion
#region Private Helpers
private HttpClient GetHttpClient()
{
client = new HttpClient();
client.BaseAddress = new Uri(_ServerAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client;
}
#endregion
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!disposed && disposing)
{
if (client != null)
{
var hc = client;
client = null;
hc.Dispose();
}
disposed = true;
}
}
#endregion IDisposable Members
}
public class ResponseException: Exception
{
public HttpStatusCode StatusCode { get; private set; }
public ResponseException(HttpStatusCode code)
{
this.StatusCode = code;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment