77from typing import List , Optional
88
99import httpx
10- from kiota_abstractions .authentication import AccessTokenProvider
1110from kiota_http .kiota_client_factory import KiotaClientFactory
1211from kiota_http .middleware import AsyncKiotaTransport
1312from kiota_http .middleware .middleware import BaseMiddleware
1413
15- from .middleware import (
16- GraphAuthorizationHandler ,
17- GraphMiddlewarePipeline ,
18- GraphRedirectHandler ,
19- GraphRetryHandler ,
20- GraphTelemetryHandler ,
21- )
14+ from ._enums import APIVersion , NationalClouds
15+ from .middleware import GraphTelemetryHandler
16+
17+ DEFAULT_CONNECTION_TIMEOUT : int = 30
18+ DEFAULT_REQUEST_TIMEOUT : int = 100
2219
2320
2421class GraphClientFactory (KiotaClientFactory ):
@@ -28,31 +25,37 @@ class GraphClientFactory(KiotaClientFactory):
2825
2926 @staticmethod
3027 def create_with_default_middleware (
31- client : httpx . AsyncClient ,
32- token_provider : Optional [ AccessTokenProvider ] = None
28+ api_version : APIVersion = APIVersion . v1 ,
29+ host : NationalClouds = NationalClouds . Global
3330 ) -> httpx .AsyncClient :
3431 """Constructs native HTTP AsyncClient(httpx.AsyncClient) instances configured with
3532 a custom transport loaded with a default pipeline of middleware.
3633 Returns:
3734 httpx.AsycClient: An instance of the AsyncClient object
3835 """
36+ timeout = httpx .Timeout (DEFAULT_REQUEST_TIMEOUT , connect = DEFAULT_CONNECTION_TIMEOUT )
37+ client = httpx .AsyncClient (
38+ base_url = GraphClientFactory ._get_base_url (host , api_version ),
39+ timeout = timeout ,
40+ http2 = True
41+ )
3942 current_transport = client ._transport
40- middleware = GraphClientFactory ._get_common_middleware ()
41- if token_provider :
42- middleware .insert (0 , GraphAuthorizationHandler (token_provider ))
43-
44- middleware_pipeline = GraphClientFactory ._create_middleware_pipeline (
43+ middleware = KiotaClientFactory ._get_default_middleware ()
44+ middleware .append (GraphTelemetryHandler ())
45+ middleware_pipeline = KiotaClientFactory ._create_middleware_pipeline (
4546 middleware , current_transport
4647 )
4748
4849 client ._transport = AsyncKiotaTransport (
49- transport = current_transport , middleware = middleware_pipeline
50+ transport = current_transport , pipeline = middleware_pipeline
5051 )
5152 return client
5253
5354 @staticmethod
5455 def create_with_custom_middleware (
55- client : httpx .AsyncClient , middleware : Optional [List [BaseMiddleware ]]
56+ middleware : Optional [List [BaseMiddleware ]],
57+ api_version : APIVersion = APIVersion .v1 ,
58+ host : NationalClouds = NationalClouds .Global ,
5659 ) -> httpx .AsyncClient :
5760 """Applies a custom middleware chain to the HTTP Client
5861
@@ -61,34 +64,24 @@ def create_with_custom_middleware(
6164 a middleware pipeline. The middleware should be arranged in the order in which they will
6265 modify the request.
6366 """
67+ timeout = httpx .Timeout (DEFAULT_REQUEST_TIMEOUT , connect = DEFAULT_CONNECTION_TIMEOUT )
68+ client = httpx .AsyncClient (
69+ base_url = GraphClientFactory ._get_base_url (host , api_version ),
70+ timeout = timeout ,
71+ http2 = True
72+ )
6473 current_transport = client ._transport
65- middleware_pipeline = GraphClientFactory ._create_middleware_pipeline (
74+ middleware_pipeline = KiotaClientFactory ._create_middleware_pipeline (
6675 middleware , current_transport
6776 )
6877
6978 client ._transport = AsyncKiotaTransport (
70- transport = current_transport , middleware = middleware_pipeline
79+ transport = current_transport , pipeline = middleware_pipeline
7180 )
7281 return client
7382
7483 @staticmethod
75- def _get_common_middleware () -> List [BaseMiddleware ]:
76- """
77- Helper method that returns a list of cross cutting middleware
78- """
79- middleware = [GraphRedirectHandler (), GraphRetryHandler (), GraphTelemetryHandler ()]
80-
81- return middleware
82-
83- @staticmethod
84- def _create_middleware_pipeline (
85- middleware : Optional [List [BaseMiddleware ]], transport : httpx .AsyncBaseTransport
86- ) -> GraphMiddlewarePipeline :
87- """
88- Helper method that constructs a middleware_pipeline with the specified middleware
89- """
90- middleware_pipeline = GraphMiddlewarePipeline (transport )
91- if middleware :
92- for ware in middleware :
93- middleware_pipeline .add_middleware (ware )
94- return middleware_pipeline
84+ def _get_base_url (host : str , api_version : APIVersion ) -> str :
85+ """Helper method to set the complete base url"""
86+ base_url = f'{ host } /{ api_version } '
87+ return base_url
0 commit comments