Last active
August 29, 2015 14:05
-
-
Save LucasMoffitt/8abf13630558e42faecb to your computer and use it in GitHub Desktop.
Basic Windows 8 / 8.1 Rest Toolkit
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
public class RestToolkit : IDisposable | |
{ | |
private readonly string _endPoint; | |
private readonly HttpClient _client = new HttpClient(); | |
public RestToolkit(string endPoint) | |
{ | |
_endPoint = endPoint; | |
} | |
public async Task<T> Get<T>(string route) | |
{ | |
HttpResponseMessage response; | |
try | |
{ | |
response = await _client.GetAsync(_endPoint + route); | |
} | |
catch (Exception) | |
{ | |
return default(T); | |
} | |
return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync()); | |
} | |
public async Task<bool> Get(string route) | |
{ | |
HttpResponseMessage response; | |
try | |
{ | |
response = await _client.GetAsync(_endPoint + route, HttpCompletionOption.ResponseHeadersRead); | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
return response.IsSuccessStatusCode; | |
} | |
public async Task<bool> Post(Object objectToPost, string route) | |
{ | |
HttpResponseMessage response; | |
try | |
{ | |
response = await _client.PostAsync(_endPoint + route, CreatePostContent(objectToPost)); | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
return response.IsSuccessStatusCode; | |
} | |
public async Task<T> Post<T>(Object objectToPost, string route) | |
{ | |
HttpResponseMessage response; | |
try | |
{ | |
response = await _client.PostAsync(_endPoint + route, CreatePostContent(objectToPost)); | |
} | |
catch (Exception) | |
{ | |
return default(T); | |
} | |
return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync()); | |
} | |
public async Task<bool> Authenticate(string userName, string password, string confirmationRoute) | |
{ | |
try | |
{ | |
var authenticationData = String.Format("{0}:{1}", userName, password); | |
var authHeader = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(authenticationData))); | |
_client.DefaultRequestHeaders.Authorization = authHeader; | |
var connected = await _client.GetAsync(_endPoint + confirmationRoute, HttpCompletionOption.ResponseHeadersRead); | |
if (connected.StatusCode == HttpStatusCode.OK) | |
return true; | |
_client.DefaultRequestHeaders.Authorization = null; | |
return false; | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
} | |
public StringContent CreatePostContent(Object content) | |
{ | |
var jsonContent = JsonConvert.SerializeObject(content, new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.Objects}); | |
var postContent = new StringContent(jsonContent, Encoding.UTF8, "application/json"); | |
return postContent; | |
} | |
public void Dispose() | |
{ | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
protected virtual void Dispose(bool disposing) | |
{ | |
if (!disposing) | |
return; | |
if (_client != null) _client.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment