Skip to content

Commit cf45368

Browse files
committed
Refactor to allow passing request adapter object
1 parent 6e1217f commit cf45368

8 files changed

Lines changed: 32 additions & 37 deletions

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ credential = ClientSecretCredential(
3636
client_secret='CLIENT_SECRET',
3737
)
3838
scopes = ['https://graph.microsoft.com/.default']
39-
client = GraphServiceClient(credentials, scopes=scopes)
39+
client = GraphServiceClient(credentials=credentials, scopes=scopes)
4040
```
4141

4242
The above example uses default scopes for [app-only access](https://learn.microsoft.com/en-us/graph/permissions-overview?tabs=http#application-permissions). If using [delegated access](https://learn.microsoft.com/en-us/graph/permissions-overview#delegated-permissions) you can provide custom scopes:
@@ -51,7 +51,7 @@ credential=DeviceCodeCredential(
5151
tenant_id='TENANT_ID',
5252
)
5353
scopes = ['User.Read', 'Mail.Read']
54-
client = GraphServiceClient(credentials, scopes=scopes)
54+
client = GraphServiceClient(credentials=credentials, scopes=scopes)
5555
```
5656

5757
## 3. Make requests against the service
@@ -73,7 +73,7 @@ credential = ClientSecretCredential(
7373
'client_secret'
7474
)
7575
scopes = ['https://graph.microsoft.com/.default']
76-
client = GraphServiceClient(credentials, scopes=scopes)
76+
client = GraphServiceClient(credentials=credentials, scopes=scopes)
7777

7878
async def get_user():
7979
user = await client.users.by_user_id('userPrincipalName').get()
@@ -92,7 +92,7 @@ from msgraph import GraphServiceClient
9292

9393
credential = InteractiveBrowserCredential()
9494
scopes=['User.Read']
95-
client = GraphServiceClient(credentials, scopes=scopes,)
95+
client = GraphServiceClient(credentials=credentials, scopes=scopes,)
9696

9797
async def me():
9898
me = await client.me.get()

msgraph/graph_service_client.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
class GraphServiceClient(BaseGraphServiceClient):
1111
def __init__(
1212
self,
13-
credentials: Union[TokenCredential, AsyncTokenCredential],
14-
scopes: Optional[List[str]],
15-
base_url: Optional[str] = None,
16-
client: Optional[AsyncClient] = None
13+
credentials: Optional[Union[TokenCredential, AsyncTokenCredential]] = None,
14+
scopes: Optional[List[str]] = None,
15+
request_adapter: Optional[GraphRequestAdapter] = None
16+
1717
) -> None:
1818
"""Constructs a client instance to use for making requests to the
1919
Microsoft Graph v.1.0 API.
@@ -23,24 +23,19 @@ def __init__(
2323
tokenCredential to use for authentication.
2424
scopes (Optional[List[str]]): The scopes to use for authentication.
2525
Defaults to ['https://graph.microsoft.com/.default'].
26-
base_url (Optional[str], optional): The base url to use for
27-
requests. Defaults to https://graph.microsoft.com/v1.0.
28-
client (Optional[AsyncClient], optional): A custom httpx.AsyncClient
29-
to use for http requests. Defaults to None.
26+
request_adapter (Optional[GraphRequestAdapter], optional): The
27+
request adapter to use for requests. Defaults to None.
3028
"""
3129

32-
if scopes:
33-
auth_provider = AzureIdentityAuthenticationProvider(credentials, scopes)
34-
else:
35-
auth_provider = AzureIdentityAuthenticationProvider(credentials)
36-
37-
if client:
38-
request_adapter = GraphRequestAdapter(auth_provider, client)
39-
else:
30+
if not request_adapter:
31+
if not credentials:
32+
raise ValueError("Missing request adapter or valid credentials")
33+
34+
if scopes:
35+
auth_provider = AzureIdentityAuthenticationProvider(credentials, scopes)
36+
else:
37+
auth_provider = AzureIdentityAuthenticationProvider(credentials)
38+
4039
request_adapter = GraphRequestAdapter(auth_provider)
4140

42-
if base_url:
43-
request_adapter.base_url = base_url
44-
45-
4641
super().__init__(request_adapter)

samples/applications_samples.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ from msgraph import GraphServiceClient
1111
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
1212

1313
# Create a credential object. Used to authenticate requests
14-
credential=EnvironmentCredential()
14+
credentials = EnvironmentCredential()
1515
scopes = ['https://graph.microsoft.com/.default']
1616

