Skip to content

Commit ffb0495

Browse files
committed
Update graph client factory and adapter
1 parent 7cd67e4 commit ffb0495

File tree

2 files changed

+34
-44
lines changed

2 files changed

+34
-44
lines changed

msgraph/core/graph_client_factory.py

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@
77
from typing import List, Optional
88

99
import httpx
10-
from kiota_abstractions.authentication import AccessTokenProvider
1110
from kiota_http.kiota_client_factory import KiotaClientFactory
1211
from kiota_http.middleware import AsyncKiotaTransport
1312
from 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

2421
class 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

msgraph/core/graph_request_adapter.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1+
import httpx
12
from kiota_abstractions.authentication import AuthenticationProvider
23
from kiota_abstractions.serialization import (
3-
Parsable,
4-
ParsableFactory,
5-
ParseNode,
64
ParseNodeFactory,
75
ParseNodeFactoryRegistry,
86
SerializationWriterFactory,
97
SerializationWriterFactoryRegistry,
108
)
119
from kiota_http.httpx_request_adapter import HttpxRequestAdapter
1210

13-
from .graph_client import GraphClient
1411
from .graph_client_factory import GraphClientFactory
1512

1613

@@ -22,7 +19,7 @@ def __init__(
2219
parse_node_factory: ParseNodeFactory = ParseNodeFactoryRegistry(),
2320
serialization_writer_factory:
2421
SerializationWriterFactory = SerializationWriterFactoryRegistry(),
25-
http_client: GraphClient = GraphClient()
22+
http_client: httpx.AsyncClient = GraphClientFactory.create_with_default_middleware()
2623
) -> None:
2724
super().__init__(
2825
authentication_provider=authentication_provider,

0 commit comments

Comments
 (0)