-
Notifications
You must be signed in to change notification settings - Fork 243
/
GraphServiceClient.cs
134 lines (122 loc) · 6.12 KB
/
GraphServiceClient.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
// ------------------------------------------------------------------------------
// 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.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Graph.Requests;
using Microsoft.Graph.Core.Requests;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Authentication.Azure;
using Microsoft.Kiota.Abstractions;
using Azure.Core;
/// <summary>
/// A default client implementation.
/// </summary>
public class GraphServiceClient : BaseGraphServiceClient, IBaseClient, IDisposable
{
private static readonly Version assemblyVersion = typeof(GraphServiceClient).GetTypeInfo().Assembly.GetName().Version;
private static readonly GraphClientOptions graphClientOptions = new GraphClientOptions
{
GraphServiceLibraryClientVersion = $"{assemblyVersion.Major}.{assemblyVersion.Minor}.{assemblyVersion.Build}",
GraphServiceTargetVersion = string.Empty,
};
/// <summary>
/// Constructs a new <see cref="GraphServiceClient"/>.
/// </summary>
/// <param name="requestAdapter">The custom <see cref="IRequestAdapter"/> to be used for making requests</param>
/// <param name="baseUrl">The base service URL. For example, "https://graph.microsoft.com/v1.0"</param>
public GraphServiceClient(IRequestAdapter requestAdapter, string baseUrl = null): base(InitializeRequestAdapterWithBaseUrl(requestAdapter,baseUrl))
{
this.RequestAdapter = requestAdapter;
}
/// <summary>
/// Constructs a new <see cref="GraphServiceClient"/>.
/// </summary>
/// <param name="tokenCredential">The <see cref="TokenCredential"/> for authenticating request messages.</param>
/// <param name="scopes">List of scopes for the authentication context.</param>
/// <param name="baseUrl">The base service URL. For example, "https://graph.microsoft.com/v1.0"</param>
public GraphServiceClient(
TokenCredential tokenCredential,
IEnumerable<string> scopes = null,
string baseUrl = null
):this(new Microsoft.Graph.Authentication.AzureIdentityAuthenticationProvider(tokenCredential, null, null,scopes?.ToArray() ?? Array.Empty<string>()), baseUrl)
{
}
/// <summary>
/// Constructs a new <see cref="GraphServiceClient"/>.
/// </summary>
/// <param name="httpClient">The customized <see cref="HttpClient"/> to be used for making requests</param>
/// <param name="tokenCredential">The <see cref="TokenCredential"/> for authenticating request messages.</param>
/// <param name="scopes">List of scopes for the authentication context.</param>
/// <param name="baseUrl">The base service URL. For example, "https://graph.microsoft.com/v1.0"</param>
public GraphServiceClient(
HttpClient httpClient,
TokenCredential tokenCredential,
IEnumerable<string> scopes = null,
string baseUrl = null
):this(httpClient, new Microsoft.Graph.Authentication.AzureIdentityAuthenticationProvider(tokenCredential, null, null, scopes?.ToArray() ?? Array.Empty<string>()), baseUrl)
{
}
/// <summary>
/// Constructs a new <see cref="GraphServiceClient"/>.
/// </summary>
/// <param name="authenticationProvider">The <see cref="IAuthenticationProvider"/> for authenticating request messages.</param>
/// <param name="baseUrl">The base service URL. For example, "https://graph.microsoft.com/v1.0"</param>
public GraphServiceClient(
IAuthenticationProvider authenticationProvider,
string baseUrl = null
): this(new BaseGraphRequestAdapter(authenticationProvider, graphClientOptions),baseUrl)
{
}
/// <summary>
/// Constructs a new <see cref="GraphServiceClient"/>.
/// </summary>
/// <param name="httpClient">The customized <see cref="HttpClient"/> to be used for making requests</param>
/// <param name="authenticationProvider">The <see cref="IAuthenticationProvider"/> for authenticating request messages.
/// Defaults to <see cref="AnonymousAuthenticationProvider"/> so that authentication is handled by custom middleware in the httpClient</param>
/// <param name="baseUrl">The base service URL. For example, "https://graph.microsoft.com/v1.0"</param>
public GraphServiceClient(
HttpClient httpClient,
IAuthenticationProvider authenticationProvider = null,
string baseUrl = null):this(new BaseGraphRequestAdapter(authenticationProvider ?? new AnonymousAuthenticationProvider(), graphClientOptions, httpClient: httpClient),baseUrl)
{
}
/// <summary>
/// Gets the <see cref="IRequestAdapter"/> for sending requests.
/// </summary>
public new IRequestAdapter RequestAdapter { get; set; }
/// <summary>
/// Gets the <see cref="BatchRequestBuilder"/> for building batch Requests
/// </summary>
public BatchRequestBuilder Batch
{
get
{
return new CustomBatchRequestBuilder(this.RequestAdapter);
}
}
/// <summary>
/// Cleanup anything as needed
/// </summary>
public void Dispose()
{
if (this.RequestAdapter is IDisposable disposable)
{
disposable.Dispose();
}
}
private static IRequestAdapter InitializeRequestAdapterWithBaseUrl(IRequestAdapter requestAdapter, string baseUrl)
{
if (!string.IsNullOrEmpty(baseUrl))
{
requestAdapter.BaseUrl = baseUrl;
}
return requestAdapter;
}
}
}