Skip to content

Commit 62bc0f5

Browse files
committed
begin adding contact folders services
1 parent 65ab80c commit 62bc0f5

File tree

3 files changed

+351
-0
lines changed

3 files changed

+351
-0
lines changed

ms_graph/client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from ms_graph.session import GraphSession
1717
from ms_graph.drive_items import DriveItems
1818
from ms_graph.search import Search
19+
from ms_graph.personal_contacts import PersonalContacts
1920

2021

2122
class MicrosoftGraphClient():
@@ -323,6 +324,10 @@ def grab_refresh_token(self) -> Dict:
323324
scopes=self.scope
324325
)
325326

327+
if 'error' in token_dict:
328+
print(token_dict)
329+
raise PermissionError("Permissions not authorized, delete json file and run again.")
330+
326331
# Save the Token.
327332
self._state(
328333
action='save',
@@ -414,3 +419,17 @@ def search(self) -> Search:
414419
search_object: Search = Search(session=self.graph_session)
415420

416421
return search_object
422+
423+
def personal_contacts(self) -> PersonalContacts:
424+
"""Used to access the PersonalContacts Services and metadata.
425+
426+
### Returns
427+
---
428+
Groups:
429+
The `PersonalContacts` services Object.
430+
"""
431+
432+
# Grab the `PersonalContacts` Object for the session.
433+
personal_contacts_object: PersonalContacts = PersonalContacts(session=self.graph_session)
434+
435+
return personal_contacts_object

ms_graph/personal_contacts.py

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
from typing import Dict
2+
from ms_graph.session import GraphSession
3+
4+
5+
class PersonalContacts():
6+
7+
"""
8+
## Overview:
9+
----
10+
A contact is an item in Outlook where you can organize and save
11+
information about the people and organizations you communicate
12+
with. Contacts are contained in contact folders.
13+
"""
14+
15+
def __init__(self, session: object) -> None:
16+
"""Initializes the `PersonalContacts` 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 = 'contacts'
29+
self.endpoint_folders = 'contactFolders'
30+
31+
def list_my_contacts(self) -> Dict:
32+
"""Retrieves all the contacts from the users mailbox.
33+
34+
### Returns
35+
----
36+
Dict:
37+
A List of `Contact` Resource Object.
38+
"""
39+
40+
# define the endpoints.
41+
endpoint = "me/" + self.endpoint
42+
43+
content = self.graph_session.make_request(
44+
method='get',
45+
endpoint=endpoint
46+
)
47+
48+
return content
49+
50+
def list_my_contacts_folder(self) -> Dict:
51+
"""Retrieves all the contacts folders from the users mailbox.
52+
53+
### Returns
54+
----
55+
Dict:
56+
A List of `ContactFolders` Resource Object.
57+
"""
58+
59+
# define the endpoints.
60+
endpoint = "me/" + self.endpoint_folders
61+
62+
content = self.graph_session.make_request(
63+
method='get',
64+
endpoint=endpoint
65+
)
66+
67+
return content
68+
69+
def list_contacts_folder_by_id(self, user_id: str, folder_id: str) -> Dict:
70+
"""Retrieves all the contacts folders from the users mailbox.
71+
72+
### Parameters
73+
----
74+
user_id : str
75+
The User ID that the folder belongs to.
76+
77+
folder_id : str
78+
The folder ID you want to retrieve.
79+
80+
### Returns
81+
----
82+
Dict:
83+
A List of `ContactFolders` Resource Object.
84+
"""
85+
86+
# define the endpoints.
87+
endpoint = "users/{user_id}/".format(user_id=user_id) + self.endpoint_folders + "/{id}".format(id=folder_id)
88+
89+
content = self.graph_session.make_request(
90+
method='get',
91+
endpoint=endpoint
92+
)
93+
94+
return content
95+
96+
def create_my_contact_folder(self, folder_resource: Dict) -> Dict:
97+
"""Creates a new Contact Folder under the default users profile.
98+
99+
### Parameters
100+
----
101+
folder_resource : Dict
102+
A dictionary that specifies the folder resource
103+
attributes like the folder ID and folder display
104+
value.
105+
106+
### Returns
107+
----
108+
Dict:
109+
A `ContactFolder` Resource Object.
110+
"""
111+
112+
# define the endpoints.
113+
endpoint = "me/" + self.endpoint_folders
114+
115+
content = self.graph_session.make_request(
116+
method='post',
117+
endpoint=endpoint,
118+
json=folder_resource
119+
)
120+
121+
return content
122+
123+
def create_user_contact_folder(self, user_id: str, folder_resource: Dict) -> Dict:
124+
"""Creates a new Contact Folder under the specified users profile.
125+
126+
### Parameters
127+
----
128+
user_id : str
129+
The User ID that the folder belongs to.
130+
131+
folder_resource : Dict
132+
A dictionary that specifies the folder resource
133+
attributes like the folder ID and folder display
134+
value.
135+
136+
### Returns
137+
----
138+
Dict:
139+
A `ContactFolder` Resource Object.
140+
"""
141+
142+
# define the endpoints.
143+
endpoint = "users/{user_id}/".format(user_id=user_id) + self.endpoint_folders
144+
145+
content = self.graph_session.make_request(
146+
method='post',
147+
endpoint=endpoint,
148+
json=folder_resource
149+
)
150+
151+
return content
152+
153+
def get_my_contacts_folder_by_id(self, folder_id: str) -> Dict:
154+
"""Retrieves a contactsFolder resource using the specified ID.
155+
156+
### Parameters
157+
----
158+
folder_id : str
159+
The folder ID you want to retrieve.
160+
161+
### Returns
162+
----
163+
Dict:
164+
A `ContactFolder` Resource Object.
165+
"""
166+
167+
# define the endpoints.
168+
endpoint = "me/" + self.endpoint_folders + "/{id}".format(id=folder_id)
169+
170+
content = self.graph_session.make_request(
171+
method='get',
172+
endpoint=endpoint
173+
)
174+
175+
return content
176+
177+
def get_contacts_folder_by_id(self, user_id: str, folder_id: str) -> Dict:
178+
"""Retrieves a contactsFolder resource using the specified ID for the
179+
specified user.
180+
181+
### Parameters
182+
----
183+
user_id : str
184+
The User ID that the folder belongs to.
185+
186+
folder_id : str
187+
The folder ID you want to retrieve.
188+
189+
### Returns
190+
----
191+
Dict:
192+
A `ContactFolder` Resource Object.
193+
"""
194+
195+
# define the endpoints.
196+
endpoint = "users/{user_id}/".format(user_id=user_id) + self.endpoint_folders + "/{id}".format(id=folder_id)
197+
198+
content = self.graph_session.make_request(
199+
method='get',
200+
endpoint=endpoint
201+
)
202+
203+
return content
204+
205+
def get_my_contact_by_id(self, contact_id: str) -> Dict:
206+
"""Retrieves the Contact Resource for the specified contact ID.
207+
208+
### Parameters
209+
----
210+
contact_id : str
211+
An authenticated session for our Microsoft Graph Client.
212+
213+
### Returns
214+
----
215+
Dict:
216+
A List of `Contact` Resource Object.
217+
"""
218+
219+
# define the endpoints.
220+
endpoint = "me/" + self.endpoint + "/{id}".format(id=contact_id)
221+
222+
content = self.graph_session.make_request(
223+
method='get',
224+
endpoint=endpoint
225+
)
226+
227+
return content
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
from pprint import pprint
2+
from ms_graph.client import MicrosoftGraphClient
3+
from configparser import ConfigParser
4+
5+
scopes = [
6+
'Contacts.ReadWrite',
7+
'Calendars.ReadWrite',
8+
'Files.ReadWrite.All',
9+
'User.ReadWrite.All',
10+
'Notes.ReadWrite.All',
11+
'Directory.ReadWrite.All',
12+
'User.Read.All',
13+
'Directory.Read.All',
14+
'Directory.ReadWrite.All',
15+
'Mail.ReadWrite',
16+
'Sites.ReadWrite.All',
17+
'ExternalItem.Read.All'
18+
]
19+
20+
# Initialize the Parser.
21+
config = ConfigParser()
22+
23+
# Read the file.
24+
config.read('config/config.ini')
25+
26+
# Get the specified credentials.
27+
client_id = config.get('graph_api', 'client_id')
28+
client_secret = config.get('graph_api', 'client_secret')
29+
redirect_uri = config.get('graph_api', 'redirect_uri')
30+
31+
# Initialize the Client.
32+
graph_client = MicrosoftGraphClient(
33+
client_id=client_id,
34+
client_secret=client_secret,
35+
redirect_uri=redirect_uri,
36+
scope=scopes,
37+
credentials='config/ms_graph_state.jsonc'
38+
)
39+
40+
# Login to the Client.
41+
graph_client.login()
42+
43+
# Define a valid User ID.
44+
user_id = '8bc640c57cda25b6'
45+
46+
# Define a folder ID.
47+
folder_id = 'AQMkADAwATZiZmYAZC1hMDI2LTE3NTgtMDACLTAwCgAuAAADpjqwNb_dak68rN7703uffQEAFNKsLOjbGUuHHmYnyKdJiAAFAP8ORwAAAA=='
48+
49+
# Grab the Personal Contacts Service.
50+
personal_contacts_service = graph_client.personal_contacts()
51+
52+
# Grab my contacts folders.
53+
pprint(
54+
personal_contacts_service.list_my_contacts_folder()
55+
)
56+
57+
# Grab a contact folder for a specific user and a specific ID.
58+
pprint(
59+
personal_contacts_service.list_contacts_folder_by_id(
60+
user_id=user_id,
61+
folder_id=folder_id
62+
)
63+
)
64+
65+
# Grab a contact folder for a specific user and a specific ID.
66+
pprint(
67+
personal_contacts_service.get_contacts_folder_by_id(
68+
user_id=user_id,
69+
folder_id=folder_id
70+
)
71+
)
72+
73+
# Grab the Contacts.
74+
my_contacts = personal_contacts_service.list_my_contacts()
75+
76+
# Get a random contact id.
77+
contact_id = my_contacts['value'][-1]['id']
78+
79+
# Grab a specific contact from my contacts folder.
80+
pprint(
81+
personal_contacts_service.get_my_contact_by_id(
82+
contact_id=contact_id
83+
)
84+
)
85+
86+
# Create a new contact folder under the default profile.
87+
pprint(
88+
personal_contacts_service.create_my_contact_folder(
89+
folder_resource={
90+
"parentFolderId": "sigma-coding-contacts",
91+
"displayName": "Sigma Coding - Contacts"
92+
}
93+
)
94+
)
95+
96+
# Create a new contact folder under the specified user profile.
97+
pprint(
98+
personal_contacts_service.create_user_contact_folder(
99+
user_id=user_id,
100+
folder_resource={
101+
"parentFolderId": "trading-robot-contacts",
102+
"displayName": "Trading Robot - Contacts"
103+
}
104+
)
105+
)

0 commit comments

Comments
 (0)