forked from microsoftgraph/msgraph-sdk-python-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.py
More file actions
48 lines (37 loc) · 1.42 KB
/
middleware.py
File metadata and controls
48 lines (37 loc) · 1.42 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
import ssl
from urllib3 import PoolManager
from requests.adapters import HTTPAdapter
class MiddlewarePipeline(HTTPAdapter):
"""MiddlewarePipeline, entry point of middleware
The pipeline is implemented as a linked-list, read more about
it here https://buffered.dev/middleware-python-requests/
"""
def __init__(self):
super().__init__()
self._middleware = None
self.poolmanager = PoolManager(ssl_version=ssl.PROTOCOL_TLSv1_2)
def add_middleware(self, middleware):
if self._middleware_present():
self._middleware.next = middleware
else:
self._middleware = middleware
def send(self, request, **kwargs):
if self._middleware_present():
return self._middleware.send(request, **kwargs)
# No middleware in pipeline, call superclass' send
return super().send(request, **kwargs)
def _middleware_present(self):
return self._middleware
class BaseMiddleware(HTTPAdapter):
"""Base class for middleware
Handles moving a Request to the next middleware in the pipeline.
If the current middleware is the last one in the pipeline, it
makes a network request
"""
def __init__(self):
super().__init__()
self.next = None
def send(self, request, **kwargs):
if self.next is None:
return super().send(request, **kwargs)
return self.next.send(request, **kwargs)