Skip to content

Commit 40bd471

Browse files
Merge pull request #330 from microsoftgraph/HttpProviderWithGraphClientFactory
Http provider with graph client factory
2 parents 1c33ddf + e97039d commit 40bd471

27 files changed

+153
-384
lines changed

src/Microsoft.Graph.Core/Requests/AsyncMonitor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ public async Task<T> PollForOperationCompletionAsync(IProgress<AsyncOperationSta
4444
{
4545
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, this.monitorUrl))
4646
{
47-
await this.client.AuthenticationProvider.AuthenticateRequestAsync(httpRequestMessage).ConfigureAwait(false);
48-
4947
using (var responseMessage = await this.client.HttpProvider.SendAsync(httpRequestMessage).ConfigureAwait(false))
5048
{
5149
// The monitor service will return an Accepted status for any monitor operation that hasn't completed.

src/Microsoft.Graph.Core/Requests/AuthenticationHandler.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ public class AuthenticationHandler: DelegatingHandler
2323
/// </summary>
2424
public IAuthenticationProvider AuthenticationProvider { get; set; }
2525

26-
/// <summary>
27-
/// Construct a new <see cref="AuthenticationHandler"/>
28-
/// </summary>
29-
public AuthenticationHandler()
30-
{
31-
32-
}
33-
3426
/// <summary>
3527
/// Construct a new <see cref="AuthenticationHandler"/>
3628
/// <param name="authenticationProvider">An authentication provider to pass to <see cref="AuthenticationHandler"/> for authenticating requests.</param>
@@ -103,18 +95,27 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
10395
if (AuthenticationProvider != null)
10496
{
10597
await AuthenticationProvider.AuthenticateRequestAsync(httpRequestMessage);
106-
}
10798

108-
HttpResponseMessage response = await base.SendAsync(httpRequestMessage, cancellationToken);
99+
HttpResponseMessage response = await base.SendAsync(httpRequestMessage, cancellationToken);
100+
101+
// Chcek if response is a 401 & is not a streamed body (is buffered)
102+
if (IsUnauthorized(response) && httpRequestMessage.IsBuffered())
103+
{
104+
// re-issue the request to get a new access token
105+
response = await SendRetryAsync(response, cancellationToken);
106+
}
109107

110-
// Chcek if response is a 401 & is not a streamed body (is buffered)
111-
if (IsUnauthorized(response) && httpRequestMessage.IsBuffered() && (AuthenticationProvider != null))
108+
return response;
109+
}
110+
else
112111
{
113-
// re-issue the request to get a new access token
114-
response = await SendRetryAsync(response, cancellationToken);
112+
throw new ServiceException(
113+
new Error
114+
{
115+
Code = ErrorConstants.Codes.InvalidRequest,
116+
Message = ErrorConstants.Messages.AuthenticationProviderMissing,
117+
});
115118
}
116-
117-
return response;
118119
}
119120
}
120121
}

src/Microsoft.Graph.Core/Requests/BaseClient.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,9 @@ public BaseClient(
2525
IHttpProvider httpProvider = null)
2626
{
2727
this.BaseUrl = baseUrl;
28-
this.AuthenticationProvider = authenticationProvider;
29-
this.HttpProvider = httpProvider ?? new HttpProvider(new Serializer());
28+
this.HttpProvider = httpProvider ?? new HttpProvider(authenticationProvider, new Serializer());
3029
}
3130

32-
/// <summary>
33-
/// Gets the <see cref="IAuthenticationProvider"/> for authenticating requests.
34-
/// </summary>
35-
public IAuthenticationProvider AuthenticationProvider { get; set; }
36-
3731
/// <summary>
3832
/// Gets or sets the base URL for requests of the client.
3933
/// </summary>

src/Microsoft.Graph.Core/Requests/BaseRequest.cs