1717
# Create an API client with the credentials and scopes
18-
client = GraphServiceClient(credentials, scopes=scopes)
18+
client = GraphServiceClient(credentials=credentials, scopes=scopes)
1919
```
2020

2121
## 1. LIST ALL APPLICATIONS IN THE TENANT (GET /applications)

samples/authentication_samples.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ credential = DeviceCodeCredential(
2121
scopes = ["User.Read"]
2222

2323
# Create an API client with the credentials and scopes.
24-
client = GraphServiceClient(credential, scopes=scopes)
24+
client = GraphServiceClient(credentials=credential, scopes=scopes)
2525

2626
# GET A USER USING THE USER ID (GET /users/{id})
2727
async def get_user():
@@ -51,7 +51,7 @@ credential = InteractiveBrowserCredential()
5151
scopes = ["User.Read"]
5252

5353
# Create an API client with the credentials and scopes.
54-
client = GraphServiceClient(credential, scopes=scopes)
54+
client = GraphServiceClient(credentials=credential, scopes=scopes)
5555

5656
# GET A USER USING THE USER ID (GET /users/{id})
5757
async def get_user():
@@ -87,7 +87,7 @@ credential = ClientSecretCredential(
8787
scopes = ['https://graph.microsoft.com/.default']
8888

8989
# Create an API client with the credentials and scopes.
90-
client = GraphServiceClient(credential, scopes=scopes)
90+
client = GraphServiceClient(credentials=credential, scopes=scopes)
9191

9292
# GET A USER USING THE USER ID (GET /users/{id})
9393
async def get_user():
@@ -117,7 +117,7 @@ credential = EnvironmentCredential()
117117
scopes = ['https://graph.microsoft.com/.default']
118118

119119
# Create an API client with the credentials and scopes.
120-
client = GraphServiceClient(credential, scopes=scopes)
120+
client = GraphServiceClient(credentials=credential, scopes=scopes)
121121

122122
# GET A USER USING THE USER ID (GET /users/{id})
123123
async def get_user():

samples/drives_samples.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ credential = ClientSecretCredential(
1919
scopes = ['https://graph.microsoft.com/.default']
2020

2121
# Create an API client with the credentials and scopes.
22-
client = GraphServiceClient(credentials, scopes=scopes)
23-
```
22+
client = GraphServiceClient(credentials=credential, scopes=scopes)
2423
```
2524

2625
## 1. LIST ALL DRIVES (GET /drives)

samples/general_samples.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ credentials = AuthorizationCodeCredential(
1818
redirect_uri: str
1919
)
2020
scopes = ['User.Read', 'Mail.ReadWrite']
21-
client = GraphServiceClient(credentials, scopes=scopes)
21+
client = GraphServiceClient(credentials=credentials, scopes=scopes)
2222
```
2323

2424
## 2. Creating a Graph client using a custom `httpx.AsyncClient` instance
@@ -28,7 +28,8 @@ from msgraph import GraphRequestAdapter
2828
from msgraph_core import GraphClientFactory
2929

3030
http_client = GraphClientFactory.create_with_default_middleware(client=httpx.AsyncClient())
31-
client = GraphServiceClient(credentials, scopes=scopes, client=http_client)
31+
request_adapter = GraphRequestAdapter(auth_provider, http_client)
32+
client = GraphServiceClient(request_adapter=request_adapter)
3233
```
3334

3435
## 3. Get an item from the Microsoft Graph API
@@ -159,7 +160,7 @@ credential = ClientSecretCredential(
159160
'client_secret'
160161
)
161162
scopes = ['Mail.Send']
162-
client = GraphServiceClient(credential, scopes=scopes)
163+
client = GraphServiceClient(credentials=credential, scopes=scopes)
163164

164165
async def send_mail():
165166
try:

samples/groups_samples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ credential = ClientSecretCredential(
1919
scopes = ['https://graph.microsoft.com/.default']
2020

2121
# Create an API client with the credentials and scopes.
22-
client = GraphServiceClient(credential, scopes=scopes)
22+
client = GraphServiceClient(credentials=credential, scopes=scopes)
2323
```
2424

2525
## 1. LIST ALL GROUPS IN THE TENANT (GET /groups)

samples/users_samples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ credential = ClientSecretCredential(
1919
scopes = ['https://graph.microsoft.com/.default']
2020

2121
# Create an API client with the credentials and scopes.
22-
client = GraphServiceClient(credential, scopes=scopes)
22+
client = GraphServiceClient(credentials=credential, scopes=scopes)
2323
```
2424

2525
## 1. GET ALL USERS IN A TENANT (GET /users)

0 commit comments

Comments
 (0)