99from kiota_abstractions .authentication import AccessTokenProvider
1010from kiota_http .middleware .middleware import BaseMiddleware
1111
12+ from msgraph .core import middleware
13+
1214from ._enums import APIVersion , NationalClouds
1315from .graph_client_factory import GraphClientFactory
1416
@@ -36,23 +38,21 @@ def wrapper(*args, **kwargs):
3638class GraphClient :
3739 """Constructs a custom HTTPClient to be used for requests against Microsoft Graph
3840
39- :keyword token_provider: AccessTokenProvider used to acquire an access token for the Microsoft
40- Graph API. Created through one of the credential classes from `azure.identity`
41- :keyword list middleware: Custom middleware list that will be used to create
41+ Args:
42+ token_provider (AccessTokenProvider): Used to acquire an access token for the
43+ Microsoft Graph API.
44+ api_version (APIVersion): The Microsoft Graph API version to be used, for example
45+ `APIVersion.v1` (default). This value is used in setting
46+ the base url for all requests for that session.
47+ base_url (NationalClouds): a supported Microsoft Graph cloud endpoint.
48+ timeout (httpx.Timeout):Default connection and read timeout values for all session
49+ requests.Specify a tuple in the form of httpx.Timeout(
50+ REQUEST_TIMEOUT, connect=CONNECTION_TIMEOUT),
51+ client (Optional[httpx.AsyncClient]): A custom AsynClient instance from the
52+ python httpx library
53+ middleware (BaseMiddlware): Custom middleware list that will be used to create
4254 a middleware pipeline. The middleware should be arranged in the order in which they will
4355 modify the request.
44- :keyword enum api_version: The Microsoft Graph API version to be used, for example
45- `APIVersion.v1` (default). This value is used in setting the base url for all requests for
46- that session.
47- :class:`~msgraphcore.enums.APIVersion` defines valid API versions.
48- :keyword enum base_url: a supported Microsoft Graph cloud endpoint.
49- Defaults to `NationalClouds.Global`
50- :class:`~msgraphcore.enums.NationalClouds` defines supported sovereign clouds.
51- :keyword tuple timeout: Default connection and read timeout values for all session requests.
52- Specify a tuple in the form of Tuple(connect_timeout, read_timeout) if you would like to set
53- the values separately. If you specify a single value for the timeout, the timeout value will
54- be applied to both the connect and the read timeouts.
55- :keyword client: A custom client instance from the python httpx library
5656 """
5757 DEFAULT_CONNECTION_TIMEOUT : int = 30
5858 DEFAULT_REQUEST_TIMEOUT : int = 100
@@ -78,7 +78,7 @@ def __init__(
7878 Class constructor that accepts a session object and kwargs to
7979 be passed to the GraphClientFactory
8080 """
81- self .client = self .get_graph_client (
81+ self .client = self ._get_graph_client (
8282 token_provider , api_version , base_url , timeout , client , middleware
8383 )
8484
@@ -272,26 +272,30 @@ async def delete(
272272 url , params = params , headers = headers , cookies = cookies , extensions = extensions
273273 )
274274
275- @staticmethod
276- def get_graph_client (
277- token_provider : Optional [AccessTokenProvider ],
278- api_version : APIVersion ,
279- base_url : NationalClouds ,
280- timeout : httpx .Timeout ,
281- client : Optional [httpx .AsyncClient ],
282- middleware : Optional [List [BaseMiddleware ]],
275+ def _get_graph_client (
276+ self , token_provider : Optional [AccessTokenProvider ], api_version : APIVersion ,
277+ base_url : NationalClouds , timeout : httpx .Timeout , client : Optional [httpx .AsyncClient ],
278+ middleware : Optional [List [BaseMiddleware ]]
283279 ):
284280 """Method to always return a single instance of a HTTP Client"""
285-
281+ if not client :
282+ client = httpx .AsyncClient (
283+ base_url = self ._get_base_url (base_url , api_version ), timeout = timeout , http2 = True
284+ )
286285 if token_provider and middleware :
287286 raise ValueError (
288287 "Invalid parameters! Both TokenCredential and middleware cannot be passed"
289288 )
290- if not token_provider and not middleware :
291- raise ValueError ("Invalid parameters!. Missing TokenCredential or middleware" )
292289
293- if token_provider and not middleware :
294- return GraphClientFactory (api_version , base_url , timeout ,
295- client ).create_with_default_middleware (token_provider )
296- return GraphClientFactory (api_version , base_url , timeout ,
297- client ).create_with_custom_middleware (middleware )
290+ if middleware :
291+ return GraphClientFactory .create_with_custom_middleware (
292+ client = client , middleware = middleware
293+ )
294+ return GraphClientFactory .create_with_default_middleware (
295+ client = client , token_provider = token_provider
296+ )
297+
298+ def _get_base_url (self , base_url : str , api_version : APIVersion ) -> str :
299+ """Helper method to set the complete base url"""
300+ base_url = f'{ base_url } /{ api_version } '
301+ return base_url
0 commit comments