Skip to content

Commit 65ab80c

Browse files
committed
Add search service
1 parent 3a06cbe commit 65ab80c

File tree

4 files changed

+131
-3
lines changed

4 files changed

+131
-3
lines changed

ms_graph/client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ms_graph.notes import Notes
1616
from ms_graph.session import GraphSession
1717
from ms_graph.drive_items import DriveItems
18+
from ms_graph.search import Search
1819

1920

2021
class MicrosoftGraphClient():
@@ -399,3 +400,17 @@ def notes(self) -> Notes:
399400
notes_object: Notes = Notes(session=self.graph_session)
400401

401402
return notes_object
403+
404+
def search(self) -> Search:
405+
"""Used to access the Search Services and metadata.
406+
407+
### Returns
408+
---
409+
Groups:
410+
The `Search` services Object.
411+
"""
412+
413+
# Grab the `Search` Object for the session.
414+
search_object: Search = Search(session=self.graph_session)
415+
416+
return search_object

ms_graph/search.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from typing import Dict
2+
from ms_graph.session import GraphSession
3+
4+
5+
class Search():
6+
7+
"""
8+
## Overview:
9+
----
10+
You can use the Microsoft Search API to query Microsoft 365 data in your apps.
11+
Search requests run in the context of the signed-in user, identified using an
12+
access token with delegated permissions.
13+
"""
14+
15+
def __init__(self, session: object) -> None:
16+
"""Initializes the `Query` object.
17+
18+
### Parameters
19+
----
20+
session : object
21+
An authenticated session for our Microsoft Graph Client.
22+
"""
23+
24+
# Set the session.
25+
self.graph_session: GraphSession = session
26+
27+
# Set the endpoint.
28+
self.endpoint = 'search'
29+
30+
def query(self, search_request: Dict) -> Dict:
31+
"""Runs the query specified in the request body. Search
32+
results are provided in the response.
33+
34+
### Returns
35+
----
36+
Dict:
37+
A `SearchResponse` collection.
38+
"""
39+
40+
# define the endpoints.
41+
endpoint = self.endpoint + "/query"
42+
43+
content = self.graph_session.make_request(
44+
method='post',
45+
endpoint=endpoint,
46+
json=search_request
47+
)
48+
49+
return content

samples/use_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
'User.Read.All',
1212
'Directory.Read.All',
1313
'Directory.ReadWrite.All',
14-
'offline_access',
15-
'openid',
16-
'profile'
14+
# 'offline_access',
15+
# 'openid',
16+
# 'profile'
1717
]
1818

1919
# Initialize the Parser.

samples/use_search_service.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from pprint import pprint
2+
from ms_graph.client import MicrosoftGraphClient
3+
from configparser import ConfigParser
4+
5+
scopes = [
6+
'Calendars.ReadWrite',
7+
'Files.ReadWrite.All',
8+
'User.ReadWrite.All',
9+
'Notes.ReadWrite.All',
10+
'Directory.ReadWrite.All',
11+
'User.Read.All',
12+
'Directory.Read.All',
13+
'Directory.ReadWrite.All',
14+
'Mail.ReadWrite',
15+
'Sites.ReadWrite.All',
16+
'ExternalItem.Read.All'
17+
]
18+
19+
# Initialize the Parser.
20+
config = ConfigParser()
21+
22+
# Read the file.
23+
config.read('config/config.ini')
24+
25+
# Get the specified credentials.
26+
client_id = config.get('graph_api', 'client_id')
27+
client_secret = config.get('graph_api', 'client_secret')
28+
redirect_uri = config.get('graph_api', 'redirect_uri')
29+
30+
# Initialize the Client.
31+
graph_client = MicrosoftGraphClient(
32+
client_id=client_id,
33+
client_secret=client_secret,
34+
redirect_uri=redirect_uri,
35+
scope=scopes,
36+
credentials='config/ms_graph_state.jsonc'
37+
)
38+
39+
# Login to the Client.
40+
graph_client.login()
41+
42+
# Grab the Search Service.
43+
search_service = graph_client.search()
44+
45+
# Search for some documents.
46+
search_response = search_service.query(
47+
search_request={
48+
"requests": [
49+
{
50+
"entityTypes": [
51+
"message"
52+
],
53+
"query": {
54+
"queryString": "sigma"
55+
},
56+
"from": 0,
57+
"size": 25
58+
}
59+
]
60+
}
61+
)
62+
63+
# Print the Output.
64+
pprint(search_response)

0 commit comments

Comments
 (0)