From a22efb2f51d39c154065d74f0877b88b86d52914 Mon Sep 17 00:00:00 2001 From: alexandros Date: Thu, 20 Dec 2018 13:50:58 +0100 Subject: [PATCH 1/3] Add custom exception HttpURLConnectionClient --- .../HttpClient/HttpClientException.cs | 14 ------ .../HttpClient/HttpURLConnectionClient.cs | 24 ++++++++-- Adyen.EcommLibrary/Service/Payment.cs | 47 ++++--------------- AdyenEcommLibrarySolution.sln | 12 ++--- 4 files changed, 35 insertions(+), 62 deletions(-) diff --git a/Adyen.EcommLibrary/HttpClient/HttpClientException.cs b/Adyen.EcommLibrary/HttpClient/HttpClientException.cs index a0a94490b..8a820db00 100644 --- a/Adyen.EcommLibrary/HttpClient/HttpClientException.cs +++ b/Adyen.EcommLibrary/HttpClient/HttpClientException.cs @@ -10,20 +10,6 @@ public class HttpClientException : Exception public WebHeaderCollection WebHeaderCollection { get; private set; } public string ResponseBody { get; private set; } - public HttpClientException() - { - } - - public HttpClientException(string message) : base(message) - { - } - - public HttpClientException(int code, string message) : base(message) - { - this.Code = code; - } - - public HttpClientException( int code, string message, WebHeaderCollection webHeaderCollection, string responseBody) : base(message) { this.Code = code; diff --git a/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs b/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs index 93b790131..0521b20c0 100644 --- a/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs +++ b/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs @@ -17,7 +17,7 @@ public class HttpUrlConnectionClient : IClient public string Request(string endpoint, string json, Config config, bool isApiKeyRequired) { - string responseText; + string responseText=null; //Set security protocol. Only TLS1.2 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; @@ -30,13 +30,29 @@ public string Request(string endpoint, string json, Config config, bool isApiKey streamWriter.Close(); } - using (var response = (HttpWebResponse)httpWebRequest.GetResponse()) + try { - using (var reader = new StreamReader(response.GetResponseStream(), _encoding)) + using (var response = (HttpWebResponse)httpWebRequest.GetResponse()) { - responseText = reader.ReadToEnd(); + using (var reader = new StreamReader(response.GetResponseStream(), _encoding)) + { + responseText = reader.ReadToEnd(); + } + } } + catch (WebException e) + { + var response = (HttpWebResponse)e.Response; + var httpClientException = new HttpClientException((int)response.StatusCode, "HTTP Exception", + response.Headers, response.StatusDescription); + throw httpClientException; + } + catch (Exception) + { + + throw; + } return responseText; } diff --git a/Adyen.EcommLibrary/Service/Payment.cs b/Adyen.EcommLibrary/Service/Payment.cs index fb562ebe2..4060756fa 100644 --- a/Adyen.EcommLibrary/Service/Payment.cs +++ b/Adyen.EcommLibrary/Service/Payment.cs @@ -1,7 +1,6 @@ using Adyen.EcommLibrary.Model; using Adyen.EcommLibrary.Service.Resource.Payment; using Newtonsoft.Json; -using System; using System.Threading.Tasks; namespace Adyen.EcommLibrary.Service @@ -20,51 +19,23 @@ public Payment(Client client) public PaymentResult Authorise(PaymentRequest paymentRequest) { - PaymentResult paymentResult = null; - try - { - var jsonRequest = Util.JsonOperation.SerializeRequest(paymentRequest); - - var jsonResponse = _authorise.Request(jsonRequest); - paymentResult = JsonConvert.DeserializeObject(jsonResponse); - } - catch (Exception ex) - { - throw ex; - } - return paymentResult; + var jsonRequest = Util.JsonOperation.SerializeRequest(paymentRequest); + var jsonResponse = _authorise.Request(jsonRequest); + return JsonConvert.DeserializeObject(jsonResponse); } public async Task AuthoriseAsync(PaymentRequest paymentRequest) { - PaymentResult paymentResult = null; - try - { - var jsonRequest = Util.JsonOperation.SerializeRequest(paymentRequest); - var jsonResponse = await _authorise.RequestAsync(jsonRequest); - paymentResult = JsonConvert.DeserializeObject(jsonResponse); - } - catch (Exception ex) - { - throw ex; - } - return paymentResult; + var jsonRequest = Util.JsonOperation.SerializeRequest(paymentRequest); + var jsonResponse = await _authorise.RequestAsync(jsonRequest); + return JsonConvert.DeserializeObject(jsonResponse); } public PaymentResult Authorise3D(PaymentRequest3D paymentRequest3D) { - PaymentResult paymentResult = null; - try - { - var jsonRequest = JsonConvert.SerializeObject(paymentRequest3D); - var jsonResponse = _authorise3D.Request(jsonRequest); - paymentResult = JsonConvert.DeserializeObject(jsonResponse); - } - catch (Exception ex) - { - throw ex; - } - return paymentResult; + var jsonRequest = JsonConvert.SerializeObject(paymentRequest3D); + var jsonResponse = _authorise3D.Request(jsonRequest); + return JsonConvert.DeserializeObject(jsonResponse); } } } diff --git a/AdyenEcommLibrarySolution.sln b/AdyenEcommLibrarySolution.sln index 950e94374..390d6ce41 100644 --- a/AdyenEcommLibrarySolution.sln +++ b/AdyenEcommLibrarySolution.sln @@ -3,24 +3,24 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.27130.2026 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Adyen.EcommLibrary.Test", "Adyen.EcommLibrary.Test\Adyen.EcommLibrary.Test.csproj", "{8AC1D1BC-1B6A-4DE4-93C2-91C845EC31D9}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Adyen.EcommLibrary", "Adyen.EcommLibrary\Adyen.EcommLibrary.csproj", "{2C020234-B871-4F19-BB6A-2CECECAC3D20}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Adyen.EcommLibrary.Test", "Adyen.EcommLibrary.Test\Adyen.EcommLibrary.Test.csproj", "{B0005586-6CC5-4AD2-AE84-0D38EF6CA301}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {8AC1D1BC-1B6A-4DE4-93C2-91C845EC31D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8AC1D1BC-1B6A-4DE4-93C2-91C845EC31D9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8AC1D1BC-1B6A-4DE4-93C2-91C845EC31D9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8AC1D1BC-1B6A-4DE4-93C2-91C845EC31D9}.Release|Any CPU.Build.0 = Release|Any CPU {2C020234-B871-4F19-BB6A-2CECECAC3D20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {2C020234-B871-4F19-BB6A-2CECECAC3D20}.Debug|Any CPU.Build.0 = Debug|Any CPU {2C020234-B871-4F19-BB6A-2CECECAC3D20}.Release|Any CPU.ActiveCfg = Release|Any CPU {2C020234-B871-4F19-BB6A-2CECECAC3D20}.Release|Any CPU.Build.0 = Release|Any CPU + {B0005586-6CC5-4AD2-AE84-0D38EF6CA301}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B0005586-6CC5-4AD2-AE84-0D38EF6CA301}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B0005586-6CC5-4AD2-AE84-0D38EF6CA301}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B0005586-6CC5-4AD2-AE84-0D38EF6CA301}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From f5d27ef1ed0d30c6eab3a1ec59af3a5428701b7b Mon Sep 17 00:00:00 2001 From: alexandros Date: Thu, 20 Dec 2018 14:00:38 +0100 Subject: [PATCH 2/3] Removed unused exception --- .../HttpClient/HttpURLConnectionClient.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs b/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs index 0521b20c0..915d08f2f 100644 --- a/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs +++ b/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs @@ -17,7 +17,7 @@ public class HttpUrlConnectionClient : IClient public string Request(string endpoint, string json, Config config, bool isApiKeyRequired) { - string responseText=null; + string responseText = null; //Set security protocol. Only TLS1.2 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; @@ -29,7 +29,6 @@ public string Request(string endpoint, string json, Config config, bool isApiKey streamWriter.Flush(); streamWriter.Close(); } - try { using (var response = (HttpWebResponse)httpWebRequest.GetResponse()) @@ -38,7 +37,7 @@ public string Request(string endpoint, string json, Config config, bool isApiKey { responseText = reader.ReadToEnd(); } - + } } catch (WebException e) @@ -48,12 +47,6 @@ public string Request(string endpoint, string json, Config config, bool isApiKey response.Headers, response.StatusDescription); throw httpClientException; } - catch (Exception) - { - - throw; - } - return responseText; } From 6d028b6cf9e9cc1120e337ea1f84053272b8ab0a Mon Sep 17 00:00:00 2001 From: alexandros Date: Thu, 20 Dec 2018 14:29:59 +0100 Subject: [PATCH 3/3] small refactor --- Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs b/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs index 915d08f2f..24a1687f0 100644 --- a/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs +++ b/Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs @@ -43,9 +43,7 @@ public string Request(string endpoint, string json, Config config, bool isApiKey catch (WebException e) { var response = (HttpWebResponse)e.Response; - var httpClientException = new HttpClientException((int)response.StatusCode, "HTTP Exception", - response.Headers, response.StatusDescription); - throw httpClientException; + throw new HttpClientException((int)response.StatusCode, "HTTP Exception", response.Headers, response.StatusDescription); } return responseText; }