Skip to content
This repository has been archived by the owner on Jun 2, 2020. It is now read-only.

Refactoring: Rethink async/await and add ConfigureAwait in order to avoid dead-locking. #55

Merged
merged 1 commit into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions Klarna.Rest/Klarna.Rest.Core/Commuication/BaseRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,67 +44,67 @@ protected BaseRestClient(ApiSession apiSession, IJsonSerializer jsonSerializer)
/// <param name="data">The POST data to send</param>
/// <param name="headers">The HTTP headers to send when performing a POST request</param>
/// <returns></returns>
protected async Task Post(string url, object data = null, IDictionary<string, string> headers = null, Ref<HttpResponseMessage> outResponse = null)
protected Task Post(string url, object data = null, IDictionary<string, string> headers = null, Ref<HttpResponseMessage> outResponse = null)
{
await MakeRequest(HttpMethod.Post, url, data, headers, outResponse);
return MakeRequest(HttpMethod.Post, url, data, headers, outResponse);
}

protected async Task<T> Post<T>(string url, object data = null, IDictionary<string, string> headers = null, Ref<HttpResponseMessage> outResponse = null)
{
var result = await MakeRequest(HttpMethod.Post, url, data, headers, outResponse);
return await DeserializeOrDefault<T>(result);
var result = await MakeRequest(HttpMethod.Post, url, data, headers, outResponse).ConfigureAwait(false);
return await DeserializeOrDefault<T>(result).ConfigureAwait(false);
}

protected async Task Patch(string url, object data = null, IDictionary<string, string> headers = null, Ref<HttpResponseMessage> outResponse = null)
protected Task Patch(string url, object data = null, IDictionary<string, string> headers = null, Ref<HttpResponseMessage> outResponse = null)
{
await MakeRequest(new HttpMethod("PATCH"), url, data, headers, outResponse);
return MakeRequest(new HttpMethod("PATCH"), url, data, headers, outResponse);
}

protected async Task Delete(string url, object data = null, IDictionary<string, string> headers = null, Ref<HttpResponseMessage> outResponse = null)
protected Task Delete(string url, object data = null, IDictionary<string, string> headers = null, Ref<HttpResponseMessage> outResponse = null)
{
await MakeRequest(HttpMethod.Delete, url, data, headers, outResponse);
return MakeRequest(HttpMethod.Delete, url, data, headers, outResponse);
}

protected async Task<T> Delete<T>(string url, object data = null, IDictionary<string, string> headers = null, Ref<HttpResponseMessage> outResponse = null)
{
var result = await MakeRequest(HttpMethod.Delete, url, data, headers, outResponse);
return await DeserializeOrDefault<T>(result);
var result = await MakeRequest(HttpMethod.Delete, url, data, headers, outResponse).ConfigureAwait(false);
return await DeserializeOrDefault<T>(result).ConfigureAwait(false);
}

protected async Task<T> Put<T>(string url, object data = null, IDictionary<string, string> headers = null, Ref<HttpResponseMessage> outResponse = null)
{

var result = await MakeRequest(HttpMethod.Put, url, data, headers, outResponse);
return await DeserializeOrDefault<T>(result);
var result = await MakeRequest(HttpMethod.Put, url, data, headers, outResponse).ConfigureAwait(false);
return await DeserializeOrDefault<T>(result).ConfigureAwait(false);
}

protected async Task Put(string url, object data = null, IDictionary<string, string> headers = null, Ref<HttpResponseMessage> outResponse = null)
protected Task Put(string url, object data = null, IDictionary<string, string> headers = null, Ref<HttpResponseMessage> outResponse = null)
{
await MakeRequest(HttpMethod.Put, url, data, headers, outResponse);
return MakeRequest(HttpMethod.Put, url, data, headers, outResponse);
}

protected async Task<T> Get<T>(
string url, IDictionary<string, string> headers = null, Ref<HttpResponseMessage> outResponse = null)
{
var result = await MakeRequest(HttpMethod.Get, url, null, headers, outResponse);
return await DeserializeOrDefault<T>(result);
var result = await MakeRequest(HttpMethod.Get, url, null, headers, outResponse).ConfigureAwait(false);
return await DeserializeOrDefault<T>(result).ConfigureAwait(false);
}

