Skip to content

Commit

Permalink
Merge pull request #43 from Adyen/feature/HttpClientCustomException
Browse files Browse the repository at this point in the history
Add custom exception HttpURLConnectionClient
  • Loading branch information
AlexandrosMor authored Dec 24, 2018
2 parents 728680b + 6d028b6 commit 2ab5651
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 64 deletions.
14 changes: 0 additions & 14 deletions Adyen.EcommLibrary/HttpClient/HttpClientException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 13 additions & 6 deletions Adyen.EcommLibrary/HttpClient/HttpURLConnectionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,15 +29,22 @@ public string Request(string endpoint, string json, Config config, bool isApiKey
streamWriter.Flush();
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;
throw new HttpClientException((int)response.StatusCode, "HTTP Exception", response.Headers, response.StatusDescription);
}
return responseText;
}

Expand Down
47 changes: 9 additions & 38 deletions Adyen.EcommLibrary/Service/Payment.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<PaymentResult>(jsonResponse);
}
catch (Exception ex)
{
throw ex;
}
return paymentResult;
var jsonRequest = Util.JsonOperation.SerializeRequest(paymentRequest);
var jsonResponse = _authorise.Request(jsonRequest);
return JsonConvert.DeserializeObject<PaymentResult>(jsonResponse);
}

public async Task<PaymentResult> AuthoriseAsync(PaymentRequest paymentRequest)
{
PaymentResult paymentResult = null;
try
{
var jsonRequest = Util.JsonOperation.SerializeRequest(paymentRequest);
var jsonResponse = await _authorise.RequestAsync(jsonRequest);
paymentResult = JsonConvert.DeserializeObject<PaymentResult>(jsonResponse);
}
catch (Exception ex)
{
throw ex;
}
return paymentResult;
var jsonRequest = Util.JsonOperation.SerializeRequest(paymentRequest);
var jsonResponse = await _authorise.RequestAsync(jsonRequest);
return JsonConvert.DeserializeObject<PaymentResult>(jsonResponse);
}

public PaymentResult Authorise3D(PaymentRequest3D paymentRequest3D)
{
PaymentResult paymentResult = null;
try
{
var jsonRequest = JsonConvert.SerializeObject(paymentRequest3D);
var jsonResponse = _authorise3D.Request(jsonRequest);
paymentResult = JsonConvert.DeserializeObject<PaymentResult>(jsonResponse);
}
catch (Exception ex)
{
throw ex;
}
return paymentResult;
var jsonRequest = JsonConvert.SerializeObject(paymentRequest3D);
var jsonResponse = _authorise3D.Request(jsonRequest);
return JsonConvert.DeserializeObject<PaymentResult>(jsonResponse);
}
}
}
12 changes: 6 additions & 6 deletions AdyenEcommLibrarySolution.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2ab5651

Please sign in to comment.