Lines changed: 15 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ namespace Microsoft.Graph
2020
/// </summary>
2121
public class BaseRequest : IBaseRequest
2222
{
23-
/// The key for the SDK version header.
24-
protected string sdkVersionHeaderName;
25-
/// The value for the SDK version header.
26-
protected string sdkVersionHeaderValue;
27-
2823
/// <summary>
2924
/// Constructs a new <see cref="BaseRequest"/>.
3025
/// </summary>
@@ -43,9 +38,6 @@ public BaseRequest(
4338

4439
this.RequestUrl = this.InitializeUrl(requestUrl);
4540

46-
this.sdkVersionHeaderName = CoreConstants.Headers.SdkVersionHeaderName;
47-
this.SdkVersionHeaderPrefix = "Graph";
48-
4941
if (options != null)
5042
{
5143
var headerOptions = options.OfType<HeaderOption>();
@@ -91,11 +83,6 @@ public BaseRequest(
9183
/// Gets the URL for the request, without query string.
9284
/// </summary>
9385
public string RequestUrl { get; internal set; }
94-
95-
/// <summary>
96-
/// Gets or sets the telemetry header prefix for requests.
97-
/// </summary>
98-
protected string SdkVersionHeaderPrefix { get; set; }
9986

10087
/// <summary>
10188
/// Sends the request.
@@ -202,22 +189,10 @@ public async Task<HttpResponseMessage> SendMultiPartRequestAsync(
202189
});
203190
}
204191

205-
if (this.Client.AuthenticationProvider == null)
206-
{
207-
throw new ServiceException(
208-
new Error
209-
{
210-
Code = ErrorConstants.Codes.InvalidRequest,
211-
Message = ErrorConstants.Messages.AuthenticationProviderMissing,
212-
});
213-
}
214-
215192
if (multipartContent != null)
216193
{
217194
using (var request = this.GetHttpRequestMessage())
218195
{
219-
await this.AuthenticateRequest(request).ConfigureAwait(false);
220-
221196
request.Content = multipartContent;
222197

223198
return await this.Client.HttpProvider.SendAsync(request, completionOption, cancellationToken).ConfigureAwait(false);
@@ -251,20 +226,8 @@ public async Task<HttpResponseMessage> SendRequestAsync(
251226
});
252227
}
253228

254-
if (this.Client.AuthenticationProvider == null)
255-
{
256-
throw new ServiceException(
257-
new Error
258-
{
259-
Code = ErrorConstants.Codes.InvalidRequest,
260-
Message = ErrorConstants.Messages.AuthenticationProviderMissing,
261-
});
262-
}
263-
264229
using (var request = this.GetHttpRequestMessage())
265230
{
266-
await this.AuthenticateRequest(request).ConfigureAwait(false);
267-
268231
if (serializableObject != null)
269232
{
270233
var inputStream = serializableObject as Stream;
@@ -296,12 +259,25 @@ public HttpRequestMessage GetHttpRequestMessage()
296259
{
297260
var queryString = this.BuildQueryString();
298261
var request = new HttpRequestMessage(new HttpMethod(this.Method), string.Concat(this.RequestUrl, queryString));
299-
300262
this.AddHeadersToRequest(request);
301-
302263
return request;
303264
}
304265

266+
/// <summary>
267+
/// Adds all of the headers from the header collection to the request.
268+
/// </summary>
269+
/// <param name="request">The <see cref="HttpRequestMessage"/> representation of the request.</param>
270+
private void AddHeadersToRequest(HttpRequestMessage request)
271+
{
272+
if (this.Headers != null)
273+
{
274+
foreach (var header in this.Headers)
275+
{
276+
request.Headers.TryAddWithoutValidation(header.Name, header.Value);
277+
}
278+
}
279+
}
280+
305281
/// <summary>
306282
/// Gets a URL that is the request builder's request URL with the segment appended.
307283
/// </summary>
@@ -340,47 +316,6 @@ internal string BuildQueryString()
340316
return null;
341317
}
342318

343-
/// <summary>
344-
/// Adds all of the headers from the header collection to the request.
345-
/// </summary>
346-
/// <param name="request">The <see cref="HttpRequestMessage"/> representation of the request.</param>
347-
private void AddHeadersToRequest(HttpRequestMessage request)
348-
{
349-
if (this.Headers != null)
350-
{
351-
foreach (var header in this.Headers)
352-
{
353-
request.Headers.TryAddWithoutValidation(header.Name, header.Value);
354-
}
355-
}
356-
357-
if (string.IsNullOrEmpty(this.sdkVersionHeaderValue))
358-
{
359-
var assemblyVersion = this.GetType().GetTypeInfo().Assembly.GetName().Version;
360-
this.sdkVersionHeaderValue = string.Format(
361-
CoreConstants.Headers.SdkVersionHeaderValueFormatString,
362-
this.SdkVersionHeaderPrefix,
363-
assemblyVersion.Major,
364-
assemblyVersion.Minor,
365-
assemblyVersion.Build);
366-
}
367-
368-
// Append SDK version header for telemetry
369-
request.Headers.Add(
370-
this.sdkVersionHeaderName,
371-
this.sdkVersionHeaderValue);
372-
}
373-
374-
/// <summary>
375-
/// Adds the authentication header to the request.
376-
/// </summary>
377-
/// <param name="request">The <see cref="HttpRequestMessage"/> representation of the request.</param>
378-
/// <returns>The task to await.</returns>
379-
private Task AuthenticateRequest(HttpRequestMessage request)
380-
{
381-
return this.Client.AuthenticationProvider.AuthenticateRequestAsync(request);
382-
}
383-
384319
/// <summary>
385320
/// Initializes the request URL for the request, breaking it into query options and base URL.
386321
/// </summary>

0 commit comments

Comments
 (0)