Skip to content

Commit

Permalink
Merge pull request #117 from ProdPerfect/mondaydb-2023-10-update
Browse files Browse the repository at this point in the history
Updated "Made it work with the 2023-10 version of the API"
  • Loading branch information
pevner-p2 authored Nov 22, 2023
2 parents 94144b6 + 3f164e4 commit 6998697
Show file tree
Hide file tree
Showing 27 changed files with 814 additions and 530 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@
"contributions": [
"code"
]
},
{
"login": "willnaoosmith",
"name": "William Brochensque Júnior",
"avatar_url": "https://avatars.githubusercontent.com/u/55159911?v=4",
"profile": "https://github.com/willnaoosmith",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand Down
695 changes: 379 additions & 316 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion monday/__version__.py
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__ = '[email protected], [email protected], [email protected]'
37 changes: 25 additions & 12 deletions monday/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,36 @@
"""

from .__version__ import __version__
from .resources import ItemResource, UpdateResource, TagResource, BoardResource, UserResource, GroupResource, ComplexityResource, WorkspaceResource, NotificationResource
from .resources import CustomResource, ItemResource, ColumnsResource, UpdateResource, TagResource, BoardResource, \
UserResource, GroupResource, ComplexityResource, WorkspaceResource, NotificationResource, MeResource

_DEFAULT_HEADERS = {
"API-Version": "2023-10"
}


class MondayClient:
def __init__(self, token):
def __init__(self, token, headers=None):
"""
:param token: API Token for the new :class:`BaseResource` object.
:param token: API token for the new :class:`BaseResource` object.
:param headers: (optional) headers for the new :class:`BaseResource` object.
"""
self.items = ItemResource(token=token)
self.updates = UpdateResource(token=token)
self.tags = TagResource(token=token)
self.boards = BoardResource(token=token)
self.users = UserResource(token=token)
self.groups = GroupResource(token=token)
self.complexity = ComplexityResource(token=token)
self.workspaces = WorkspaceResource(token=token)
self.notifications = NotificationResource(token=token)

if not headers:
headers = _DEFAULT_HEADERS.copy()

self.custom = CustomResource(token=token, headers=headers)
self.items = ItemResource(token=token, headers=headers)
self.columns = ColumnsResource(token=token, headers=headers)
self.updates = UpdateResource(token=token, headers=headers)
self.tags = TagResource(token=token, headers=headers)
self.boards = BoardResource(token=token, headers=headers)
self.users = UserResource(token=token, headers=headers)
self.groups = GroupResource(token=token, headers=headers)
self.complexity = ComplexityResource(token=token, headers=headers)
self.workspaces = WorkspaceResource(token=token, headers=headers)
self.notifications = NotificationResource(token=token, headers=headers)
self.me = MeResource(token=token, headers=headers)

def __str__(self):
return f'MondayClient {__version__}'
Expand Down
8 changes: 7 additions & 1 deletion monday/exceptions.py
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 []
37 changes: 26 additions & 11 deletions monday/graphqlclient/client.py
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'])
Loading

0 comments on commit 6998697

Please sign in to comment.