Skip to content

Task/release cleanup #89

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 16 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from 12 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
8 changes: 3 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ jobs:
pipenv run isort .
- name: Lint with Pylint
run: |
pipenv run pylint msgraphcore --disable=W --rcfile=.pylintrc
- name: Test with unittest
pipenv run pylint msgraph --disable=W --rcfile=.pylintrc
- name: Test with pytest
run: |
pipenv run coverage run -m pytest tests/unit
pipenv run coverage run -m pytest tests/integration
pipenv run coverage html
pipenv run pytest
15 changes: 4 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,12 @@ repos:
name: pylint
stages: [commit]
language: system
entry: pipenv run pylint msgraphcore --disable=W --rcfile=.pylintrc
entry: pipenv run pylint msgraph --disable=W --rcfile=.pylintrc
types: [python]

- id: pytest-unit
name: pytest-unit
- id: pytest
name: pytest
stages: [commit]
language: system
entry: pipenv run coverage run -m pytest tests/unit
types: [python]

- id: pytest-integration
name: pytest-integration
stages: [commit]
language: system
entry: pipenv run coverage run -m pytest tests/integration
entry: pipenv run pytest tests
types: [python]
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ name = "pypi"

[packages] # Packages required to run the application
requests = "==2.23.0"
azure-identity = "==1.6.0"

[dev-packages] # Packages required to develop the application
coverage = "==5.0.3"
Expand All @@ -17,3 +16,4 @@ mypy = "==0.800"
pylint = "==2.7.4"
pytest = "==6.2.4"
pre-commit = "==2.13.0"
azure-identity = "*"
296 changes: 144 additions & 152 deletions Pipfile.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions msgraph/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# -----------------------------------
10 changes: 10 additions & 0 deletions msgraph/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
from ._client_factory import HTTPClientFactory
from ._constants import SDK_VERSION
from ._enums import APIVersion, NationalClouds
from ._graph_client import GraphClient

__version__ = SDK_VERSION
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import functools
from typing import Optional

from requests import Session

from msgraphcore.constants import CONNECTION_TIMEOUT, REQUEST_TIMEOUT
from msgraphcore.enums import APIVersion, NationalClouds
from msgraphcore.middleware.abc_token_credential import TokenCredential
from msgraphcore.middleware.authorization import AuthorizationHandler
from msgraphcore.middleware.middleware import BaseMiddleware, MiddlewarePipeline
from ._constants import CONNECTION_TIMEOUT, REQUEST_TIMEOUT
from ._enums import APIVersion, NationalClouds
from .middleware.abc_token_credential import TokenCredential
from .middleware.authorization import AuthorizationHandler
from .middleware.middleware import BaseMiddleware, MiddlewarePipeline


class HTTPClientFactory:
Expand Down
11 changes: 11 additions & 0 deletions msgraph/core/_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
"""
Application constants
"""
REQUEST_TIMEOUT = 100
CONNECTION_TIMEOUT = 30
BASE_URL = 'https://graph.microsoft.com/'
SDK_VERSION = '0.1.0'
4 changes: 4 additions & 0 deletions msgraphcore/enums.py → msgraph/core/_enums.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
#pylint: disable=invalid-name

from enum import Enum
Expand Down
12 changes: 6 additions & 6 deletions msgraphcore/graph_client.py → msgraph/core/_graph_client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import List, Optional

# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
from requests import Request, Session

from msgraphcore.client_factory import HTTPClientFactory
from msgraphcore.middleware.abc_token_credential import TokenCredential
from msgraphcore.middleware.middleware import BaseMiddleware
from msgraphcore.middleware.request_context import RequestContext
from ._client_factory import HTTPClientFactory
from .middleware.request_context import RequestContext

supported_options = ['scopes', 'custom_option']

Expand Down
4 changes: 4 additions & 0 deletions msgraph/core/middleware/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
11 changes: 11 additions & 0 deletions msgraph/core/middleware/abc_token_credential.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
from abc import ABC, abstractmethod


class TokenCredential(ABC):
@abstractmethod
def get_token(self, *scopes, **kwargs):
pass
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from msgraphcore.enums import FeatureUsageFlag
from msgraphcore.middleware.abc_token_credential import TokenCredential
from msgraphcore.middleware.middleware import BaseMiddleware
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
from .._enums import FeatureUsageFlag
from .abc_token_credential import TokenCredential
from .middleware import BaseMiddleware


