Skip to content

Commit 4c71bee

Browse files
committed
Cleanup
1 parent e20f127 commit 4c71bee

12 files changed

+99
-72
lines changed

.vscode/settings.json

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
{
22
"python.linting.enabled": true,
3-
"python.linting.pylintEnabled": false,
4-
"python.pythonPath": "C:\\Users\\Alex\\AppData\\Local\\Programs\\Python\\Python39\\python.exe",
5-
"python.linting.flake8Enabled": true,
6-
"python.linting.flake8Args": ["--max-line-length=140", "--ignore=E402"],
7-
"python.testing.unittestArgs": ["-v", "-s", "./tests", "-p", "test_*.py"],
3+
"python.linting.pylintEnabled": true,
4+
// Ignore Module Doc Strings.
5+
"python.linting.pylintArgs": [
6+
"--disable",
7+
"C0114,C0415"
8+
],
9+
"python.testing.unittestArgs": [
10+
"-v",
11+
"-s",
12+
"./tests",
13+
"-p",
14+
"test_*.py"
15+
],
816
"python.testing.pytestEnabled": false,
9-
"python.testing.nosetestsEnabled": false,
1017
"python.testing.unittestEnabled": true
11-
}
18+
}

ms_graph/client.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import msal
32
import time
43
import urllib
54
import random
@@ -9,6 +8,8 @@
98
from typing import List
109
from typing import Dict
1110

11+
import msal
12+
1213
from ms_graph.users import Users
1314
from ms_graph.drives import Drives
1415
from ms_graph.groups import Groups
@@ -22,6 +23,13 @@
2223

2324
class MicrosoftGraphClient():
2425

26+
"""
27+
### Overview:
28+
----
29+
Used as the main entry point for the Microsoft Graph
30+
API Service.
31+
"""
32+
2533
RESOURCE = 'https://graph.microsoft.com/'
2634

2735
AUTHORITY_URL = 'https://login.microsoftonline.com/'
@@ -32,9 +40,16 @@ class MicrosoftGraphClient():
3240
OFFICE365_AUTH_ENDPOINT = '/oauth20_authorize.srf?'
3341
OFFICE365_TOKEN_ENDPOINT = '/oauth20_token.srf'
3442

