-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpRestClient.cs
276 lines (249 loc) · 13.1 KB
/
HttpRestClient.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
namespace Microsoft.WindowsAzure.Management.HDInsight.Framework.Rest
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Reflection;
using Microsoft.HDInsight.Net.Http.Formatting;
/// <summary>
/// An abstract class to inherit from to write a rest proxy against a service interface.
/// internal class DeploymentServiceProxy : RestProxy<IDeploymentService >.
/// </summary>
/// <typeparam name="TServiceInterface">The type of the service interface for which you need the proxy for.</typeparam>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1012:AbstractTypesShouldNotHaveConstructors", Justification = "This is so that the concrete types can have them.")]
public abstract class HttpRestClient<TServiceInterface>
{
private readonly Uri _baseServiceUri;
private readonly IDictionary<string, HttpRequestParameters> _methodToRequestParametersMap = new Dictionary<string, HttpRequestParameters>();
private readonly HttpRestClientConfiguration _configuration;
/// <summary>
/// Gets the base service URI.
/// </summary>
/// <value>The base service URI.</value>
public Uri BaseServiceUri
{
get { return this._baseServiceUri; }
}
/// <summary>
/// Gets the configuration.
/// </summary>
/// <value>
/// The configuration.
/// </value>
public HttpRestClientConfiguration Configuration
{
get { return this._configuration; }
}
private static string GetUniqueInterfaceMethodSignature(MethodInfo info)
{
var allNames = new List<string>();
allNames.Add(info.Name);
allNames.AddRange(info.GetParameters().Select(pInfo => pInfo.ParameterType.FullName));
return string.Join("_", allNames);
}
private static IDictionary<string, HttpRequestParameters> BuildInterfaceMethodToParametersDictionary()
{
IEnumerable<MethodInfo> interfaceMethods = typeof(TServiceInterface).GetMethods()
.Where(method => HttpRestInterfaceValidator.ValidateInterfaceMethod(method));
Dictionary<string, HttpRequestParameters> dictionary = interfaceMethods.ToDictionary(
GetUniqueInterfaceMethodSignature, HttpRequestParameters.FromMethodInfo);
return dictionary;
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpRestClient{TServiceInterface}"/> class.
/// </summary>
/// <param name="baseUri">The base URI.</param>
/// <param name="configuration">The configuration.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "Validated", MessageId = "0")]
public HttpRestClient(Uri baseUri, HttpRestClientConfiguration configuration)
{
Contract.Requires<ArgumentNullException>(baseUri != null);
Contract.Requires<ArgumentNullException>(baseUri.IsAbsoluteUri);
Contract.Requires<ArgumentException>(baseUri.Scheme.Equals(Uri.UriSchemeHttp) || baseUri.Scheme.Equals(Uri.UriSchemeHttps));
this._baseServiceUri = baseUri;
this._methodToRequestParametersMap = BuildInterfaceMethodToParametersDictionary();
this._configuration = configuration;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No need")]
private HttpRequestMessage CreateHttpRequestMessage(HttpRequestParameters parameters, MethodInfo method, out CancellationToken cancellationToken, object[] methodArgumentsInOrder)
{
List<string> parameterNamesBoundToUri;
var requestUri = parameters.BindUri(this._baseServiceUri, method, methodArgumentsInOrder, out parameterNamesBoundToUri, out cancellationToken);
HttpRequestMessage requestMessage = new HttpRequestMessage(new HttpMethod(parameters.HttpMethod), requestUri);
//The arguments when not used for uri binding either the request body or the cancellation token
// the request body is serialized into the stream as the request body
if (methodArgumentsInOrder.Length > parameterNamesBoundToUri.Count)
{
//This ordered as they appear in the method signature
var parameterInfos = method.GetParameters();
for (int i = 0; i < parameterInfos.Length; i++)
{
var parameterName = parameterInfos[i].Name;
if (
!parameterNamesBoundToUri.Any(
paramName => paramName.Equals(parameterName, StringComparison.OrdinalIgnoreCase)))
{
if (parameterInfos[i].ParameterType != typeof(CancellationToken))
{
//Pick the unbound argument for request stream data
object dataToSerialize = methodArgumentsInOrder[i];
//serialize the data using the specified serializer into the request message
requestMessage.Content = new ObjectContent(dataToSerialize.GetType(), dataToSerialize, parameters.RequestFormatter.RequestFormatter);
}
}
}
}
foreach (var formatter in parameters.ResponseFormatter.ResponseFormatters)
{
foreach (var mediaType in formatter.SupportedMediaTypes)
{
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType.MediaType));
}
}
return requestMessage;
}
/// <summary>
/// Creates the HTTP client by chaining all the handlers found in the interface method attributes and the http
/// client configuration.
/// </summary>
/// <param name="parameters">The parameters.</param>
/// <returns>An instance of HttpClient.</returns>
private HttpClient CreateHttpClient(HttpRequestParameters parameters)
{
HttpMessageHandler rootHandler = this.Configuration.RootHandler;
if (parameters.CutomMessageProcessingHandlers.Any())
{
IList<DelegatingHandler> handlers =
parameters.CutomMessageProcessingHandlers.Select(c => c.CreateHandler()).ToList();
for (int i = 0; i < handlers.Count - 1; i++)
{
handlers[i].InnerHandler = handlers[i + 1];
}
handlers.Last().InnerHandler = this.Configuration.RootHandler;
rootHandler = handlers.First();
}
else
{
rootHandler = this.Configuration.RootHandler;
}
return new HttpClient(rootHandler, false);
}
/// <summary>
/// Creates the and invoke rest request for caller based of its interface implementation.
/// </summary>
/// <typeparam name="T">The return type.</typeparam>
/// <param name="classMethod">The class verb.</param>
/// <param name="methodArgumentsInOrder">The verb arguments in order.</param>
/// <returns>A task.</returns>
private async Task<T> InvokeHttpRestRequest<T>(MethodInfo classMethod, object[] methodArgumentsInOrder)
{
Contract.Requires<ArgumentNullException>(methodArgumentsInOrder != null);
//Transform the reflected attributes from the method
//Pull that information from the cache we created on construction
var requestParametersFromMethod = this._methodToRequestParametersMap[GetUniqueInterfaceMethodSignature(classMethod)];
Type returnType = null;
if (classMethod.ReturnType.IsGenericType && classMethod.ReturnType.BaseType == typeof(Task))
{
returnType = classMethod.ReturnType.GetGenericArguments().Single();
}
else if (classMethod.ReturnType == typeof(void) || classMethod.ReturnType == typeof(Task))
{
returnType = typeof(void);
}
else
{
returnType = classMethod.ReturnType;
}
T retVal = default(T);
for (int attempt = 1;; attempt++)
{
Exception lastException = null;
TimeSpan delay = TimeSpan.MinValue;
//Create the request message from the request parameters,i.e. set the headers and serialize the request
CancellationToken cancellationToken;
var requestMessage = this.CreateHttpRequestMessage(requestParametersFromMethod, classMethod, out cancellationToken, methodArgumentsInOrder);
HttpClient client = this.CreateHttpClient(requestParametersFromMethod);
client.Timeout = this.Configuration.HttpRequestTimeout;
try
{
using (var resp = await client.SendAsync(requestMessage, cancellationToken))
{
if (returnType == typeof(HttpResponseMessage))
{
return (T)(object)resp;
}
if (returnType != typeof(void))
{
retVal = (T)await resp.Content.ReadAsAsync(returnType, requestParametersFromMethod.ResponseFormatter.ResponseFormatters);
return retVal;
}
return default(T);
}
}
catch (Exception e)
{
if (e is OperationCanceledException)
{
throw;
}
lastException = e;
if (!this.Configuration.RetryPolicy.ShouldRetry(e, attempt, out delay))
{
throw;
}
}
if (lastException != null)
{
await Task.Delay(delay);
}
}
}
/// <summary>
/// Creates and invokes the rest request for caller based of its interface definition.
/// </summary>
/// <param name="orderedArgumentsOfTheParentMethod">The ordered arguments of the parent verb.</param>
/// <returns>A task.</returns>
/// <remarks>Please be sure to dispose the HttpWebResponseObject after you are done with it.</remarks>
protected Task<T> CreateAndInvokeRestRequestForParentMethodAsync<T>(params object[] orderedArgumentsOfTheParentMethod)
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
//Get parent from the stackframe
MethodInfo classMethod = (MethodInfo)stackFrame.GetMethod();
return this.InvokeHttpRestRequest<T>(classMethod, orderedArgumentsOfTheParentMethod);
}
/// <summary>
/// Creates and invokes the rest request for caller based of its interface definition.
/// </summary>
/// <param name="orderedArgumentsOfTheParentMethod">The ordered arguments of the parent verb.</param>
/// <returns>A task.</returns>
/// <remarks>Please be sure to dispose the HttpWebResponseObject after you are done with it.</remarks>
protected Task CreateAndInvokeRestRequestForParentMethodAsync(params object[] orderedArgumentsOfTheParentMethod)
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
//Get parent from the stackframe
MethodInfo classMethod = (MethodInfo)stackFrame.GetMethod();
return this.InvokeHttpRestRequest<object>(classMethod, orderedArgumentsOfTheParentMethod);
}
/// <summary>
/// Creates and invokes the rest request for caller based of its interface definition.
/// </summary>
/// <param name="orderedArgumentsOfTheParentMethod">The ordered arguments of the parent verb.</param>
/// <returns>A task.</returns>
/// <remarks>Please be sure to dispose the HttpWebResponseObject after you are done with it.</remarks>
protected object CreateAndInvokeRestRequestForParentMethod(params object[] orderedArgumentsOfTheParentMethod)
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
//Get parent from the stackframe
MethodInfo classMethod = (MethodInfo)stackFrame.GetMethod();
return this.InvokeHttpRestRequest<object>(classMethod, orderedArgumentsOfTheParentMethod).GetAwaiter().GetResult();
}
}
}