-
Notifications
You must be signed in to change notification settings - Fork 53
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 #117 from ProdPerfect/mondaydb-2023-10-update
Updated "Made it work with the 2023-10 version of the API"
- Loading branch information
Showing
27 changed files
with
814 additions
and
530 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,3 +1,3 @@ | ||
__version__ = '1.3.3' | ||
__version__ = '2.0.0-pre' | ||
__author__ = 'Christina D\'Astolfo' | ||
__email__ = 'chdastolfo@gmail.com, lemi@prodperfect.com, pevner@prodperfect.com' |
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,2 +1,8 @@ | ||
class MondayError(RuntimeError): | ||
class MondayError(Exception): | ||
pass | ||
|
||
|
||
class MondayQueryError(MondayError): | ||
def __init__(self, message, original_errors=None): | ||
super().__init__(message) | ||
self.original_errors = original_errors or [] |
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,41 +1,56 @@ | ||
import requests | ||
import json | ||
|
||
import requests | ||
|
||
from monday.exceptions import MondayQueryError | ||
|
||
TOKEN_HEADER = 'Authorization' | ||
|
||
|
||
class GraphQLClient: | ||
def __init__(self, endpoint): | ||
self.endpoint = endpoint | ||
self.token = None | ||
self.headername = None | ||
self.headers = {} | ||
|
||
def execute(self, query, variables=None): | ||
return self._send(query, variables) | ||
|
||
def inject_token(self, token, headername='Authorization'): | ||
def inject_token(self, token): | ||
self.token = token | ||
self.headername = headername | ||
|
||
def inject_headers(self, headers): | ||
self.headers = headers | ||
|
||
def _send(self, query, variables): | ||
payload = {'query': query} | ||
headers = {} | ||
headers = self.headers.copy() | ||
files = None | ||
|
||
if self.token is not None: | ||
headers[self.headername] = '{}'.format(self.token) | ||
headers[TOKEN_HEADER] = self.token | ||
|
||
if variables is None: | ||
headers['Content-Type'] = 'application/json' | ||
headers.setdefault('Content-Type', 'application/json') | ||
|
||
payload = json.dumps({'query': query}).encode('utf-8') | ||
|
||
elif variables.get('file', None) is not None: | ||
headers['content'] = 'multipart/form-data' | ||
headers.setdefault('content', 'multipart/form-data') | ||
|
||
files = [ | ||
('variables[file]', (variables['file'], open(variables['file'], 'rb'))) | ||
] | ||
|
||
try: | ||
response = requests.request("POST", self.endpoint, headers=headers, data=payload, files=files) | ||
response.raise_for_status() | ||
return response.json() | ||
except requests.HTTPError as e: | ||
print(e) | ||
response_data = response.json() | ||
self._throw_on_error(response_data) | ||
return response_data | ||
except (requests.HTTPError, json.JSONDecodeError, MondayQueryError) as e: | ||
raise e | ||
|
||
def _throw_on_error(self, response_data): | ||
if 'errors' in response_data: | ||
raise MondayQueryError(response_data['errors'][0]['message'], response_data['errors']) |
Oops, something went wrong.