-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
simple_graph_client.py
31 lines (24 loc) · 1 KB
/
simple_graph_client.py
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import json
from urllib.parse import urlparse, urljoin
from requests_oauthlib import OAuth2Session
AUTHORITY_URL = "https://login.microsoftonline.com/common"
RESOURCE = "https://graph.microsoft.com"
API_VERSION = "beta"
class SimpleGraphClient:
def __init__(self, token: str):
self.token = token
self.client = OAuth2Session(
token={"access_token": token, "token_type": "Bearer"}
)
async def get_me(self) -> {}:
response = self.client.get(self.api_endpoint("me"))
return json.loads(response.text)
def api_endpoint(self, url):
"""Convert a relative path such as /me/photo/$value to a full URI based
on the current RESOURCE and API_VERSION settings in config.py.
"""
if urlparse(url).scheme in ["http", "https"]:
return url # url is already complete
return urljoin(f"{RESOURCE}/{API_VERSION}/", url.lstrip("/"))