Skip to content

Commit ac4c2f5

Browse files
committed
Refactor client instantiation logic
1 parent 386efec commit ac4c2f5

1 file changed

Lines changed: 41 additions & 1 deletion

File tree

‎msgraph/graph_service_client.py‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,46 @@
1+
from typing import List, Optional, TYPE_CHECKING, Union'
2+
from azure.core.credentials import TokenCredential
3+
from azure.core.credentials_async import AsyncTokenCredential
4+
from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider
5+
from httpx import AsyncClient
6+
17
from .generated.base_graph_service_client import BaseGraphServiceClient
28
from .graph_request_adapter import GraphRequestAdapter
39

410
class GraphServiceClient(BaseGraphServiceClient):
5-
def __init__(self, request_adapter: GraphRequestAdapter) -> None:
11+
def __init__(
12+
self,
13+
credentials: Union[TokenCredential, AsyncTokenCredential],
14+
scopes: Optional[List[str]],
15+
base_url: Optional[str] = None,
16+
client: Optional[AsyncClient] = None
17+
) -> None:
18+
"""Constructs a client instance to use for making requests to the
19+
Microsoft Graph v.1.0 API.
20+
21+
Args:
22+
credentials (Union[TokenCredential, AsyncTokenCredential]): The
23+
tokenCredential to use for authentication.
24+
scopes (Optional[List[str]]): The scopes to use for authentication.
25+
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.
30+
"""
31+
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:
40+
request_adapter = GraphRequestAdapter(auth_provider)
41+
42+
if base_url:
43+
request_adapter.base_url = base_url
44+
45+
646
super().__init__(request_adapter)

0 commit comments

Comments
 (0)