-
Notifications
You must be signed in to change notification settings - Fork 62
Setup BaseRequestAdapter #359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/Microsoft.Graph.Core/Requests/BaseGraphRequestAdapter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // ------------------------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. | ||
| // ------------------------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Graph | ||
| { | ||
| using Microsoft.Kiota.Abstractions; | ||
| using Microsoft.Kiota.Abstractions.Authentication; | ||
| using Microsoft.Kiota.Abstractions.Serialization; | ||
| using Microsoft.Kiota.Http.HttpClientLibrary; | ||
| using System.Net.Http; | ||
|
|
||
| /// <summary> | ||
| /// The <see cref="IRequestAdapter"/> instance for use with microsoft graph | ||
| /// </summary> | ||
| public class BaseGraphRequestAdapter : HttpClientRequestAdapter | ||
| { | ||
| /// <summary> | ||
| /// The public constructor for <see cref="BaseGraphRequestAdapter"/> | ||
| /// </summary> | ||
| /// <param name="authenticationProvider">The authentication provider.</param> | ||
| /// <param name="graphClientOptions">The options for the graph client</param> | ||
| /// <param name="parseNodeFactory">The parse node factory.</param> | ||
| /// <param name="serializationWriterFactory">The serialization writer factory.</param> | ||
| /// <param name="httpClient">The native HTTP client.</param> | ||
| public BaseGraphRequestAdapter(IAuthenticationProvider authenticationProvider, GraphClientOptions graphClientOptions = null, IParseNodeFactory parseNodeFactory = null, ISerializationWriterFactory serializationWriterFactory = null, HttpClient httpClient = null) | ||
| : base(authenticationProvider, parseNodeFactory ?? ParseNodeFactoryRegistry.DefaultInstance, serializationWriterFactory ?? SerializationWriterFactoryRegistry.DefaultInstance, httpClient ?? GraphClientFactory.Create(graphClientOptions)) | ||
| { | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // ------------------------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. | ||
| // ------------------------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Graph | ||
| { | ||
| /// <summary> | ||
| /// The options for setting up a given graph client | ||
| /// </summary> | ||
| public class GraphClientOptions | ||
| { | ||
| /// <summary> | ||
| /// The target version of the api endpoint we are targeting (v1 or beta) | ||
| /// </summary> | ||
| public string GraphServiceTargetVersion { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The version of the service library in use. Should be in the format `x.x.x` (Semantic version) | ||
| /// </summary> | ||
| public string GraphServiceLibraryClientVersion { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The version of the core library in use. Should be in the format `x.x.x` (Semantic version). | ||
| /// </summary> | ||
| public string GraphCoreClientVersion { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The product prefix to use in setting the telemetry headers. | ||
| /// Will default to `graph-dotnet` if not set. | ||
| /// </summary> | ||
| public string GraphProductPrefix { get; set; } | ||
| } | ||
| } |
82 changes: 82 additions & 0 deletions
82
src/Microsoft.Graph.Core/Requests/Middleware/GraphTelemetryHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // ------------------------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. | ||
| // ------------------------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Graph | ||
| { | ||
| using System; | ||
| using System.Net.Http; | ||
| using System.Reflection; | ||
| using System.Runtime.InteropServices; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| /// <summary> | ||
| /// A <see cref="DelegatingHandler"/> implementation that telemetry for graph. | ||
| /// </summary> | ||
| public class GraphTelemetryHandler : DelegatingHandler | ||
| { | ||
| /// The version for current assembly. | ||
| private static Version assemblyVersion = typeof(GraphTelemetryHandler).GetTypeInfo().Assembly.GetName().Version; | ||
|
|
||
| /// The value for the SDK version header. | ||
| private static string SdkVersionHeaderValue = string.Format( | ||
| CoreConstants.Headers.SdkVersionHeaderValueFormatString, | ||
| assemblyVersion.Major, | ||
| assemblyVersion.Minor, | ||
| assemblyVersion.Build); | ||
|
|
||
| private readonly GraphClientOptions graphClientOptions; | ||
|
|
||
| /// <summary> | ||
| /// The <see cref="GraphClientOptions"/> constructor. | ||
| /// </summary> | ||
| /// <param name="graphClientOptions"></param> | ||
| public GraphTelemetryHandler(GraphClientOptions graphClientOptions = null) | ||
| { | ||
| this.graphClientOptions = graphClientOptions ?? new GraphClientOptions(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sends a HTTP request. | ||
| /// </summary> | ||
| /// <param name="httpRequest">The <see cref="HttpRequestMessage"/> to be sent.</param> | ||
| /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param> | ||
| /// <returns></returns> | ||
| protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken) | ||
| { | ||
| if (httpRequest == null) | ||
| throw new ArgumentNullException(nameof(httpRequest)); | ||
|
|
||
| // Build the service library string from the options | ||
| var serviceLibraryString = string.Empty; | ||
| if (!string.IsNullOrEmpty(graphClientOptions?.GraphServiceLibraryClientVersion)) | ||
| { | ||
| serviceLibraryString = graphClientOptions?.GraphProductPrefix ?? "graph-dotnet"; | ||
| if (!string.IsNullOrEmpty(graphClientOptions?.GraphServiceTargetVersion)) | ||
| serviceLibraryString += $"-{graphClientOptions?.GraphServiceTargetVersion}"; | ||
| serviceLibraryString += $"/{graphClientOptions?.GraphServiceLibraryClientVersion},"; | ||
| } | ||
|
|
||
| // Default to the version string we have, otherwise use the ope provided | ||
| var coreLibraryString = SdkVersionHeaderValue; | ||
| if (!string.IsNullOrEmpty(graphClientOptions?.GraphCoreClientVersion) && !string.IsNullOrEmpty(graphClientOptions?.GraphProductPrefix)) | ||
| { | ||
| coreLibraryString = $"{graphClientOptions?.GraphProductPrefix}-core/{graphClientOptions?.GraphCoreClientVersion}"; | ||
| } | ||
|
|
||
| // Get the features section of the telemetry header | ||
| var features = string.Empty; | ||
| if (Environment.OSVersion != null) | ||
| features += " hostOS=" + Environment.OSVersion + ";" + " hostArch=" + RuntimeInformation.OSArchitecture + ";"; ; | ||
| features += " runtimeEnvironment=" + RuntimeInformation.FrameworkDescription + ";"; | ||
|
|
||
| var telemetryString = $"{serviceLibraryString} {coreLibraryString} (featureUsage={Enum.Format(typeof(FeatureFlag), httpRequest.GetFeatureFlags(), "x")};{features})"; | ||
| httpRequest.Headers.Add(CoreConstants.Headers.SdkVersionHeaderName, telemetryString); | ||
| httpRequest.Headers.Add(CoreConstants.Headers.ClientRequestId, Guid.NewGuid().ToString()); | ||
|
|
||
| return base.SendAsync(httpRequest, cancellationToken); | ||
| } | ||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.