public async Task <HttpServiceResult <TResponse> > PutWithErrorResult <TRequest, TResponse>(Uri uri, TRequest resource, Action <HttpRequestMessage> action = null) where TRequest : class where TResponse : class { var request = new HttpRequestMessage(HttpMethod.Put, uri); if (action != null) { action(request); } var content = JsonConvert.SerializeObject(resource); request.Content = new StringContent(content, Encoding.UTF8, "application/json"); var response = await _client.SendAsync(request); var resultSerialized = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { return(HttpServiceResult <TResponse> .Ok(JsonConvert.DeserializeObject <TResponse>(resultSerialized), (int)response.StatusCode)); } var failedJson = JsonConvert.DeserializeObject <ErrorWithErrorCode>(resultSerialized); return(failedJson != null ? HttpServiceResult <TResponse> .Fail(failedJson.ErrorDescription, failedJson.ErrorCode, (int)response.StatusCode) : HttpServiceResult <TResponse> .Fail($"Error occurred while performing put to {uri}: {response} - {resultSerialized}", null, (int)response.StatusCode)); }
public async Task <HttpServiceResult <TResponse> > Put <TResponse>(Uri uri, Action <HttpRequestMessage> action = null) where TResponse : class { var request = new HttpRequestMessage(HttpMethod.Put, uri); if (action != null) { action(request); } var response = await _client.SendAsync(request); var resultSerialized = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { return(HttpServiceResult <TResponse> .Ok(JsonConvert.DeserializeObject <TResponse>(resultSerialized), (int)response.StatusCode)); } return(HttpServiceResult <TResponse> .Fail($"Error occurred while performing put to {uri} : {response}", null, (int)response.StatusCode)); }
public async Task <HttpServiceResult> Put(Uri uri, Action <HttpRequestMessage> action = null) { var request = new HttpRequestMessage(HttpMethod.Put, uri); if (action != null) { action(request); } var response = await _client.SendAsync(request); var resultSerialized = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { return(HttpServiceResult.Fail($"Error occurred while performing put to {uri}: {response} - {resultSerialized}", null, (int)response.StatusCode)); } return(HttpServiceResult.Ok((int)response.StatusCode)); }
public async Task <HttpServiceResult> PutAsJson <T>(Uri uri, T resource, Action <HttpRequestMessage> action = null) where T : class { var request = new HttpRequestMessage(HttpMethod.Put, uri); if (action != null) { action(request); } var content = JsonConvert.SerializeObject(resource); request.Content = new StringContent(content, Encoding.UTF8, "application/json"); var response = await _client.SendAsync(request); var resultSerialized = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { return(HttpServiceResult.Fail($"Error occurred while performing put to {uri}: {response} - {resultSerialized}", null, (int)response.StatusCode)); } return(HttpServiceResult.Ok((int)response.StatusCode)); }
public async Task <HttpServiceResult <TResponse> > PatchWithErrorResult <TRequest, TResponse, TResolver>(Uri uri, TRequest resource, Action <HttpRequestMessage> action = null, TResolver jsonContractResolver = null) where TRequest : class where TResponse : class where TResolver : DefaultContractResolver { string content; if (jsonContractResolver != null) { content = JsonConvert.SerializeObject(resource, new JsonSerializerSettings { ContractResolver = jsonContractResolver }); } else { content = JsonConvert.SerializeObject(resource); } var stringContent = new StringContent(content, Encoding.UTF8, "application/json"); var request = new HttpRequestMessage(new HttpMethod("PATCH"), uri); if (action != null) { action(request); } request.Content = stringContent; var response = await _client.SendAsync(request); var resultSerialized = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { return(HttpServiceResult <TResponse> .Ok(JsonConvert.DeserializeObject <TResponse>(resultSerialized), (int)response.StatusCode)); } var failedJson = JsonConvert.DeserializeObject <ErrorWithErrorCode>(resultSerialized); return(failedJson != null ? HttpServiceResult <TResponse> .Fail(failedJson.ErrorDescription, failedJson.ErrorCode, (int)response.StatusCode) : HttpServiceResult <TResponse> .Fail($"Error occurred while performing patch to {uri}: {response} - {resultSerialized}", null, (int)response.StatusCode)); }
public async Task <HttpServiceResult <T> > PostWithErrorResult <T>(Uri uri, Action <HttpRequestMessage> action = null) where T : class { var request = new HttpRequestMessage(HttpMethod.Post, uri); if (action != null) { action(request); } var response = await _client.SendAsync(request); var result = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { return(HttpServiceResult <T> .Ok(JsonConvert.DeserializeObject <T>(result), (int)response.StatusCode)); } var failedJson = JsonConvert.DeserializeObject <ErrorWithErrorCode>(result); return(failedJson != null ? HttpServiceResult <T> .Fail(failedJson.ErrorDescription, failedJson.ErrorCode, (int)response.StatusCode) : HttpServiceResult <T> .Fail($"Error occurred while performing post to {uri}: {response} - {result}", null, (int)response.StatusCode)); }