Skip to content

Commit ca395d1

Browse files
committed
Use keyword arguments to pass config to the graph client and client factory
1 parent 13fb2fc commit ca395d1

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

msgraphcore/client_factory.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,17 @@ class HTTPClientFactory:
1515
Constructs HTTP Client(session) instances configured with either custom or default
1616
pipeline of middleware.
1717
"""
18-
def __init__(self, session: Optional[Session], **kwargs):
18+
def __init__(self, **kwargs):
1919
"""Class constructor that accepts a user provided session object and kwargs
2020
to configure the request handling behaviour of the client"""
21-
self.api_version = kwargs.get("api_version", APIVersion.v1)
21+
self.api_version = kwargs.get('api_version', APIVersion.v1)
2222
self.endpoint = kwargs.get('cloud', NationalClouds.Global)
2323
self.timeout = kwargs.get('timeout', (CONNECTION_TIMEOUT, REQUEST_TIMEOUT))
24-
self.base_url = self._get_base_url()
25-
if session:
26-
self.session = session
27-
else:
28-
self.session = Session()
29-
self._set_default_timeout()
24+
self.session = kwargs.get('session', Session())
25+
26+
self._get_base_url()
27+
self._set_default_timeout()
3028

31-
# should this be a class method
3229
def create_with_default_middleware(self, credential: TokenCredential, **kwargs) -> Session:
3330
"""Applies the default middleware chain to the HTTP Client"""
3431
middleware = [
@@ -46,7 +43,8 @@ def create_with_custom_middleware(self, middleware: [BaseMiddleware]) -> Session
4643

4744
def _get_base_url(self):
4845
"""Helper method to get the base url"""
49-
return self.endpoint + '/' + self.api_version
46+
base_url = self.endpoint + '/' + self.api_version
47+
self.session.base_url = base_url
5048

5149
def _set_default_timeout(self):
5250
"""Helper method to set a default timeout for the session

msgraphcore/graph_client.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import List, Optional
22

33
from requests import Session
44

@@ -10,15 +10,12 @@
1010

1111
class GraphClient:
1212
"""Constructs a custom HTTPClient to be used for requests against Microsoft Graph"""
13-
def __init__(
14-
self, credential: Optional[TokenCredential], session: Optional[Session],
15-
middleware: Optional[[BaseMiddleware]], **kwargs
16-
):
13+
def __init__(self, **kwargs):
1714
"""
1815
Class constructor that accepts a session object and kwargs to
1916
be passed to the HTTPClientFactory
2017
"""
21-
self.graph_session = get_graph_session(credential, session, middleware, **kwargs)
18+
self.graph_session = get_graph_session(**kwargs)
2219

2320
@middleware_control.get_middleware_options
2421
def get(self, url: str, **kwargs):
@@ -83,22 +80,21 @@ def _graph_url(self, url: str) -> str:
8380
_INSTANCE = None
8481

8582

86-
def get_graph_session(
87-
credential: Optional[TokenCredential], session: Optional[Session],
88-
middleware: Optional[[BaseMiddleware]], **kwargs
89-
):
83+
def get_graph_session(**kwargs):
9084
"""Method to always return a single instance of a HTTP Client"""
85+
9186
global _INSTANCE
9287

88+
credential = kwargs.get('credential')
89+
middleware = kwargs.get('middleware')
90+
9391
if credential and middleware:
9492
raise Exception("Invalid parameters! Both TokenCredential and middleware cannot be passed")
9593
if not credential and not middleware:
9694
raise ValueError("Invalid parameters!. Missing TokenCredential or middleware")
9795
if _INSTANCE is None:
9896
if credential:
99-
_INSTANCE = HTTPClientFactory(session,
100-
**kwargs).create_with_default_middleware(credential)
97+
_INSTANCE = HTTPClientFactory(**kwargs).create_with_default_middleware(credential)
10198
elif middleware:
102-
_INSTANCE = HTTPClientFactory(session,
103-
**kwargs).create_with_custom_middleware(middleware)
99+
_INSTANCE = HTTPClientFactory(**kwargs).create_with_custom_middleware(middleware)
104100
return _INSTANCE

0 commit comments

Comments
 (0)