From b575daf430946167baf688e486503d69fe495c5a Mon Sep 17 00:00:00 2001 From: kd Date: Thu, 2 Jul 2020 14:54:55 -0400 Subject: updated with black and pyflakes run --- msgraph.py | 93 +++++++++++++++++++++++++++++--------------------------------- 1 file changed, 43 insertions(+), 50 deletions(-) (limited to 'msgraph.py') diff --git a/msgraph.py b/msgraph.py index 0ed77bc..00d5b6c 100644 --- a/msgraph.py +++ b/msgraph.py @@ -3,6 +3,7 @@ import json import requests import msal + class MSGraphApi(object): """Docstring for MSGraphApi. """ @@ -21,8 +22,7 @@ class MSGraphApi(object): self.flow = flow if self.flow == "obo": app = msal.PublicClientApplication( - self.config["client_id"], - authority=self.config["authority"] + self.config["client_id"], authority=self.config["authority"] ) else: # no flow was given, assume app flow @@ -46,8 +46,8 @@ class MSGraphApi(object): # Firstly, looks up a token from cache if self.flow == "obo": result = self.app.acquire_token_silent( - self.config["scope"], - account=self.app.get_accounts(self.config["username"]) + self.config["scope"], + account=self.app.get_accounts(self.config["username"]), ) else: # Since we are looking for token for the current app, NOT for an end user, @@ -55,13 +55,15 @@ class MSGraphApi(object): result = self.app.acquire_token_silent(self.config["scope"], account=None) if not result: - logging.info("No suitable token exists in cache. Let's get a new one from AAD.") + logging.info( + "No suitable token exists in cache. Let's get a new one from AAD." + ) if self.flow == "obo": result = self.app.acquire_token_by_username_password( - self.config["username"], - self.config["password"], - scopes=self.config["scope"] + self.config["username"], + self.config["password"], + scopes=self.config["scope"], ) else: result = self.app.acquire_token_for_client(scopes=self.config["scope"]) @@ -73,7 +75,9 @@ class MSGraphApi(object): else: logging.error(result.get("error")) logging.error(result.get("error_description")) - logging.error(result.get("correlation_id")) # may need this when reporting a bug + logging.error( + result.get("correlation_id") + ) # may need this when reporting a bug return None def setAccessToken(self, tok): @@ -93,12 +97,9 @@ class MSGraphApi(object): """ endpoint = "https://graph.microsoft.com/v1.0/users" - headers = { "Authorization": "Bearer " + self.access_token } + headers = {"Authorization": "Bearer " + self.access_token} - graph_data = requests.get( - endpoint, - headers=headers - ) + graph_data = requests.get(endpoint, headers=headers) return graph_data def getMyTeams(self): @@ -109,15 +110,11 @@ class MSGraphApi(object): """ endpoint = "https://graph.microsoft.com/beta/me/joinedTeams" - headers = { "Authorization": "Bearer " + self.access_token } + headers = {"Authorization": "Bearer " + self.access_token} - graph_data = requests.get( - endpoint, - headers=headers - ) + graph_data = requests.get(endpoint, headers=headers) return graph_data - def getChatsByTeam(self, teamId): """Docstring for getChatsByTeam. Get all chats in given team id. @@ -127,12 +124,9 @@ class MSGraphApi(object): """ endpoint = "https://graph.microsoft.com/beta/teams/%s/channels/" % (teamId) - headers = { "Authorization": "Bearer " + self.access_token } + headers = {"Authorization": "Bearer " + self.access_token} - graph_data = requests.get( - endpoint, - headers=headers - ) + graph_data = requests.get(endpoint, headers=headers) return graph_data def getChatByTeamAndId(self, teamId, channelId): @@ -144,13 +138,13 @@ class MSGraphApi(object): :returns: respose object containing ms response """ - endpoint = "https://graph.microsoft.com/beta/teams/%s/channels/%s" % (teamId, channelId) - headers = { "Authorization": "Bearer " + self.access_token } - - graph_data = requests.get( - endpoint, - headers=headers + endpoint = "https://graph.microsoft.com/beta/teams/%s/channels/%s" % ( + teamId, + channelId, ) + headers = {"Authorization": "Bearer " + self.access_token} + + graph_data = requests.get(endpoint, headers=headers) return graph_data def writeChatMessageByTeamAndId(self, teamId, channelId, payload): @@ -163,22 +157,21 @@ class MSGraphApi(object): """ # other pathways - #userId = "8a18e679-3603-4e10-a5e4-c75ad362fde0" - #endpoint = "https://graph.microsoft.com/beta/users/%s/chats/%s/messages" % (userId, channelId) - #endpoint = "https://graph.microsoft.com/beta/chats/%s/messages" % (channelId) + # userId = "8a18e679-3603-4e10-a5e4-c75ad362fde0" + # endpoint = "https://graph.microsoft.com/beta/users/%s/chats/%s/messages" % (userId, channelId) + # endpoint = "https://graph.microsoft.com/beta/chats/%s/messages" % (channelId) # ms recommended pathway - endpoint = "https://graph.microsoft.com/beta/teams/%s/channels/%s/messages/" % (teamId, channelId) + endpoint = "https://graph.microsoft.com/beta/teams/%s/channels/%s/messages/" % ( + teamId, + channelId, + ) headers = { "Authorization": "Bearer " + self.access_token, - "Content-Type": "application/json" + "Content-Type": "application/json", } - graph_data = requests.post( - endpoint, - headers=headers, - data=json.dumps(payload) - ) + graph_data = requests.post(endpoint, headers=headers, data=json.dumps(payload)) return graph_data def getTeamByName(self, teamName): @@ -234,19 +227,19 @@ class MSGraphApi(object): # schema teams_mssg = { "messageType": "message", - "body": { - "content": "", - "contentType": "html" - } + "body": {"content": "", "contentType": "html"}, } # convert ``` snippets into block quotes ie. #
some code
rich_html_content = MSGraphApi.insertCodeBlocksInMessage(customMessage["text"]) - html_content = "" + \ - "

From: %s %s

" % (customMessage["name"], customMessage["ts"]) + \ - "

%s

" % (rich_html_content) + \ - "" + html_content = ( + "" + + "

From: %s %s

" + % (customMessage["name"], customMessage["ts"]) + + "

%s

" % (rich_html_content) + + "" + ) split_content = html_content.split("\n") if len(split_content) > 1: @@ -257,7 +250,7 @@ class MSGraphApi(object): # TODO: sanitize string - #teams_mssg["body"]["content"] = html_content + # teams_mssg["body"]["content"] = html_content teams_mssg["body"]["content"] = split_content_str return teams_mssg -- cgit v1.2.3