-
Notifications
You must be signed in to change notification settings - Fork 784
/
simple_graph_client.py
53 lines (44 loc) · 1.79 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import json
from urllib.parse import urlparse, urljoin, quote
from requests_oauthlib import OAuth2Session
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 search_mail_inbox(self, search_query: str):
query = quote(search_query) # cleans up the url
# Searches the user's mail Inbox using the Microsoft Graph API
# https://graph.microsoft.com/beta/search/query
search_filter = {
"requests": [
{
"entityTypes": ["microsoft.graph.message"],
"query": {"query_string": {"query": query}},
"from": 0,
"size": 20,
}
]
}
response = self.client.post(
self.api_endpoint(f"search/query"),
headers={"Content-Type": "application/json"},
json=search_filter,
)
response_json = json.loads(response.text)
total_results = response_json["value"][0]["hitsContainers"][0]["total"]
if int(total_results) > 0:
return response_json["value"][0]["hitsContainers"][0]["hits"]
return {}
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("/"))