Skip to content

Feat/graph session scopes #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,14 @@ device_credential = DeviceCodeCredential(

```

Create AuthorizationProvider, AuthorizationHandler and list of middleware
Create an authorization provider object and a list of scopes
```python
auth_provider = TokenCredentialAuthProvider(device_credential)
options = AuthMiddlewareOptions(['mail.send', 'user.read'])
auth_handler = AuthorizationHandler(auth_provider, auth_provider_options=options)

middleware = [
auth_handler
]
scopes = ['mail.send', 'user.read']
```

```python
graph_session = GraphSession(middleware=middleware)
graph_session = GraphSession(scopes, auth_provider)
result = graph_session.get('/me')
print(result.json())
```
Expand Down
13 changes: 10 additions & 3 deletions msgraphcore/graph_session.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
"""
Creates a session object
Graph Session
"""
from requests import Session, Request, Response

from msgraphcore.constants import BASE_URL, SDK_VERSION
from msgraphcore.middleware._middleware import MiddlewarePipeline, BaseMiddleware
from msgraphcore.middleware.options.auth_middleware_options import AuthMiddlewareOptions
from msgraphcore.middleware._base_auth import AuthProviderBase
from msgraphcore.middleware.authorization_handler import AuthorizationHandler


class GraphSession(Session):
"""
Extends session object with graph functionality
"""
def __init__(self, **kwargs):
def __init__(self, scopes: [str], auth_provider: AuthProviderBase, middleware: list = []):
super().__init__()
self.headers.update({'sdkVersion': 'graph-python-' + SDK_VERSION})
self._base_url = BASE_URL
middleware = kwargs.get('middleware')

options = AuthMiddlewareOptions(scopes)
auth_handler = AuthorizationHandler(auth_provider, auth_provider_options=options)

middleware.insert(0, auth_handler)
self._register(middleware)

def get(self, url: str, **kwargs) -> Response:
Expand Down
12 changes: 2 additions & 10 deletions tests/integration/test_middleware_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from msgraphcore.graph_session import GraphSession

from msgraphcore.middleware.authorization_provider import AuthProviderBase
from msgraphcore.middleware.authorization_handler import AuthorizationHandler
from msgraphcore.middleware.options.auth_middleware_options import AuthMiddlewareOptions


class MiddlewarePipelineTest(TestCase):
Expand All @@ -16,14 +14,8 @@ def test_middleware_pipeline(self):
url = 'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me'

auth_provider = _CustomAuthProvider()
options = AuthMiddlewareOptions(['user.read'])
auth_handler = AuthorizationHandler(auth_provider, auth_provider_options=options)

middleware = [
auth_handler
]

graph_session = GraphSession(middleware=middleware)
scopes = ['user.read']
graph_session = GraphSession(scopes, auth_provider)
result = graph_session.get(url)

self.assertEqual(result.status_code, 200)
Expand Down
15 changes: 9 additions & 6 deletions tests/unit/test_graph_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

from msgraphcore.graph_session import GraphSession
from msgraphcore.constants import BASE_URL, SDK_VERSION
from msgraphcore.middleware._base_auth import AuthProviderBase


class GraphSessionTest(TestCase):
def setUp(self) -> None:
self.requests = GraphSession()
self.auth_provider = _CustomAuthProvider()
self.requests = GraphSession(['user.read'], self.auth_provider)

def tearDown(self) -> None:
self.requests = None
Expand All @@ -25,11 +27,7 @@ def test_has_sdk_version_header(self):
self.assertEqual(self.requests.headers.get('sdkVersion'), 'graph-python-'+SDK_VERSION)

def test_initialized_with_middlewares(self):
middlewares = [
HTTPAdapter() # Middlewares inherit from the HTTPAdapter class
]

graph_session = GraphSession(middlewares=middlewares)
graph_session = GraphSession(['user.read'], self.auth_provider)
mocked_middleware = graph_session.get_adapter('https://')

self.assertIsInstance(mocked_middleware, HTTPAdapter)
Expand All @@ -54,3 +52,8 @@ def test_does_not_build_graph_urls_for_full_urls(self):

self.assertEqual(other_url, request_url)


class _CustomAuthProvider(AuthProviderBase):

def get_access_token(self):
return '{token:https://graph.microsoft.com/}'