35-
def __init__(self, client_id: str, client_secret: str, redirect_uri: str,
36-
scope: List[str], account_type: str = 'consumers',
37-
office365: bool = False, credentials: str = None):
43+
def __init__(
44+
self,
45+
client_id: str,
46+
client_secret: str,
47+
redirect_uri: str,
48+
scope: List[str],
49+
account_type: str = 'consumers',
50+
office365: bool = False,
51+
credentials: str = None
52+
):
3853
"""Initializes the Graph Client.
3954
4055
### Parameters
@@ -86,6 +101,7 @@ def __init__(self, client_id: str, client_secret: str, redirect_uri: str,
86101
self.office_url = self.OFFICE365_AUTHORITY_URL + self.OFFICE365_AUTH_ENDPOINT
87102
self.graph_url = self.AUTHORITY_URL + self.account_type + self.AUTH_ENDPOINT
88103
self.office365 = office365
104+
self._redirect_code = None
89105

90106
# Initialize the Credential App.
91107
self.client_app = msal.ConfidentialClientApplication(
@@ -121,7 +137,7 @@ def _state(self, action: str, token_dict: dict = None) -> bool:
121137
if does_exists and action == 'load':
122138

123139
# Load the file.
124-
with open(file=self.credentials, mode='r') as state_file:
140+
with open(file=self.credentials, mode='r', encoding='utf-8') as state_file:
125141
credentials = json.load(fp=state_file)
126142

127143
# Grab the Token if it exists.
@@ -150,7 +166,7 @@ def _state(self, action: str, token_dict: dict = None) -> bool:
150166
self.id_token = token_dict['id_token']
151167
self.token_dict = token_dict
152168

153-
with open(file=self.credentials, mode='w+') as state_file:
169+
with open(file=self.credentials, mode='w+', encoding='utf-8') as state_file:
154170
json.dump(obj=token_dict, fp=state_file, indent=2)
155171

156172
def _token_seconds(self, token_type: str = 'access_token') -> int:
@@ -249,10 +265,12 @@ def login(self) -> None:
249265
# Build the URL.
250266
url = self.authorization_url()
251267

252-
# aks the user to go to the URL provided, they will be prompted to authenticate themsevles.
253-
print('Please go to URL provided authorize your account: {}'.format(url))
268+
# aks the user to go to the URL provided, they will be prompted
269+
# to authenticate themsevles.
270+
print(f'Please go to URL provided authorize your account: {url}')
254271

255-
# ask the user to take the final URL after authentication and paste here so we can parse.
272+
# ask the user to take the final URL after authentication and
273+
# paste here so we can parse.
256274
my_response = input('Paste the full URL redirect here: ')
257275

258276
# store the redirect URL

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
msal==1.5.0
2-
requests==2.24.0
1+
msal>=1.5.0
2+
requests>=2.24.0

samples/configs/write_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
config.set('graph_api', 'redirect_uri', '')
1313

1414
# Write the file.
15-
with open(file='samples/configs/config.ini', mode='w+') as f:
15+
with open(file='samples/configs/config.ini', mode='w+', encoding='utf-8') as f:
1616
config.write(f)

samples/use_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pprint import pprint
2-
from ms_graph.client import MicrosoftGraphClient
32
from configparser import ConfigParser
3+
from ms_graph.client import MicrosoftGraphClient
44

55
scopes = [
66
'Calendars.ReadWrite',

samples/use_drive_services.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pprint import pprint
2-
from ms_graph.client import MicrosoftGraphClient
32
from configparser import ConfigParser
3+
from ms_graph.client import MicrosoftGraphClient
44

55
scopes = [
66
'Calendars.ReadWrite',

samples/use_mail_services.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,17 @@
5454
graph_client.login()
5555

5656
# Define a valid User ID.
57-
user_id = '8bc640c57cda25b6'
57+
USER_ID = '8bc640c57cda25b6'
5858

5959
# Define a mail Item ID.
60-
mail_id = 'AQMkADAwATZiZmYAZC1hMDI2LTE3NTgtMDACLTAwCgBGAAADpjqwNb_dak68rN7703u' + \
61-
'ffQcAFNKsLOjbGUuHHmYnyKdJiAAAAgEhAAAAFNKsLOjbGUuHHmYnyKdJiAAFBMTneQAAAA=='
60+
MAIL_ID = 'AQMkADAwATZiZmYAZC1hMDI2LTE3NTgtMDACLTAwCgBGAAADpjqwNb_dak68rN7703u' + \
61+
'ffQcAFNKsLOjbGUuHHmYnyKdJiAAAAgEhAAAAFNKsLOjbGUuHHmYnyKdJiAAFBMTneQ' + \
62+
'AAAA=='
6263

6364
# Define a mail item ID with Attachments.
64-
mail_id_with_attachments = 'AQMkADAwATZiZmYAZC1hMDI2LTE3NTgtMDACLTAwCgBGAAADpjqwNb+' + \
65-
'dak68rN7703uffQcAFNKsLOjbGUuHHmYnyKdJiAAAAgEMAAAAFNKsLOjbGUuHHmYnyKdJiAAE9ucV+AAAAA=='
65+
MAIL_ID_ATTACHMENTS = 'AQMkADAwATZiZmYAZC1hMDI2LTE3NTgtMDACLTAwCgBGAAADpjqwNb+' + \
66+
'dak68rN7703uffQcAFNKsLOjbGUuHHmYnyKdJiAAAAgEMAAAAFNKsLO' + \
67+
'jbGUuHHmYnyKdJiAAE9ucV+AAAAA=='
6668

6769
# Grab the Notes Services.
6870
mail_services = graph_client.mail()
@@ -75,21 +77,21 @@
7577
# Grab a specific message for the default user.
7678
pprint(
7779
mail_services.get_my_messages(
78-
message_id=mail_id
80+
message_id=MAIL_ID
7981
)
8082
)
8183

8284
# Get a Specific User's Message.
8385
pprint(
8486
mail_services.get_user_messages(
85-
user_id=user_id,
86-
message_id=mail_id
87+
user_id=USER_ID,
88+
message_id=MAIL_ID
8789
)
8890
)
8991

9092
# List the rules for a specific user..
9193
pprint(
92-
mail_services.list_rules(user_id=user_id)
94+
mail_services.list_rules(user_id=USER_ID)
9395
)
9496

9597
# List the rules for the default user.
@@ -99,7 +101,7 @@
99101

100102
# List the overrides for a specific user.
101103
pprint(
102-
mail_services.list_overrides(user_id=user_id)
104+
mail_services.list_overrides(user_id=USER_ID)
103105
)
104106

105107
# List the overrides for the default user.
@@ -110,7 +112,7 @@
110112
# List the attachments for a specific message.
111113
pprint(
112114
mail_services.list_my_attachements(
113-
message_id=mail_id_with_attachments
115+
message_id=MAIL_ID_ATTACHMENTS
114116
)
115117
)
116118

samples/use_personal_contacts_service.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pprint import pprint
2-
from ms_graph.client import MicrosoftGraphClient
32
from configparser import ConfigParser
3+
from ms_graph.client import MicrosoftGraphClient
44

55
scopes = [
66
'Contacts.ReadWrite',
@@ -41,10 +41,10 @@
4141
graph_client.login()
4242

4343
# Define a valid User ID.
44-
user_id = '8bc640c57cda25b6'
44+
USER_ID = '8bc640c57cda25b6'
4545

4646
# Define a folder ID.
47-
folder_id = 'AQMkADAwATZiZmYAZC1hMDI2LTE3NTgtMDACLTAwCgAuAAADpjqwNb_d' + \
47+
FOLDER_ID = 'AQMkADAwATZiZmYAZC1hMDI2LTE3NTgtMDACLTAwCgAuAAADpjqwNb_d' + \
4848
'ak68rN7703uffQEAFNKsLOjbGUuHHmYnyKdJiAAFAP8ORwAAAA=='
4949

5050
# Grab the Personal Contacts Service.
@@ -58,16 +58,16 @@
5858
# Grab a contact folder for a specific user and a specific ID.
5959
pprint(
6060
personal_contacts_service.list_contacts_folder_by_id(
61-
user_id=user_id,
62-
folder_id=folder_id
61+
user_id=USER_ID,
62+
folder_id=FOLDER_ID
6363
)
6464
)
6565

6666
# Grab a contact folder for a specific user and a specific ID.
6767
pprint(
6868
personal_contacts_service.get_contacts_folder_by_id(
69-
user_id=user_id,
70-
folder_id=folder_id
69+
user_id=USER_ID,
70+
folder_id=FOLDER_ID
7171
)
7272
)
7373

@@ -97,7 +97,7 @@
9797
# Create a new contact folder under the specified user profile.
9898
pprint(
9999
personal_contacts_service.create_user_contact_folder(
100-
user_id=user_id,
100+
user_id=USER_ID,
101101
folder_resource={
102102
"parentFolderId": "trading-robot-contacts",
103103
"displayName": "Trading Robot - Contacts"

samples/use_search_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pprint import pprint
2-
from ms_graph.client import MicrosoftGraphClient
32
from configparser import ConfigParser
3+
from ms_graph.client import MicrosoftGraphClient
44

55
scopes = [
66
'Calendars.ReadWrite',

samples/user_drive_items_services.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pprint import pprint
2-
from ms_graph.client import MicrosoftGraphClient
32
from configparser import ConfigParser
3+
from ms_graph.client import MicrosoftGraphClient
44

55
scopes = [
66
'Calendars.ReadWrite',
@@ -40,50 +40,50 @@
4040
drive_item_services = graph_client.drive_item()
4141

4242
# Define a valid User ID.
43-
user_id = '8bc640c57cda25b6'
43+
USER_ID = '8bc640c57cda25b6'
4444

4545
# Define a valid Drive ID.
46-
drive_id = '8bc640c57cda25b6'
46+
DRIVE_ID = '8bc640c57cda25b6'
4747

4848
# Define a valid Drive Item ID.
49-
drive_item_id = '8BC640C57CDA25B6!3837'
49+
DRIVE_ITEM_ID = '8BC640C57CDA25B6!3837'
5050

5151
# Grab a Drive Item, by ID.
5252
pprint(
5353
drive_item_services.get_drive_item(
54-
drive_id=drive_id,
55-
item_id=drive_item_id
54+
drive_id=DRIVE_ID,
55+
item_id=DRIVE_ITEM_ID
5656
)
5757
)
5858

5959
# Grab a Drive Item, by path.
6060
pprint(
6161
drive_item_services.get_drive_item_by_path(
62-
drive_id=drive_id,
62+
drive_id=DRIVE_ID,
6363
item_path='/Career - Certifications & Exams'
6464
)
6565
)
6666

6767
# Grab a Drive Item, for a specific user in a specific Drive.
6868
pprint(
6969
drive_item_services.get_user_drive_item(
70-
user_id=user_id,
71-
item_id=drive_item_id
70+
user_id=USER_ID,
71+
item_id=DRIVE_ITEM_ID
7272
)
7373
)
7474

7575
# Grab a Drive Item, by path for a specific user in a specific Drive.
7676
pprint(
7777
drive_item_services.get_user_drive_item_by_path(
78-
user_id=user_id,
78+
user_id=USER_ID,
7979
item_path='/Career - Certifications & Exams'
8080
)
8181
)
8282

8383
# Grab my Drive Item by ID.
8484
pprint(
8585
drive_item_services.get_my_drive_item(
86-
item_id=drive_item_id
86+
item_id=DRIVE_ITEM_ID
8787
)
8888
)
8989

@@ -95,20 +95,20 @@
9595
)
9696

9797
# Define a valid Group ID.
98-
group_id = 'GROUP_ID_GOES_HERE'
98+
GROUP_ID = 'GROUP_ID_GOES_HERE'
9999

100100
# Grab a group Drive Item by ID.
101101
pprint(
102102
drive_item_services.get_group_drive_item(
103-
group_id=group_id,
104-
item_id=drive_item_id
103+
group_id=GROUP_ID,
104+
item_id=DRIVE_ITEM_ID
105105
)
106106
)
107107

108108
# Grab a group Drive Item, by path.
109109
pprint(
110110
drive_item_services.get_group_drive_item_by_path(
111-
group_id=group_id,
111+
group_id=GROUP_ID,
112112
item_path='/Career - Certifications & Exams'
113113
)
114114
)

0 commit comments

Comments
 (0)