class AuthorizationHandler(BaseMiddleware):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import ssl
import uuid

from requests.adapters import HTTPAdapter
from urllib3 import PoolManager

from msgraphcore.middleware.request_context import RequestContext
from .request_context import RequestContext


class MiddlewarePipeline(HTTPAdapter):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import uuid

from msgraphcore.enums import FeatureUsageFlag
from .._enums import FeatureUsageFlag


class RequestContext:
Expand Down
5 changes: 0 additions & 5 deletions msgraphcore/__init__.py

This file was deleted.

10 changes: 0 additions & 10 deletions msgraphcore/constants.py

This file was deleted.

7 changes: 0 additions & 7 deletions msgraphcore/middleware/abc_token_credential.py

This file was deleted.

10 changes: 8 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
[build-system]
requires = ["flit_core >=2,<3", "requests >= 2.23.0"]
requires = ["flit_core >=2,<4", "requests >= 2.23.0"]
build-backend = "flit_core.buildapi"

[tool.flit.metadata]
module = "msgraphcore"
module = "msgraph"
author = "Microsoft"
author-email = "[email protected]"
home-page="https://github.com/microsoftgraph/msgraph-sdk-python-core"
dist-name="msgraph-core"
requires=[
"requests >= 2.23.0",
]
requires-python=">=3.5"
classifiers = ["License :: OSI Approved :: MIT License"]
description-file = "README.md"

114 changes: 114 additions & 0 deletions samples/client_factory_samples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
#pylint: disable=undefined-variable
"""
Demonstrates using the HTTPClientFactory to create a client and make HTTP requests
to Microsoft Graph
"""
import json
from pprint import pprint

# This sample uses InteractiveBrowserCredential only for demonstration.
# Any azure-identity TokenCredential class will work the same.
from azure.identity import InteractiveBrowserCredential
from requests import Session

from msgraph.core import APIVersion, HTTPClientFactory, NationalClouds

scopes = ['user.read']
browser_credential = InteractiveBrowserCredential(client_id='YOUR_CLIENT_ID')

# Create client with default middleware
client = HTTPClientFactory().create_with_default_middleware(browser_credential)


def get_sample():
"""Sample HTTP GET request using the client"""
result = client.get(
'/users',
params={
'$select': 'displayName',
'$top': '10'
},
)
pprint(result.json())


def post_sample():
"""Sample HTTP POST request using the client"""
body = {
'message': {
'subject': 'Python SDK Meet for lunch?',
'body': {
'contentType': 'Text',
'content': 'The new cafeteria is open.'
},
'toRecipients': [{
'emailAddress': {
'address': 'ENTER_RECEPIENT_EMAIL_ADDRESS'
}
}]
}
}

result = client \
.post('/me/sendMail',
data=json.dumps(body),
scopes=['mail.send'],
headers={'Content-Type': 'application/json'}
)
pprint(result.status_code)


def client_with_custom_session_sample():
"""Sample client with a custom Session object"""
session = Session()
my_client = HTTPClientFactory(session=session
).create_with_default_middleware(browser_credential)
result = my_client.get('/me')
pprint(result.json())


def client_with_custom_settings_sample():
"""Sample client that makes requests against the beta api on a specified cloud endpoint"""
my_client = HTTPClientFactory(
credential=browser_credential,
api_version=APIVersion.beta,
cloud=NationalClouds.Germany,
).create_with_default_middleware(browser_credential)
result = my_client.get(
'/users',
params={
'$select': 'displayName',
'$top': '10'
},
)
pprint(result.json())


def client_with_custom_middleware():
"""Sample client with a custom middleware chain"""
middleware = [
CustomAuthorizationHandler(),
MyCustomMiddleware(),
]

my_client = HTTPClientFactory().create_with_custom_middleware(middleware)
result = my_client.get(
'https://graph.microsoft.com/v1.0/users',
params={
'$select': 'displayName',
'$top': '10'
},
)
pprint(result.json())


if __name__ == '__main__':
get_sample()
post_sample()
client_with_custom_session_sample()
client_with_custom_settings_sample()
client_with_custom_middleware()
Loading