Created
February 1, 2019 21:46
-
-
Save cpoDesign/6bc2ab841e6168beea69f401768a07ca 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
public partial class ApiClient | |
{ | |
private readonly HttpClient _httpClient; | |
private Uri BaseEndpoint { get; set; } | |
public ApiClient(Uri baseEndpoint) | |
{ | |
if (baseEndpoint == null) | |
{ | |
throw new ArgumentNullException("baseEndpoint"); | |
} | |
BaseEndpoint = baseEndpoint; | |
_httpClient = new HttpClient(); | |
} | |
/// <summary> | |
/// Common method for making GET calls | |
/// </summary> | |
private async Task<T> GetAsync<T>(Uri requestUrl) | |
{ | |
addHeaders(); | |
var response = await _httpClient.GetAsync(requestUrl, HttpCompletionOption.ResponseHeadersRead); | |
response.EnsureSuccessStatusCode(); | |
var data = await response.Content.ReadAsStringAsync(); | |
return JsonConvert.DeserializeObject<T>(data); | |
} | |
/// <summary> | |
/// Common method for making POST calls | |
/// </summary> | |
private async Task<Message<T>> PostAsync<T>(Uri requestUrl, T content) | |
{ | |
addHeaders(); | |
var response = await _httpClient.PostAsync(requestUrl.ToString(), CreateHttpContent<T>(content)); | |
response.EnsureSuccessStatusCode(); | |
var data = await response.Content.ReadAsStringAsync(); | |
return JsonConvert.DeserializeObject<Message<T>>(data); | |
} | |
private async Task<Message<T1>> PostAsync<T1, T2>(Uri requestUrl, T2 content) | |
{ | |
addHeaders(); | |
var response = await _httpClient.PostAsync(requestUrl.ToString(), CreateHttpContent<T2>(content)); | |
response.EnsureSuccessStatusCode(); | |
var data = await response.Content.ReadAsStringAsync(); | |
return JsonConvert.DeserializeObject<Message<T1>>(data); | |
} | |
private Uri CreateRequestUri(string relativePath, string queryString = "") | |
{ | |
var endpoint = new Uri(BaseEndpoint, relativePath); | |
var uriBuilder = new UriBuilder(endpoint); | |
uriBuilder.Query = queryString; | |
return uriBuilder.Uri; | |
} | |
private HttpContent CreateHttpContent<T>(T content) | |
{ | |
var json = JsonConvert.SerializeObject(content, MicrosoftDateFormatSettings); | |
return new StringContent(json, Encoding.UTF8, "application/json"); | |
} | |
private static JsonSerializerSettings MicrosoftDateFormatSettings | |
{ | |
get | |
{ | |
return new JsonSerializerSettings | |
{ | |
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat | |
}; | |
} | |
} | |
private void addHeaders() | |
{ | |
_httpClient.DefaultRequestHeaders.Remove("userIP"); | |
_httpClient.DefaultRequestHeaders.Add("userIP", "192.168.1.1"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment