Skip to content

Commit

Permalink
Refactored HttpPostAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
wadewegner committed Sep 17, 2014
1 parent 6b43bc9 commit 3877eb0
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions src/CommonLibrariesForNET/ServiceHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,34 +129,21 @@ public async Task<IList<T>> HttpGetAsync<T>(string urlSuffix, string nodeName)
public async Task<T> HttpPostAsync<T>(object inputObject, string urlSuffix)
{
var url = Common.FormatUrl(urlSuffix, _instanceUrl, _apiVersion);
var r = await this.HttpPostAsync<T>(inputObject, new Uri(url));
return r;
}

var json = JsonConvert.SerializeObject(inputObject,
Formatting.None,
public async Task<T> HttpPostAsync<T>(object inputObject, Uri uri)
{
var json = JsonConvert.SerializeObject(inputObject,
Formatting.None,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CreateableContractResolver()
});

var content = new StringContent(json, Encoding.UTF8, "application/json");
var responseMessage = await _httpClient.PostAsync(url, content).ConfigureAwait(false);
var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);

if (responseMessage.IsSuccessStatusCode)
{
var r = JsonConvert.DeserializeObject<T>(response);
return r;
}

var errorResponse = JsonConvert.DeserializeObject<ErrorResponses>(response);
throw new ForceException(errorResponse[0].errorCode, errorResponse[0].message);
}

public async Task<T> HttpPostAsync<T>(object inputObject, Uri uri)
{
//TODO: We don't need the contract resolver here, do we?
var json = JsonConvert.SerializeObject(inputObject, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
var content = new StringContent(json, Encoding.UTF8, "application/json");

var responseMessage = await _httpClient.PostAsync(uri, content);
var response = await responseMessage.Content.ReadAsStringAsync();
Expand Down

0 comments on commit 3877eb0

Please sign in to comment.