Skip to content

Commit f3fa04f

Browse files
committed
Added GraphClientFactory to HttpProvider
1 parent 0d34c89 commit f3fa04f

File tree

4 files changed

+32
-255
lines changed

4 files changed

+32
-255
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,27 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
103103
if (AuthenticationProvider != null)
104104
{
105105
await AuthenticationProvider.AuthenticateRequestAsync(httpRequestMessage);
106-
}
107106

108-
HttpResponseMessage response = await base.SendAsync(httpRequestMessage, cancellationToken);
107+
HttpResponseMessage response = await base.SendAsync(httpRequestMessage, cancellationToken);
109108

110-
// Chcek if response is a 401 & is not a streamed body (is buffered)
111-
if (IsUnauthorized(response) && httpRequestMessage.IsBuffered() && (AuthenticationProvider != null))
109+
// Chcek if response is a 401 & is not a streamed body (is buffered)
110+
if (IsUnauthorized(response) && httpRequestMessage.IsBuffered())
111+
{
112+
// re-issue the request to get a new access token
113+
response = await SendRetryAsync(response, cancellationToken);
114+
}
115+
116+
return response;
117+
}
118+
else
112119
{
113-
// re-issue the request to get a new access token
114-
response = await SendRetryAsync(response, cancellationToken);
120+
throw new ServiceException(
121+
new Error
122+
{
123+
Code = ErrorConstants.Codes.InvalidRequest,
124+
Message = ErrorConstants.Messages.AuthenticationProviderMissing,
125+
});
115126
}
116-
117-
return response;
118127
}
119128
}
120129
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public BaseClient(
2626
{
2727
this.BaseUrl = baseUrl;
2828
this.AuthenticationProvider = authenticationProvider;
29-
this.HttpProvider = httpProvider ?? new HttpProvider(new Serializer());
29+
this.HttpProvider = httpProvider ?? new HttpProvider(this.AuthenticationProvider, new Serializer());
3030
}
3131

3232
/// <summary>

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

Lines changed: 0 additions & 81 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,9 +259,6 @@ public HttpRequestMessage GetHttpRequestMessage()
296259
{
297260
var queryString = this.BuildQueryString();
298261
var request = new HttpRequestMessage(new HttpMethod(this.Method), string.Concat(this.RequestUrl, queryString));
299-
300-
this.AddHeadersToRequest(request);
301-
302262
return request;
303263
}
304264

@@ -340,47 +300,6 @@ internal string BuildQueryString()
340300
return null;
341301
}
342302

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-
384303
/// <summary>
385304
/// Initializes the request URL for the request, breaking it into query options and base URL.
386305
/// </summary>

0 commit comments

Comments
 (0)