forked from microsoftgraph/msgraph-sdk-python-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_session.py
More file actions
118 lines (95 loc) · 4.55 KB
/
graph_session.py
File metadata and controls
118 lines (95 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
Graph Session
"""
from requests import Session
from msgraphcore.constants import BASE_URL, SDK_VERSION
from msgraphcore.middleware.middleware import MiddlewarePipeline, BaseMiddleware
from msgraphcore.middleware.abc_token_credential import TokenCredential
from msgraphcore.middleware.authorization import AuthorizationHandler
from msgraphcore.middleware.options.middleware_control import middleware_control
class GraphSession(Session):
"""Extends Session with Graph functionality
Extends Session by adding support for middleware options and middleware pipeline
"""
def __init__(self,
credential: TokenCredential,
scopes: [str] = ['.default'],
middleware: list = []
):
super().__init__()
self._append_sdk_version()
self._base_url = BASE_URL
auth_handler = AuthorizationHandler(credential, scopes)
# The authorization handler should be the first middleware in the pipeline.
middleware.insert(0, auth_handler)
self._register(middleware)
@middleware_control.get_middleware_options
def get(self, url: str, **kwargs):
r"""Sends a GET request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:rtype: requests.Response
"""
return super().get(self._graph_url(url))
@middleware_control.get_middleware_options
def post(self, url, data=None, json=None, **kwargs):
r"""Sends a POST request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param json: (optional) json to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:rtype: requests.Response
"""
return super().post(self._graph_url(url), data, json, **kwargs)
@middleware_control.get_middleware_options
def put(self, url, data=None, **kwargs):
r"""Sends a PUT request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:rtype: requests.Response
"""
return super().put(self._graph_url(url), data, **kwargs)
@middleware_control.get_middleware_options
def patch(self, url, data=None, **kwargs):
r"""Sends a PATCH request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:rtype: requests.Response
"""
return super().patch(self._graph_url(url), data, **kwargs)
@middleware_control.get_middleware_options
def delete(self, url, **kwargs):
r"""Sends a DELETE request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:rtype: requests.Response
"""
return super().delete(url, **kwargs)
def _graph_url(self, url: str) -> str:
"""Appends BASE_URL to user provided path
:param url: user provided path
:return: graph_url
"""
return self._base_url+url if (url[0] == '/') else url
def _register(self, middleware: [BaseMiddleware]) -> None:
"""Adds middleware to middleware_pipeline
:param middleware: list of middleware
"""
if middleware:
middleware_pipeline = MiddlewarePipeline()
for ware in middleware:
middleware_pipeline.add_middleware(ware)
self.mount('https://', middleware_pipeline)
def _append_sdk_version(self) -> None:
"""Updates sdkVersion in headers with comma-separated new values
"""
if 'sdkVersion' in self.headers:
self.headers.update({'sdkVersion': 'graph-python-' + SDK_VERSION + ', '
+ self.headers.get('sdkVersion')})
else:
self.headers.update({'sdkVersion': 'graph-python-' + SDK_VERSION})