protected async Task Get(
protected Task Get(
string url, IDictionary<string, string> headers = null, Ref<HttpResponseMessage> response = null)
{
await MakeRequest(HttpMethod.Get, url, null, headers, response);
return MakeRequest(HttpMethod.Get, url, null, headers, response);
}

protected async Task<Stream> GetStream(string url)
{
using (var client = GetClient())
{
var result = await client.SendAsync(GetMessage(HttpMethod.Get, url));
var result = await client.SendAsync(GetMessage(HttpMethod.Get, url)).ConfigureAwait(false);

await ThrowIfError(result);
await ThrowIfError(result).ConfigureAwait(false);

return await result.Content.ReadAsStreamAsync();
return await result.Content.ReadAsStreamAsync().ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -184,7 +184,7 @@ private HttpContent GetMessageContent(object data)

private async Task<T> DeserializeOrDefault<T>(HttpResponseMessage result)
{
var content = await result.Content.ReadAsStringAsync();
var content = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
return !string.IsNullOrEmpty(content) ? _jsonSerializer.Deserialize<T>(content) : default(T);
}

Expand All @@ -197,7 +197,7 @@ private static async Task ThrowIfError(HttpResponseMessage result)
{
if (!result.IsSuccessStatusCode)
{
var content = await result.Content.ReadAsStringAsync();
var content = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
var errorMessage = new ErrorMessage();

try
Expand Down
15 changes: 3 additions & 12 deletions Klarna.Rest/Klarna.Rest.Core/Store/CheckoutStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ internal CheckoutStore(ApiSession apiSession, IJsonSerializer jsonSerializer) :
public async Task<CheckoutOrder> CreateOrder(CheckoutOrder order)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri);

var response = await Post<CheckoutOrder>(url, order);

return response;
return await Post<CheckoutOrder>(url, order).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -40,10 +37,7 @@ public async Task<CheckoutOrder> CreateOrder(CheckoutOrder order)
public async Task<CheckoutOrder> GetOrder(string orderId)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, orderId);

var response = await Get<CheckoutOrder>(url);

return response;
return await Get<CheckoutOrder>(url).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -56,10 +50,7 @@ public async Task<CheckoutOrder> GetOrder(string orderId)
public async Task<CheckoutOrder> UpdateOrder(CheckoutOrder order)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, order.OrderId);

var response = await Post<CheckoutOrder>(url, order);

return response;
return await Post<CheckoutOrder>(url, order).ConfigureAwait(false);
}
}
}
14 changes: 4 additions & 10 deletions Klarna.Rest/Klarna.Rest.Core/Store/CustomerTokenStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ public async Task<CustomerTokenDetails> GetCustomerTokenDetails(string customerT
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, customerToken);

var response = await Get<CustomerTokenDetails>(url);

return response;
return await Get<CustomerTokenDetails>(url).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -38,10 +36,7 @@ public async Task<CustomerTokenDetails> GetCustomerTokenDetails(string customerT
public async Task<CustomerTokenCreateOrderResponse> CreateOrder(string customerToken, CustomerTokenOrder order)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{customerToken}/order");

var response = await Post<CustomerTokenCreateOrderResponse>(url, order);

return response;
return await Post<CustomerTokenCreateOrderResponse>(url, order).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -50,11 +45,10 @@ public async Task<CustomerTokenCreateOrderResponse> CreateOrder(string customerT
/// <param name="customerToken">Customer token</param>
/// <param name="update">A <see cref="CustomerTokenStatusUpdateRequest"/> object</param>
/// <returns><see cref="CustomerTokenCreateOrderResponse"/></returns>
public async Task UpdateStatus(string customerToken, CustomerTokenStatusUpdateRequest update)
public Task UpdateStatus(string customerToken, CustomerTokenStatusUpdateRequest update)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{customerToken}/status");

await Patch(url, update);
return Patch(url, update);
}
}
}
15 changes: 4 additions & 11 deletions Klarna.Rest/Klarna.Rest.Core/Store/HostedPaymentPageStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public async Task<HostedPaymentPageCreateSessionResponse> CreateSession(
HostedPaymentPageCreateSessionRequest session)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri);

var response = await Post<HostedPaymentPageCreateSessionResponse>(url, session);

return response;
return await Post<HostedPaymentPageCreateSessionResponse>(url, session).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -40,11 +37,10 @@ public async Task<HostedPaymentPageCreateSessionResponse> CreateSession(
/// <param name="sessionId">HPP session id</param>
/// <param name="distribution">The <see cref="HostedPaymentPageDistributeLink"/> object</param>
/// <returns></returns>
public async Task DistributeLinkToSession(string sessionId, HostedPaymentPageDistributeLink distribution)
public Task DistributeLinkToSession(string sessionId, HostedPaymentPageDistributeLink distribution)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{sessionId}/distribution");

await Post(url);
return Post(url);
}

/// <summary>
Expand All @@ -55,10 +51,7 @@ public async Task DistributeLinkToSession(string sessionId, HostedPaymentPageDis
public async Task<HostedPaymentPageSessionStatus> GetSessionStatus(string sessionId)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{sessionId}/status");

var response = await Get<HostedPaymentPageSessionStatus>(url);

return response;
return await Get<HostedPaymentPageSessionStatus>(url).ConfigureAwait(false);
}
}
}
Loading