|
| 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 |
0 commit comments