2
2
# Copyright (c) Microsoft Corporation.
3
3
# Licensed under the MIT License.
4
4
# ------------------------------------
5
+ import json
6
+
5
7
from requests import Request , Session
6
8
7
9
from ._client_factory import HTTPClientFactory
25
27
]
26
28
27
29
28
- def attach_context (func ):
29
- """Attaches a request context object to every graph request """
30
+ def collect_options (func ):
31
+ """Collect middleware options into a middleware control dict and pass it as a header """
30
32
def wrapper (* args , ** kwargs ):
33
+
31
34
middleware_control = dict ()
32
35
33
36
for option in supported_options :
34
37
value = kwargs .pop (option , None )
35
38
if value :
36
39
middleware_control .update ({option : value })
37
40
38
- headers = kwargs .get ('headers' , {})
39
- request_context = RequestContext (middleware_control , headers )
40
-
41
- request = func (* args , ** kwargs )
42
- request .context = request_context
41
+ if 'headers' in kwargs .keys ():
42
+ kwargs ['headers' ].update ({'middleware_control' : json .dumps (middleware_control )})
43
+ else :
44
+ kwargs ['headers' ] = {'middleware_control' : json .dumps (middleware_control )}
43
45
44
- return request
46
+ return func ( * args , ** kwargs )
45
47
46
48
return wrapper
47
49
@@ -81,16 +83,34 @@ def __init__(self, **kwargs):
81
83
"""
82
84
self .graph_session = self .get_graph_session (** kwargs )
83
85
86
+ @collect_options
84
87
def get (self , url : str , ** kwargs ):
85
88
r"""Sends a GET request. Returns :class:`Response` object.
86
89
:param url: URL for the new :class:`Request` object.
87
90
:param \*\*kwargs: Optional arguments that ``request`` takes.
88
91
:rtype: requests.Response
89
92
"""
90
- prepared_request = self .prepare_request ('GET' , self ._graph_url (url ), ** kwargs )
91
- return self .graph_session .send (prepared_request )
93
+ return self .graph_session .get (self ._graph_url (url ), ** kwargs )
94
+
95
+ def options (self , url , ** kwargs ):
96
+ r"""Sends a OPTIONS request. Returns :class:`Response` object.
97
+ :param url: URL for the new :class:`Request` object.
98
+ :param \*\*kwargs: Optional arguments that ``request`` takes.
99
+ :rtype: requests.Response
100
+ """
101
+
102
+ return self .graph_session .options (self ._graph_url (url ), ** kwargs )
92
103
93
- def post (self , url , ** kwargs ):
104
+ def head (self , url , ** kwargs ):
105
+ r"""Sends a HEAD request. Returns :class:`Response` object.
106
+ :param url: URL for the new :class:`Request` object.
107
+ :param \*\*kwargs: Optional arguments that ``request`` takes.
108
+ :rtype: requests.Response
109
+ """
110
+
111
+ return self .graph_session .head (self ._graph_url (url ), ** kwargs )
112
+
113
+ def post (self , url , data = None , json = None , ** kwargs ):
94
114
r"""Sends a POST request. Returns :class:`Response` object.
95
115
:param url: URL for the new :class:`Request` object.
96
116
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
@@ -99,8 +119,7 @@ def post(self, url, **kwargs):
99
119
:param \*\*kwargs: Optional arguments that ``request`` takes.
100
120
:rtype: requests.Response
101
121
"""
102
- prepared_request = self .prepare_request ('POST' , self ._graph_url (url ), ** kwargs )
103
- return self .graph_session .send (prepared_request )
122
+ return self .graph_session .post (self ._graph_url (url ), data = data , json = json , ** kwargs )
104
123
105
124
def put (self , url , data = None , ** kwargs ):
106
125
r"""Sends a PUT request. Returns :class:`Response` object.
@@ -110,8 +129,8 @@ def put(self, url, data=None, **kwargs):
110
129
:param \*\*kwargs: Optional arguments that ``request`` takes.
111
130
:rtype: requests.Response
112
131
"""
113
- prepared_request = self . prepare_request ( 'PUT' , self . _graph_url ( url ), ** kwargs )
114
- return self .graph_session .send ( prepared_request )
132
+
133
+ return self .graph_session .put ( self . _graph_url ( url ), data = data , ** kwargs )
115
134
116
135
def patch (self , url , data = None , ** kwargs ):
117
136
r"""Sends a PATCH request. Returns :class:`Response` object.
@@ -121,17 +140,15 @@ def patch(self, url, data=None, **kwargs):
121
140
:param \*\*kwargs: Optional arguments that ``request`` takes.
122
141
:rtype: requests.Response
123
142
"""
124
- prepared_request = self .prepare_request ('PATCH' , self ._graph_url (url ), ** kwargs )
125
- return self .graph_session .send (prepared_request )
143
+ return self .graph_session .patch (self ._graph_url (url ), data = data , ** kwargs )
126
144
127
145
def delete (self , url , ** kwargs ):
128
146
r"""Sends a DELETE request. Returns :class:`Response` object.
129
147
:param url: URL for the new :class:`Request` object.
130
148
:param \*\*kwargs: Optional arguments that ``request`` takes.
131
149
:rtype: requests.Response
132
150
"""
133
- prepared_request = self .prepare_request ('DELETE' , self ._graph_url (url ), ** kwargs )
134
- return self .graph_session .send (prepared_request )
151
+ return self .graph_session .delete (self ._graph_url (url ), ** kwargs )
135
152
136
153
def _graph_url (self , url : str ) -> str :
137
154
"""Appends BASE_URL to user provided path
@@ -140,12 +157,6 @@ def _graph_url(self, url: str) -> str:
140
157
"""
141
158
return self .graph_session .base_url + url if (url [0 ] == '/' ) else url
142
159
143
- @attach_context
144
- def prepare_request (self , method , url , ** kwargs ):
145
- req = Request (method , url , ** kwargs )
146
- prepared = Session ().prepare_request (req )
147
- return prepared
148
-
149
160
@staticmethod
150
161
def get_graph_session (** kwargs ):
151
162
"""Method to always return a single instance of a HTTP Client"""
0 commit comments