Skip to content

Commit a4202d2

Browse files
committed
add notebook services
1 parent 191a14b commit a4202d2

File tree

4 files changed

+350
-0
lines changed

4 files changed

+350
-0
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.users import Users
1616
from ms_graph.drives import Drives
1717
from ms_graph.groups import Groups
18+
from ms_graph.notes import Notes
1819
from ms_graph.session import GraphSession
1920

2021
from urllib.parse import urlencode, urlparse, quote_plus
@@ -367,3 +368,17 @@ def groups(self) -> Groups:
367368
groups_object: Groups = Groups(session=self.graph_session)
368369

369370
return groups_object
371+
372+
def notes(self) -> Notes:
373+
"""Used to access the OneNotes Services and metadata.
374+
375+
### Returns
376+
---
377+
Groups:
378+
The `Notes` services Object.
379+
"""
380+
381+
# Grab the `Notes` Object for the session.
382+
notes_object: Notes = Notes(session=self.graph_session)
383+
384+
return notes_object

ms_graph/notes.py

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
from typing import List
2+
from typing import Dict
3+
from typing import Union
4+
5+
from ms_graph.session import GraphSession
6+
7+
8+
class Notes():
9+
10+
"""
11+
## Overview:
12+
----
13+
Microsoft Graph lets your app get authorized access to a user's
14+
OneNote notebooks, sections, and pages in a personal or organization
15+
account. With the appropriate delegated or application permissions,
16+
your app can access the OneNote data of the signed-in user or any
17+
user in a tenant.
18+
"""
19+
20+
def __init__(self, session: object) -> None:
21+
"""Initializes the `Notes` object.
22+
23+
### Parameters
24+
----
25+
session : object
26+
An authenticated session for our Microsoft Graph Client.
27+
"""
28+
29+
# Set the session.
30+
self.graph_session: GraphSession = session
31+
32+
# Set the endpoint.
33+
self.endpoint = 'onenote'
34+
35+
def list_my_notebooks(self) -> Dict:
36+
"""Retrieve a list of your notebook objects.
37+
38+
### Returns
39+
----
40+
Dict:
41+
A List of `Notebook` Resource Object.
42+
"""
43+
44+
# define the endpoints.
45+
endpoint = "me/" + self.endpoint + "/notebooks"
46+
47+
content = self.graph_session.make_request(
48+
method='get',
49+
endpoint=endpoint
50+
)
51+
52+
return content
53+
54+
def list_user_notebooks(self, user_id: str) -> Dict:
55+
"""Retrieve a list of notebook objects.
56+
57+
### Parameters
58+
----
59+
user_id (str): The User's ID that is assoicated with
60+
their Graph account.
61+
62+
### Returns
63+
----
64+
Dict:
65+
A List of `Notebook` Resource Object.
66+
"""
67+
68+
# Define the endpoint.
69+
endpoint = "users/{user_id}".format(user_id=user_id) + \
70+
self.endpoint + "/notebooks"
71+
72+
content = self.graph_session.make_request(
73+
method='get',
74+
endpoint=endpoint
75+
)
76+
77+
return content
78+
79+
def list_group_notebooks(self, group_id: str) -> Dict:
80+
"""Retrieve a list of notebook objects.
81+
82+
### Parameters
83+
----
84+
group_id (str): The Group ID that you want to pull
85+
notebooks for.
86+
87+
### Returns
88+
----
89+
Dict:
90+
A List of `Notebook` Resource Object.
91+
"""
92+
93+
# Define the endpoint.
94+
endpoint = "groups/{group_id}".format(
95+
group_id=group_id) + self.endpoint + "/notebooks"
96+
97+
content = self.graph_session.make_request(
98+
method='get',
99+
endpoint=endpoint
100+
)
101+
102+
return content
103+
104+
def list_site_notebooks(self, site_id: str) -> Dict:
105+
"""Retrieve a list of notebook objects.
106+
107+
### Parameters
108+
----
109+
site_id (str): The Site ID that you want to pull
110+
notebooks for.
111+
112+
### Returns
113+
----
114+
Dict:
115+
A List of `Notebook` Resource Object.
116+
"""
117+
118+
# Define the endpoint.
119+
endpoint = "sites/{site_id}".format(site_id=site_id) + \
120+
self.endpoint + "/notebooks"
121+
122+
content = self.graph_session.make_request(
123+
method='get',
124+
endpoint=endpoint
125+
)
126+
127+
return content
128+
129+
def get_my_notebook(self, notebook_id: str) -> Dict:
130+
"""Retrieve a list of notebook objects.
131+
132+
### Parameters
133+
----
134+
notebook_id (str): The User's Notebook ID that you
135+
want to pull.
136+
137+
### Returns
138+
----
139+
Dict:
140+
A List of `Notebook` Resource Object.
141+
"""
142+
143+
# define the endpoints.
144+
endpoint = "me/" + self.endpoint + "/notebooks/{notebook_id}".format(
145+
notebook_id=notebook_id
146+
)
147+
148+
content = self.graph_session.make_request(
149+
method='get',
150+
endpoint=endpoint
151+
)
152+
153+
return content
154+
155+
def get_user_notebook(self, user_id: str, notebook_id: str) -> Dict:
156+
"""Retrieve a notebook object from a user by it's ID.
157+
158+
### Parameters
159+
----
160+
user_id (str): The User's ID that is assoicated with
161+
their Graph account.
162+
163+
notebook_id (str): The Notebook ID that you
164+
want to pull.
165+
166+
### Returns
167+
----
168+
Dict:
169+
A List of `Notebook` Resource Object.
170+
"""
171+
172+
# Define the endpoint.
173+
endpoint = "users/{user_id}".format(user_id=user_id) + self.endpoint + "/notebooks/{notebook_id}".format(
174+
notebook_id=notebook_id
175+
)
176+
177+
content = self.graph_session.make_request(
178+
method='get',
179+
endpoint=endpoint
180+
)
181+
182+
return content
183+
184+
def get_group_notebook(self, group_id: str, notebook_id: str) -> Dict:
185+
"""Retrieve a notebook object from a Group by it's ID.
186+
187+
### Parameters
188+
----
189+
group_id (str): The Group ID that you want to pull
190+
notebooks for.
191+
192+
notebook_id (str): The Notebook ID that you
193+
want to pull.
194+
195+
### Returns
196+
----
197+
Dict:
198+
A List of `Notebook` Resource Object.
199+
"""
200+
201+
# Define the endpoint.
202+
endpoint = "groups/{group_id}".format(group_id=group_id) + self.endpoint + "/notebooks/{notebook_id}".format(
203+
notebook_id=notebook_id
204+
)
205+
206+
content = self.graph_session.make_request(
207+
method='get',
208+
endpoint=endpoint
209+
)
210+
211+
return content
212+
213+
def get_site_notebook(self, site_id: str, notebook_id: str) -> Dict:
214+
"""Retrieve a notebook object from a SharePoint Site by it's ID.
215+
216+
### Parameters
217+
----
218+
site_id (str): The Site ID that you want to pull
219+
notebooks for.
220+
221+
notebook_id (str): The Notebook ID that you
222+
want to pull.
223+
224+
### Returns
225+
----
226+
Dict:
227+
A List of `Notebook` Resource Object.
228+
"""
229+
230+
# Define the endpoint.
231+
endpoint = "sites/{site_id}".format(site_id=site_id) + self.endpoint + "/notebooks/{notebook_id}".format(
232+
notebook_id=notebook_id
233+
)
234+
235+
content = self.graph_session.make_request(
236+
method='get',
237+
endpoint=endpoint
238+
)
239+
240+
return content
241+
242+
def list_my_notebook_section(self, notebook_id: str) -> Dict:
243+
"""Retrieve a list of onenoteSection objects from one of your notebooks.
244+
245+
### Parameters
246+
----
247+
notebook_id (str): The Notebook ID that you
248+
want to pull.
249+
250+
### Returns
251+
----
252+
Dict:
253+
A List of `Notebook` Resource Object.
254+
"""
255+
256+
# Define the endpoint.
257+
endpoint = endpoint = "me/" + self.endpoint + "/notebooks/{notebook_id}".format(
258+
notebook_id=notebook_id
259+
) + "/sections"
260+
261+
content = self.graph_session.make_request(
262+
method='get',
263+
endpoint=endpoint
264+
)
265+
266+
return content

