Skip to content

Commit 0d22012

Browse files
committed
Document middleware
1 parent cedaea4 commit 0d22012

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

msgraphcore/graph_session.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def __init__(self, auth_provider: AuthProviderBase, middleware: list = []):
2525

2626
auth_handler = AuthorizationHandler(auth_provider)
2727

28+
# The authorization handler should be the first middleware in the pipeline.
2829
middleware.insert(0, auth_handler)
2930
self._register(middleware)
3031

@@ -87,7 +88,7 @@ def delete(self, url, **kwargs):
8788

8889
def _graph_url(self, url: str) -> str:
8990
"""Appends BASE_URL to user provided path
90-
91+
9192
:param url: user provided path
9293
:return: graph_url
9394
"""

msgraphcore/middleware/_middleware.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66

77
class MiddlewarePipeline(HTTPAdapter):
8+
"""MiddlewarePipeline, entry point of middleware
9+
10+
The pipeline is implemented as a linked-list, read more about
11+
it here https://buffered.dev/middleware-python-requests/
12+
"""
813
def __init__(self):
914
super().__init__()
1015
self._middleware = None
@@ -27,6 +32,12 @@ def _middleware_present(self):
2732

2833

2934
class BaseMiddleware(HTTPAdapter):
35+
"""Base class for middleware
36+
37+
Handles moving a Request to the next middleware in the pipeline.
38+
If the current middleware is the last one in the pipeline, it
39+
makes a network request
40+
"""
3041
def __init__(self):
3142
super().__init__()
3243
self.next = None

msgraphcore/middleware/authorization.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ def __init__(self, auth_provider: AuthProviderBase):
1111
self.retry_count = 0
1212

1313
def send(self, request, **kwargs):
14+
# Checks if there're any options for this middleware
1415
options = self._get_middleware_options()
16+
# If there is, get the scopes from the options
1517
if options:
1618
self.auth_provider.scopes = options.scopes
1719

1820
token = self.auth_provider.get_access_token()
1921
request.headers.update({'Authorization': 'Bearer {}'.format(token)})
2022
response = super().send(request, **kwargs)
2123

24+
# Token might have expired just before transmission, retry the request
2225
if response.status_code == 401 and self.retry_count < 2:
2326
self.retry_count += 1
2427
return self.send(request, **kwargs)

0 commit comments

Comments
 (0)