Skip to content

Commit d07932a

Browse files
committed
Replace GraphSession with GraphClient
1 parent 80d2c6a commit d07932a

File tree

2 files changed

+43
-67
lines changed

2 files changed

+43
-67
lines changed

msgraphcore/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
"""msgraph-core"""
2-
3-
from msgraphcore.graph_session import GraphSession
4-
51
from .constants import SDK_VERSION
62

73
__version__ = SDK_VERSION
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,104 @@
1-
"""
2-
Graph Session
3-
"""
1+
from typing import Optional
2+
43
from requests import Session
54

6-
from msgraphcore.constants import BASE_URL, SDK_VERSION
5+
from msgraphcore.client_factory import HTTPClientFactory
76
from msgraphcore.middleware.abc_token_credential import TokenCredential
8-
from msgraphcore.middleware.authorization import AuthorizationHandler
9-
from msgraphcore.middleware.middleware import BaseMiddleware, MiddlewarePipeline
7+
from msgraphcore.middleware.middleware import BaseMiddleware
108
from msgraphcore.middleware.options.middleware_control import middleware_control
119

1210

13-
class GraphSession(Session):
14-
"""Extends Session with Graph functionality
15-
16-
Extends Session by adding support for middleware options and middleware pipeline
17-
"""
11+
class GraphClient:
12+
"""Constructs a custom HTTPClient to be used for requests against Microsoft Graph"""
1813
def __init__(
19-
self,
20-
credential: TokenCredential,
21-
scopes: [str] = ['.default'],
22-
middleware: list = [],
23-
api_version: str = 'v1.0'
14+
self, credential: Optional[TokenCredential], session: Optional[Session],
15+
middleware: Optional[[BaseMiddleware]], **kwargs
2416
):
25-
super().__init__()
26-
self._append_sdk_version()
27-
self._base_url = BASE_URL + '/' + api_version
28-
29-
auth_handler = AuthorizationHandler(credential, scopes)
30-
31-
# The authorization handler should be the first middleware in the pipeline.
32-
middleware.insert(0, auth_handler)
33-
self._register(middleware)
17+
"""
18+
Class constructor that accepts a session object and kwargs to
19+
be passed to the HTTPClientFactory
20+
"""
21+
self.graph_session = get_graph_session(credential, session, middleware, **kwargs)
3422

3523
@middleware_control.get_middleware_options
3624
def get(self, url: str, **kwargs):
3725
r"""Sends a GET request. Returns :class:`Response` object.
38-
3926
:param url: URL for the new :class:`Request` object.
4027
:param \*\*kwargs: Optional arguments that ``request`` takes.
4128
:rtype: requests.Response
4229
"""
43-
return super().get(self._graph_url(url), **kwargs)
30+
return self.graph_session.get(self._graph_url(url), **kwargs)
4431

4532
@middleware_control.get_middleware_options
4633
def post(self, url, data=None, json=None, **kwargs):
4734
r"""Sends a POST request. Returns :class:`Response` object.
48-
4935
:param url: URL for the new :class:`Request` object.
5036
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
5137
object to send in the body of the :class:`Request`.
5238
:param json: (optional) json to send in the body of the :class:`Request`.
5339
:param \*\*kwargs: Optional arguments that ``request`` takes.
5440
:rtype: requests.Response
5541
"""
56-
return super().post(self._graph_url(url), data, json, **kwargs)
42+
return self.graph_session.post(self._graph_url(url), data, json, **kwargs)
5743

5844
@middleware_control.get_middleware_options
5945
def put(self, url, data=None, **kwargs):
6046
r"""Sends a PUT request. Returns :class:`Response` object.
61-
6247
:param url: URL for the new :class:`Request` object.
6348
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
6449
object to send in the body of the :class:`Request`.
6550
:param \*\*kwargs: Optional arguments that ``request`` takes.
6651
:rtype: requests.Response
6752
"""
68-
return super().put(self._graph_url(url), data, **kwargs)
53+
return self.graph_session.put(self._graph_url(url), data, **kwargs)
6954

7055
@middleware_control.get_middleware_options
7156
def patch(self, url, data=None, **kwargs):
7257
r"""Sends a PATCH request. Returns :class:`Response` object.
73-
7458
:param url: URL for the new :class:`Request` object.
7559
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
7660
object to send in the body of the :class:`Request`.
7761
:param \*\*kwargs: Optional arguments that ``request`` takes.
7862
:rtype: requests.Response
7963
"""
80-
return super().patch(self._graph_url(url), data, **kwargs)
64+
return self.graph_session.patch(self._graph_url(url), data, **kwargs)
8165

8266
@middleware_control.get_middleware_options
8367
def delete(self, url, **kwargs):
8468
r"""Sends a DELETE request. Returns :class:`Response` object.
85-
8669
:param url: URL for the new :class:`Request` object.
8770
:param \*\*kwargs: Optional arguments that ``request`` takes.
8871
:rtype: requests.Response
8972
"""
90-
return super().delete(self._graph_url(url), **kwargs)
73+
return self.graph_session.delete(self._graph_url(url), **kwargs)
9174

9275
def _graph_url(self, url: str) -> str:
9376
"""Appends BASE_URL to user provided path
94-
9577
:param url: user provided path
9678
:return: graph_url
9779
"""
98-
return self._base_url + url if (url[0] == '/') else url
99-
100-
def _register(self, middleware: [BaseMiddleware]) -> None:
101-
"""Adds middleware to middleware_pipeline
102-
103-
:param middleware: list of middleware
104-
"""
105-
if middleware:
106-
middleware_pipeline = MiddlewarePipeline()
107-
108-
for ware in middleware:
109-
middleware_pipeline.add_middleware(ware)
110-
111-
self.mount('https://', middleware_pipeline)
112-
113-
def _append_sdk_version(self) -> None:
114-
"""Updates sdkVersion in headers with comma-separated new values
115-
"""
116-
if 'sdkVersion' in self.headers:
117-
self.headers.update(
118-
{
119-
'sdkVersion':
120-
'graph-python-' + SDK_VERSION + ', ' + self.headers.get('sdkVersion')
121-
}
122-
)
123-
else:
124-
self.headers.update({'sdkVersion': 'graph-python-' + SDK_VERSION})
80+
return self.graph_session.base_url + url if (url[0] == '/') else url
81+
82+
83+
_INSTANCE = None
84+
85+
86+
def get_graph_session(
87+
credential: Optional[TokenCredential], session: Optional[Session],
88+
middleware: Optional[[BaseMiddleware]], **kwargs
89+
):
90+
"""Method to always return a single instance of a HTTP Client"""
91+
global _INSTANCE
92+
93+
if credential and middleware:
94+
raise Exception("Invalid parameters! Both TokenCredential and middleware cannot be passed")
95+
if not credential and not middleware:
96+
raise ValueError("Invalid parameters!. Missing TokenCredential or middleware")
97+
if _INSTANCE is None:
98+
if credential:
99+
_INSTANCE = HTTPClientFactory(session,
100+
**kwargs).create_with_default_middleware(credential)
101+
elif middleware:
102+
_INSTANCE = HTTPClientFactory(session,
103+
**kwargs).create_with_custom_middleware(middleware)
104+
return _INSTANCE

0 commit comments

Comments
 (0)