From 6d9af5b610c463ef2f8dc69b0644fbf2b07a92b8 Mon Sep 17 00:00:00 2001 From: Alexander Zinovev Date: Mon, 5 Aug 2019 11:38:38 +0200 Subject: [PATCH] Refactoring: Rethink async/await and add ConfigureAwait in order to avoid dead-locking. --- .../Commuication/BaseRestClient.cs | 46 +++++------ .../Klarna.Rest.Core/Store/CheckoutStore.cs | 15 +--- .../Store/CustomerTokenStore.cs | 14 +--- .../Store/HostedPaymentPageStore.cs | 15 +--- .../Store/OrderManagementStore.cs | 81 +++++++----------- .../Klarna.Rest.Core/Store/PaymentStore.cs | 30 ++----- .../Klarna.Rest.Core/Store/SettlementStore.cs | 82 ++++++++----------- .../Store/VirtualCardServiceStore.cs | 15 +--- 8 files changed, 108 insertions(+), 190 deletions(-) diff --git a/Klarna.Rest/Klarna.Rest.Core/Commuication/BaseRestClient.cs b/Klarna.Rest/Klarna.Rest.Core/Commuication/BaseRestClient.cs index 0827576..77bf63d 100644 --- a/Klarna.Rest/Klarna.Rest.Core/Commuication/BaseRestClient.cs +++ b/Klarna.Rest/Klarna.Rest.Core/Commuication/BaseRestClient.cs @@ -44,67 +44,67 @@ protected BaseRestClient(ApiSession apiSession, IJsonSerializer jsonSerializer) /// The POST data to send /// The HTTP headers to send when performing a POST request /// - protected async Task Post(string url, object data = null, IDictionary headers = null, Ref outResponse = null) + protected Task Post(string url, object data = null, IDictionary headers = null, Ref outResponse = null) { - await MakeRequest(HttpMethod.Post, url, data, headers, outResponse); + return MakeRequest(HttpMethod.Post, url, data, headers, outResponse); } protected async Task Post(string url, object data = null, IDictionary headers = null, Ref outResponse = null) { - var result = await MakeRequest(HttpMethod.Post, url, data, headers, outResponse); - return await DeserializeOrDefault(result); + var result = await MakeRequest(HttpMethod.Post, url, data, headers, outResponse).ConfigureAwait(false); + return await DeserializeOrDefault(result).ConfigureAwait(false); } - protected async Task Patch(string url, object data = null, IDictionary headers = null, Ref outResponse = null) + protected Task Patch(string url, object data = null, IDictionary headers = null, Ref 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 headers = null, Ref outResponse = null) + protected Task Delete(string url, object data = null, IDictionary headers = null, Ref outResponse = null) { - await MakeRequest(HttpMethod.Delete, url, data, headers, outResponse); + return MakeRequest(HttpMethod.Delete, url, data, headers, outResponse); } protected async Task Delete(string url, object data = null, IDictionary headers = null, Ref outResponse = null) { - var result = await MakeRequest(HttpMethod.Delete, url, data, headers, outResponse); - return await DeserializeOrDefault(result); + var result = await MakeRequest(HttpMethod.Delete, url, data, headers, outResponse).ConfigureAwait(false); + return await DeserializeOrDefault(result).ConfigureAwait(false); } protected async Task Put(string url, object data = null, IDictionary headers = null, Ref outResponse = null) { - var result = await MakeRequest(HttpMethod.Put, url, data, headers, outResponse); - return await DeserializeOrDefault(result); + var result = await MakeRequest(HttpMethod.Put, url, data, headers, outResponse).ConfigureAwait(false); + return await DeserializeOrDefault(result).ConfigureAwait(false); } - protected async Task Put(string url, object data = null, IDictionary headers = null, Ref outResponse = null) + protected Task Put(string url, object data = null, IDictionary headers = null, Ref outResponse = null) { - await MakeRequest(HttpMethod.Put, url, data, headers, outResponse); + return MakeRequest(HttpMethod.Put, url, data, headers, outResponse); } protected async Task Get( string url, IDictionary headers = null, Ref outResponse = null) { - var result = await MakeRequest(HttpMethod.Get, url, null, headers, outResponse); - return await DeserializeOrDefault(result); + var result = await MakeRequest(HttpMethod.Get, url, null, headers, outResponse).ConfigureAwait(false); + return await DeserializeOrDefault(result).ConfigureAwait(false); } - protected async Task Get( + protected Task Get( string url, IDictionary headers = null, Ref response = null) { - await MakeRequest(HttpMethod.Get, url, null, headers, response); + return MakeRequest(HttpMethod.Get, url, null, headers, response); } protected async Task 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); } } @@ -184,7 +184,7 @@ private HttpContent GetMessageContent(object data) private async Task DeserializeOrDefault(HttpResponseMessage result) { - var content = await result.Content.ReadAsStringAsync(); + var content = await result.Content.ReadAsStringAsync().ConfigureAwait(false); return !string.IsNullOrEmpty(content) ? _jsonSerializer.Deserialize(content) : default(T); } @@ -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 diff --git a/Klarna.Rest/Klarna.Rest.Core/Store/CheckoutStore.cs b/Klarna.Rest/Klarna.Rest.Core/Store/CheckoutStore.cs index 786d9b1..e6bdbf5 100644 --- a/Klarna.Rest/Klarna.Rest.Core/Store/CheckoutStore.cs +++ b/Klarna.Rest/Klarna.Rest.Core/Store/CheckoutStore.cs @@ -24,10 +24,7 @@ internal CheckoutStore(ApiSession apiSession, IJsonSerializer jsonSerializer) : public async Task CreateOrder(CheckoutOrder order) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri); - - var response = await Post(url, order); - - return response; + return await Post(url, order).ConfigureAwait(false); } /// @@ -40,10 +37,7 @@ public async Task CreateOrder(CheckoutOrder order) public async Task GetOrder(string orderId) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, orderId); - - var response = await Get(url); - - return response; + return await Get(url).ConfigureAwait(false); } /// @@ -56,10 +50,7 @@ public async Task GetOrder(string orderId) public async Task UpdateOrder(CheckoutOrder order) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, order.OrderId); - - var response = await Post(url, order); - - return response; + return await Post(url, order).ConfigureAwait(false); } } } diff --git a/Klarna.Rest/Klarna.Rest.Core/Store/CustomerTokenStore.cs b/Klarna.Rest/Klarna.Rest.Core/Store/CustomerTokenStore.cs index f2a9171..e6432a2 100644 --- a/Klarna.Rest/Klarna.Rest.Core/Store/CustomerTokenStore.cs +++ b/Klarna.Rest/Klarna.Rest.Core/Store/CustomerTokenStore.cs @@ -24,9 +24,7 @@ public async Task GetCustomerTokenDetails(string customerT { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, customerToken); - var response = await Get(url); - - return response; + return await Get(url).ConfigureAwait(false); } /// @@ -38,10 +36,7 @@ public async Task GetCustomerTokenDetails(string customerT public async Task CreateOrder(string customerToken, CustomerTokenOrder order) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{customerToken}/order"); - - var response = await Post(url, order); - - return response; + return await Post(url, order).ConfigureAwait(false); } /// @@ -50,11 +45,10 @@ public async Task CreateOrder(string customerT /// Customer token /// A object /// - 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); } } } diff --git a/Klarna.Rest/Klarna.Rest.Core/Store/HostedPaymentPageStore.cs b/Klarna.Rest/Klarna.Rest.Core/Store/HostedPaymentPageStore.cs index d9b5670..059c416 100644 --- a/Klarna.Rest/Klarna.Rest.Core/Store/HostedPaymentPageStore.cs +++ b/Klarna.Rest/Klarna.Rest.Core/Store/HostedPaymentPageStore.cs @@ -28,10 +28,7 @@ public async Task CreateSession( HostedPaymentPageCreateSessionRequest session) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri); - - var response = await Post(url, session); - - return response; + return await Post(url, session).ConfigureAwait(false); } /// @@ -40,11 +37,10 @@ public async Task CreateSession( /// HPP session id /// The object /// - 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); } /// @@ -55,10 +51,7 @@ public async Task DistributeLinkToSession(string sessionId, HostedPaymentPageDis public async Task GetSessionStatus(string sessionId) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{sessionId}/status"); - - var response = await Get(url); - - return response; + return await Get(url).ConfigureAwait(false); } } } diff --git a/Klarna.Rest/Klarna.Rest.Core/Store/OrderManagementStore.cs b/Klarna.Rest/Klarna.Rest.Core/Store/OrderManagementStore.cs index 5085b6c..994f350 100644 --- a/Klarna.Rest/Klarna.Rest.Core/Store/OrderManagementStore.cs +++ b/Klarna.Rest/Klarna.Rest.Core/Store/OrderManagementStore.cs @@ -23,11 +23,10 @@ internal OrderManagementStore(ApiSession apiSession, IJsonSerializer jsonSeriali /// /// Id of order to release /// - public async Task ReleaseRemainingAuthorization(string orderId) + public Task ReleaseRemainingAuthorization(string orderId) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/release-remaining_authorization"); - - await Post(url); + return Post(url); } /// @@ -36,11 +35,10 @@ public async Task ReleaseRemainingAuthorization(string orderId) /// /// Id of order to extend /// - public async Task ExtendAuthorizationTime(string orderId) + public Task ExtendAuthorizationTime(string orderId) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/extend-authorization-time"); - - await Post(url); + return Post(url); } /// @@ -50,11 +48,10 @@ public async Task ExtendAuthorizationTime(string orderId) /// Id of order to update /// The object /// - public async Task UpdateCustomerAddresses(string orderId, OrderManagementCustomerAddresses customerAddresses) + public Task UpdateCustomerAddresses(string orderId, OrderManagementCustomerAddresses customerAddresses) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/customer-details"); - - await Patch(url, customerAddresses); + return Patch(url, customerAddresses); } /// @@ -63,11 +60,10 @@ public async Task UpdateCustomerAddresses(string orderId, OrderManagementCustome /// /// Id of order to cancel /// - public async Task CancelOrder(string orderId) + public Task CancelOrder(string orderId) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/cancel"); - - await Post(url); + return Post(url); } /// @@ -77,11 +73,10 @@ public async Task CancelOrder(string orderId) /// Id of order to update /// The object /// - public async Task UpdateMerchantReferences(string orderId, OrderManagementMerchantReferences merchantReferences) + public Task UpdateMerchantReferences(string orderId, OrderManagementMerchantReferences merchantReferences) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/merchant-references"); - - await Patch(url, merchantReferences); + return Patch(url, merchantReferences); } /// @@ -90,11 +85,10 @@ public async Task UpdateMerchantReferences(string orderId, OrderManagementMercha /// /// Id of order to acknowledge /// - public async Task AcknowledgeOrder(string orderId) + public Task AcknowledgeOrder(string orderId) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/acknowledge"); - - await Post(url); + return Post(url); } /// @@ -106,10 +100,7 @@ public async Task AcknowledgeOrder(string orderId) public async Task GetOrder(string orderId) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}"); - - var response = await Get(url); - - return response; + return await Get(url).ConfigureAwait(false); } /// @@ -119,11 +110,10 @@ public async Task GetOrder(string orderId) /// Id of order to update /// The object /// - public async Task SetNewOrderAmountAndOrderLines(string orderId, OrderManagementSetNewOrderAmountAndLines newOrderAmountAndLines) + public Task SetNewOrderAmountAndOrderLines(string orderId, OrderManagementSetNewOrderAmountAndLines newOrderAmountAndLines) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/authorization"); - - await Patch(url, newOrderAmountAndLines); + return Patch(url, newOrderAmountAndLines); } /// @@ -136,10 +126,7 @@ public async Task SetNewOrderAmountAndOrderLines(string orderId, OrderManagement public async Task GetCapture(string orderId, string captureId) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures/{captureId}"); - - var response = await Get(url); - - return response; + return await Get(url); } /// @@ -149,11 +136,10 @@ public async Task GetCapture(string orderId, string capt /// Id of order that contains the capture /// Id of capture to resend /// - public async Task TriggerResendOfCustomerCommunication(string orderId, string captureId) + public Task TriggerResendOfCustomerCommunication(string orderId, string captureId) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures/{captureId}/trigger-send-out"); - - await Post(url); + return Post(url); } /// @@ -165,10 +151,7 @@ public async Task TriggerResendOfCustomerCommunication(string orderId, string ca public async Task> GetCapturesForOrder(string orderId) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures"); - - var response = await Get>(url); - - return response; + return await Get>(url); } /// @@ -178,11 +161,10 @@ public async Task> GetCapturesForOrder(strin /// Id of order to create capture /// The object /// Object of - public async Task CreateCapture(string orderId, OrderManagementCreateCapture capture) + public Task CreateCapture(string orderId, OrderManagementCreateCapture capture) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures"); - - await Post(url, capture); + return Post(url, capture); } /// @@ -197,14 +179,14 @@ public async Task CreateAndFetchCapture(string orderId, var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures"); var response = new Ref(); - await Post(url, capture, null, response); + await Post(url, capture, null, response).ConfigureAwait(false); var headers = response.Value.Headers; url = headers.Location.ToString(); if (!string.IsNullOrEmpty(url)) { - return await Get(url); + return await Get(url).ConfigureAwait(false); } return default(OrderManagementCapture); @@ -218,11 +200,10 @@ public async Task CreateAndFetchCapture(string orderId, /// Id of capture to add shipping info /// The object /// - public async Task AddShippingInfoToCapture(string orderId, string captureId, OrderManagementAddShippingInfo shippingInfo) + public Task AddShippingInfoToCapture(string orderId, string captureId, OrderManagementAddShippingInfo shippingInfo) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures/{captureId}/shipping-info"); - - await Post(url, shippingInfo); + return Post(url, shippingInfo); } /// @@ -232,11 +213,10 @@ public async Task AddShippingInfoToCapture(string orderId, string captureId, Ord /// Id of order to create a refund /// The object /// - public async Task CreateRefund(string orderId, OrderManagementRefund refund) + public Task CreateRefund(string orderId, OrderManagementRefund refund) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/refunds"); - - await Post(url, refund); + return Post(url, refund); } /// @@ -251,14 +231,14 @@ public async Task CreateAndFetchRefund(string var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/refunds"); var response = new Ref(); - await Post(url, refund, null, response); + await Post(url, refund, null, response).ConfigureAwait(false); var headers = response.Value.Headers; url = headers.Location.ToString(); if (!string.IsNullOrEmpty(url)) { - return await Get(url); + return await Get(url).ConfigureAwait(false); } return default(OrderManagementGetRefundResponse); @@ -274,8 +254,7 @@ public async Task CreateAndFetchRefund(string public async Task GetRefundForOrder(string orderId, string refundId) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/refunds/{refundId}"); - - return await Get(url); + return await Get(url).ConfigureAwait(false); } } } \ No newline at end of file diff --git a/Klarna.Rest/Klarna.Rest.Core/Store/PaymentStore.cs b/Klarna.Rest/Klarna.Rest.Core/Store/PaymentStore.cs index 2d7c7e0..da23618 100644 --- a/Klarna.Rest/Klarna.Rest.Core/Store/PaymentStore.cs +++ b/Klarna.Rest/Klarna.Rest.Core/Store/PaymentStore.cs @@ -24,10 +24,7 @@ internal PaymentStore(ApiSession apiSession, IJsonSerializer jsonSerializer) : b public async Task CreateCreditSession(PaymentCreditSession creditSession) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri); - - var response = await Post(url, creditSession); - - return response; + return await Post(url, creditSession).ConfigureAwait(false); } /// @@ -39,10 +36,7 @@ public async Task CreateCreditSession(PaymentCredi public async Task GetCreditSession(string sessionId) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, sessionId); - - var response = await Get(url); - - return response; + return await Get(url).ConfigureAwait(false); } /// @@ -52,11 +46,10 @@ public async Task GetCreditSession(string sessionId) /// Id of the credit session to update /// The object /// - public async Task UpdateCreditSession(string sessionId, PaymentCreditSession creditSession) + public Task UpdateCreditSession(string sessionId, PaymentCreditSession creditSession) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, sessionId); - - await Post(url, creditSession); + return Post(url, creditSession); } /// @@ -65,11 +58,10 @@ public async Task UpdateCreditSession(string sessionId, PaymentCreditSession cre /// /// Authorization token from JS client /// - public async Task CancelAuthorization(string authorizationToken) + public Task CancelAuthorization(string authorizationToken) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllers.PaymentAuthorizations, authorizationToken); - - await Delete(url); + return Delete(url); } /// @@ -82,10 +74,7 @@ public async Task CancelAuthorization(string authorizationToken) public async Task GenerateConsumerToken(string authorizationToken, PaymentGenerateConsumerTokenRequest consumerTokenRequest) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllers.PaymentAuthorizations, $"{authorizationToken}/customer-token"); - - var response = await Post(url, consumerTokenRequest); - - return response; + return await Post(url, consumerTokenRequest).ConfigureAwait(false); } /// @@ -98,10 +87,7 @@ public async Task GenerateConsumerToken(st public async Task CreateOrder(string authorizationToken, PaymentOrder order) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllers.PaymentAuthorizations, $"{authorizationToken}/order"); - - var response = await Post(url, order); - - return response; + return await Post(url, order).ConfigureAwait(false); } } } \ No newline at end of file diff --git a/Klarna.Rest/Klarna.Rest.Core/Store/SettlementStore.cs b/Klarna.Rest/Klarna.Rest.Core/Store/SettlementStore.cs index e5dbd4d..671a328 100644 --- a/Klarna.Rest/Klarna.Rest.Core/Store/SettlementStore.cs +++ b/Klarna.Rest/Klarna.Rest.Core/Store/SettlementStore.cs @@ -37,9 +37,7 @@ public async Task> GetPayoutsSummary(strin }; var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, "summary", nvm); - var response = await Get>(url); - - return response; + return await Get>(url).ConfigureAwait(false); } /// @@ -50,10 +48,7 @@ public async Task> GetPayoutsSummary(strin public async Task GetPayout(string paymentReference) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, paymentReference); - - var response = await Get(url); - - return response; + return await Get(url).ConfigureAwait(false); } /// @@ -68,37 +63,34 @@ public async Task GetPayout(string paymentReference) public async Task GetAllPayouts(string startDate = "", string endDate = "", string currencyCode = "", int size = 0, int offset = 0) { - var nvm = new NameValueCollection(); + var parameters = new NameValueCollection(); if (!string.IsNullOrEmpty(startDate)) { - nvm.Add("start_date", startDate); + parameters.Add("start_date", startDate); } if (!string.IsNullOrEmpty(endDate)) { - nvm.Add("end_date", endDate); + parameters.Add("end_date", endDate); } if (!string.IsNullOrEmpty(currencyCode)) { - nvm.Add("currency_code", currencyCode); + parameters.Add("currency_code", currencyCode); } if (size > 0) { - nvm.Add("size", size.ToString("F0")); + parameters.Add("size", size.ToString("F0")); } if (offset > 0) { - nvm.Add("offset", offset.ToString("F0")); + parameters.Add("offset", offset.ToString("F0")); } - var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, null, nvm); - - var response = await Get(url); - - return response; + var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, null, parameters); + return await Get(url).ConfigureAwait(false); } /// @@ -112,32 +104,29 @@ public async Task GetAllPayouts(string startDa public async Task GetTransactions(string paymentReference = "", string orderId = "", int size = 0, int offset = 0) { - var nvm = new NameValueCollection(); + var parameters = new NameValueCollection(); if (!string.IsNullOrEmpty(paymentReference)) { - nvm.Add("payment_reference", paymentReference); + parameters.Add("payment_reference", paymentReference); } if (!string.IsNullOrEmpty(orderId)) { - nvm.Add("order_id", orderId); + parameters.Add("order_id", orderId); } if (size > 0) { - nvm.Add("size", size.ToString("F0")); + parameters.Add("size", size.ToString("F0")); } if (offset > 0) { - nvm.Add("offset", offset.ToString("F0")); + parameters.Add("offset", offset.ToString("F0")); } - var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllers.SettlementTransactions, null, nvm); - - var response = await Get(url); - - return response; + var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllers.SettlementTransactions, null, parameters); + return await Get(url).ConfigureAwait(false); } /// @@ -147,13 +136,12 @@ public async Task GetTransactions(string pay /// /// The reference id of the payout /// A of content type text/csv - public async Task GetCsvPayoutReport(string paymentReference) + public Task GetCsvPayoutReport(string paymentReference) { - var nvm = new NameValueCollection{{"payment_reference", paymentReference}}; + var parameters = new NameValueCollection{{"payment_reference", paymentReference}}; + var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllers.SettlementReports, "payout-with-transactions", parameters); - var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllers.SettlementReports, "payout-with-transactions", nvm); - - return await GetStream(url); + return GetStream(url); } /// @@ -164,17 +152,16 @@ public async Task GetCsvPayoutReport(string paymentReference) /// Required. ISO-8601 formatted date with optional time string /// Required. ISO-8601 formatted date with optional time string /// A of content type text/csv - public async Task GetCsvSummary(string startDate, string endDate) + public Task GetCsvSummary(string startDate, string endDate) { - var nvm = new NameValueCollection + var parameters = new NameValueCollection { { "start_date", startDate }, { "end_date", endDate } }; - var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllers.SettlementReports, "payouts-summary-with-transactions", nvm); - - return await GetStream(url); + var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllers.SettlementReports, "payouts-summary-with-transactions", parameters); + return GetStream(url); } /// @@ -182,13 +169,12 @@ public async Task GetCsvSummary(string startDate, string endDate) /// /// Required. The reference id of the payout /// A of content type application/pdf - public async Task GetPdfPayoutSummary(string paymentReference) + public Task GetPdfPayoutSummary(string paymentReference) { - var nvm = new NameValueCollection { { "payment_reference", paymentReference } }; - - var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllers.SettlementReports, "payout", nvm); + var parameters = new NameValueCollection { { "payment_reference", paymentReference } }; + var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllers.SettlementReports, "payout", parameters); - return await GetStream(url); + return GetStream(url); } /// @@ -197,18 +183,16 @@ public async Task GetPdfPayoutSummary(string paymentReference) /// Required. ISO-8601 formatted date with optional time string /// Required. ISO-8601 formatted date with optional time string /// A of content type application/pdf - public async Task GetPdfSummary(string startDate, string endDate) + public Task GetPdfSummary(string startDate, string endDate) { - var nvm = new NameValueCollection + var parameters = new NameValueCollection { { "start_date", startDate }, { "end_date", endDate } }; + var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllers.SettlementReports, "payouts-summary", parameters); - var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllers.SettlementReports, "payouts-summary", nvm); - - return await GetStream(url); + return GetStream(url); } - } } diff --git a/Klarna.Rest/Klarna.Rest.Core/Store/VirtualCardServiceStore.cs b/Klarna.Rest/Klarna.Rest.Core/Store/VirtualCardServiceStore.cs index bdd9263..311d1a2 100644 --- a/Klarna.Rest/Klarna.Rest.Core/Store/VirtualCardServiceStore.cs +++ b/Klarna.Rest/Klarna.Rest.Core/Store/VirtualCardServiceStore.cs @@ -27,10 +27,7 @@ internal VirtualCardServiceStore(ApiSession apiSession, IJsonSerializer jsonSeri public async Task CreateSettlement(VirtualCardCreateSettlementRequest request) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri); - - var response = await Post(url, request); - - return response; + return await Post(url, request).ConfigureAwait(false); } /// @@ -43,10 +40,7 @@ public async Task CreateSettlement(VirtualCardCreateSettl public async Task GetSettlement(string settlementId, string keyId) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, settlementId); - - var response = await Get(url, new Dictionary{ { "KeyId", keyId } }); - - return response; + return await Get(url, new Dictionary{ { "KeyId", keyId } }).ConfigureAwait(false); } /// @@ -59,10 +53,7 @@ public async Task GetSettlement(string settlementId, stri public async Task GetSettlementForOrder(string orderId, string keyId) { var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"order/{orderId}"); - - var response = await Get(url, new Dictionary { { "KeyId", keyId } }); - - return response; + return await Get(url, new Dictionary { { "KeyId", keyId } }).ConfigureAwait(false); } } }