7
7
from typing import List , Optional
8
8
9
9
import httpx
10
- from kiota_abstractions .authentication import AccessTokenProvider
11
10
from kiota_http .kiota_client_factory import KiotaClientFactory
12
11
from kiota_http .middleware import AsyncKiotaTransport
13
12
from kiota_http .middleware .middleware import BaseMiddleware
14
13
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
22
19
23
20
24
21
class GraphClientFactory (KiotaClientFactory ):
@@ -28,31 +25,37 @@ class GraphClientFactory(KiotaClientFactory):
28
25
29
26
@staticmethod
30
27
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
33
30
) -> httpx .AsyncClient :
34
31
"""Constructs native HTTP AsyncClient(httpx.AsyncClient) instances configured with
35
32
a custom transport loaded with a default pipeline of middleware.
36
33
Returns:
37
34
httpx.AsycClient: An instance of the AsyncClient object
38
35
"""
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
+ )
39
42
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 (
45
46
middleware , current_transport
46
47
)
47
48
48
49
client ._transport = AsyncKiotaTransport (
49
- transport = current_transport , middleware = middleware_pipeline
50
+ transport = current_transport , pipeline = middleware_pipeline
50
51
)
51
52
return client
52
53
53
54
@staticmethod
54
55
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 ,
56
59
) -> httpx .AsyncClient :
57
60
"""Applies a custom middleware chain to the HTTP Client
58
61
@@ -61,34 +64,24 @@ def create_with_custom_middleware(
61
64
a middleware pipeline. The middleware should be arranged in the order in which they will
62
65
modify the request.
63
66
"""
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
+ )
64
73
current_transport = client ._transport
65
- middleware_pipeline = GraphClientFactory ._create_middleware_pipeline (
74
+ middleware_pipeline = KiotaClientFactory ._create_middleware_pipeline (
66
75
middleware , current_transport
67
76
)
68
77
69
78
client ._transport = AsyncKiotaTransport (
70
- transport = current_transport , middleware = middleware_pipeline
79
+ transport = current_transport , pipeline = middleware_pipeline
71
80
)
72
81
return client
73
82
74
83
@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