-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from michalm138/dev
Alerts and general improvements
- Loading branch information
Showing
11 changed files
with
266 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,3 +129,4 @@ dmypy.json | |
.pyre/ | ||
|
||
.idea/ | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,34 @@ | ||
from . import common, config | ||
import requests | ||
import json | ||
|
||
|
||
def post_request(url, data, token): | ||
""" | ||
:param url: (string) | ||
:param data: (dict) | ||
:param token: (string) Token of the Slack bot | ||
:return: (bool, dict) True and response JSON if the request is a success | ||
""" | ||
try: | ||
headers = {'Authorization': f'Bearer {token}'} | ||
response = requests.post(url, data, headers=headers) | ||
return response.json() | ||
except (requests.ConnectionError, json.JSONDecodeError, Exception): | ||
return {} | ||
class Request: | ||
|
||
def __init__(self, token): | ||
self.response = {} | ||
self._token = None | ||
self._headers = {'Authorization': f'Bearer {token}'} | ||
|
||
def post(self, url, data={}, exception=None): | ||
"""Sends the POST request | ||
Args: | ||
url (str): A key of the url in the config file. | ||
data (dict, optional): The request JSON payload. Defaults to {}. | ||
exception (exception): An exception - it is raised in case of request sending failure. | ||
""" | ||
try: | ||
response = requests.post( | ||
config.data['urls'][url], | ||
json=data, | ||
headers=self._headers, | ||
).json() | ||
if response['ok']: | ||
self.response = response | ||
else: | ||
raise common.get_exception(response) | ||
except KeyError as exp: | ||
raise Exception(f'Given url does not exist: {exp}') | ||
except (requests.ConnectionError, json.JSONDecodeError, requests.ConnectTimeout): | ||
raise exception if exception else Exception('Unknown error while sending request. Exception not provided.') |
Oops, something went wrong.