ms_graph/session.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ def make_request(self, method: str, endpoint: str, mode: str = None, params: dic
107107
# Define the headers.
108108
headers = self.build_headers(mode='json')
109109

110+
print(url)
111+
110112
# Define a new session.
111113
request_session = requests.Session()
112114
request_session.verify = True

samples/use_notebook_services.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from pprint import pprint
2+
from configparser import ConfigParser
3+
from ms_graph.client import MicrosoftGraphClient
4+
5+
# SCOPES NEEDED:
6+
# ---------------
7+
# 'Notes.Create
8+
# 'Notes.Read'
9+
# 'Notes.ReadWrite'
10+
# 'Notes.Read.All',
11+
# 'Notes.ReadWrite.All'
12+
13+
# Define the Scopes needed to Login.
14+
scopes = [
15+
'Calendars.ReadWrite',
16+
'Files.ReadWrite.All',
17+
'User.ReadWrite.All',
18+
'Notes.ReadWrite.All',
19+
'Directory.ReadWrite.All',
20+
'User.Read.All',
21+
'Directory.Read.All',
22+
'Directory.ReadWrite.All',
23+
'Group.Read.All',
24+
'Group.ReadWrite.All',
25+
'Notes.Create',
26+
'Notes.Read',
27+
'Notes.ReadWrite',
28+
'Notes.Read.All',
29+
'Notes.ReadWrite.All'
30+
]
31+
32+
# Initialize the Parser.
33+
config = ConfigParser()
34+
35+
# Read the file.
36+
config.read('config/config.ini')
37+
38+
# Get the specified credentials.
39+
client_id = config.get('graph_api', 'client_id')
40+
client_secret = config.get('graph_api', 'client_secret')
41+
redirect_uri = config.get('graph_api', 'redirect_uri')
42+
43+
# Initialize the Client.
44+
graph_client = MicrosoftGraphClient(
45+
client_id=client_id,
46+
client_secret=client_secret,
47+
redirect_uri=redirect_uri,
48+
scope=scopes,
49+
credentials='config/ms_graph_state.jsonc'
50+
)
51+
52+
# Login to the Client.
53+
graph_client.login()
54+
55+
# Grab the Notes Services.
56+
notes_services = graph_client.notes()
57+
58+
# List all Notes.
59+
pprint(notes_services.list_my_notebooks())
60+
61+
# Grab the Notebook Sections.
62+
notebook_sections = notes_services.list_my_notebook_section(
63+
notebook_id="0-8BC640C57CDA25B6!71451"
64+
)
65+
66+
# List all Notes.
67+
pprint(notebook_sections)

0 commit comments

Comments
 (0)