-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathAPIService.cs
146 lines (135 loc) · 5.71 KB
/
APIService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using log4net;
using PayPal.Manager;
using PayPal.Exception;
using PayPal.Authentication;
namespace PayPal
{
/// <summary>
/// Calls the actual Platform API web service for the given Payload and APIProfile settings
/// </summary>
public class APIService
{
/// <summary>
/// HTTP Method needs to be set.
/// </summary>
private const string RequestMethod = BaseConstants.REQUESTMETHOD;
/// <summary>
/// X509Certificate
/// </summary>
private X509Certificate x509;
/// <summary>
/// Exception log
/// </summary>
private static readonly ILog log = LogManager.GetLogger(typeof(APIService));
private static ArrayList retryCodes = new ArrayList(new HttpStatusCode[]
{ HttpStatusCode.GatewayTimeout,
HttpStatusCode.RequestTimeout,
HttpStatusCode.InternalServerError,
HttpStatusCode.ServiceUnavailable,
});
/// <summary>
/// Makes a request to API service
/// </summary>
/// <param name="apiCallHandler"></param>
/// <returns></returns>
public string MakeRequestUsing(IAPICallPreHandler apiCallHandler)
{
string responseString = string.Empty;
string uri = apiCallHandler.GetEndPoint();
Dictionary<string, string> headers = apiCallHandler.GetHeaderMap();
string payLoad = apiCallHandler.GetPayLoad();
ConfigManager configMngr = ConfigManager.Instance;
// Constructing HttpWebRequest object
ConnectionManager connMngr = ConnectionManager.Instance;
HttpWebRequest httpRequest = connMngr.GetConnection(uri);
httpRequest.Method = RequestMethod;
foreach (KeyValuePair<string, string> header in headers)
{
httpRequest.Headers.Add(header.Key, header.Value);
}
if (log.IsDebugEnabled)
{
foreach (string headerName in httpRequest.Headers)
{
log.Debug(headerName + ":" + httpRequest.Headers[headerName]);
}
}
// Adding payLoad to HttpWebRequest object
using (StreamWriter myWriter = new StreamWriter(httpRequest.GetRequestStream()))
{
myWriter.Write(payLoad);
log.Debug(payLoad);
}
if (apiCallHandler.GetCredential() is CertificateCredential)
{
CertificateCredential certCredential = (CertificateCredential)apiCallHandler.GetCredential();
// Load the certificate into an X509Certificate2 object.
if (((CertificateCredential)certCredential).PrivateKeyPassword.Trim() == string.Empty)
{
x509 = new X509Certificate2(((CertificateCredential)certCredential).CertificateFile);
}
else
{
x509 = new X509Certificate2(((CertificateCredential)certCredential).CertificateFile, ((CertificateCredential)certCredential).PrivateKeyPassword);
}
httpRequest.ClientCertificates.Add(x509);
}
// Fire request. Retry if configured to do so
int numRetries = (configMngr.GetProperty(BaseConstants.HTTP_CONNECTION_RETRY) != null) ?
Convert.ToInt32(configMngr.GetProperty(BaseConstants.HTTP_CONNECTION_RETRY)) : 0;
int retries = 0;
do
{
try
{
// Calling the plaftform API web service and getting the response
using (WebResponse response = httpRequest.GetResponse())
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
responseString = sr.ReadToEnd();
log.Debug("Service response");
log.Debug(responseString);
return responseString;
}
}
}
// Server responses in the range of 4xx and 5xx throw a WebException
catch (WebException we)
{
HttpStatusCode statusCode = ((HttpWebResponse)we.Response).StatusCode;
log.Info("Got " + statusCode.ToString() + " response from server");
if (!RequiresRetry(we))
{
throw new ConnectionException("Invalid HTTP response " + we.Message);
}
}
catch(System.Exception ex)
{
throw ex;
}
} while (retries++ < numRetries);
throw new ConnectionException("Invalid HTTP response");
}
/// <summary>
/// Returns true if a HTTP retry is required
/// </summary>
/// <param name="ex"></param>
/// <returns></returns>
private static bool RequiresRetry(WebException ex)
{
if (ex.Status != WebExceptionStatus.ProtocolError)
{
return false;
}
HttpStatusCode status = ((HttpWebResponse)ex.Response).StatusCode;
return retryCodes.Contains(status);
}
}
}