From 5412035e1d53447b3d45f9ddaa3c91dddafc9171 Mon Sep 17 00:00:00 2001 From: Lucio Seki Date: Tue, 18 May 2021 17:04:05 -0300 Subject: [PATCH 1/4] Convert API response JSON to Python dict Some methods in this library are returning the API response JSON string without converting it to a Python dict. This is forcing the caller to selectively call json.loads depending on the retrieved resource (e.g. `users` resource requires json.loads, but `items` doesn't). This patch makes all the API responses to be converted to a Python dict, so the caller can consistently use the return value as a dict regardless the requested resource. --- monday/resources/tags.py | 4 +++- monday/resources/updates.py | 8 +++++--- monday/resources/users.py | 4 +++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/monday/resources/tags.py b/monday/resources/tags.py index 7e3aade..caa9c5e 100644 --- a/monday/resources/tags.py +++ b/monday/resources/tags.py @@ -1,3 +1,5 @@ +import json + from monday.resources.base import BaseResource from monday.query_joins import get_tags_query @@ -8,5 +10,5 @@ def __init__(self, token): def fetch_tags(self, tag_ids=None): query = get_tags_query(tag_ids) - return self.client.execute(query) + return json.loads(self.client.execute(query)) diff --git a/monday/resources/updates.py b/monday/resources/updates.py index f8dc3cf..ab13197 100644 --- a/monday/resources/updates.py +++ b/monday/resources/updates.py @@ -1,3 +1,5 @@ +import json + from monday.resources.base import BaseResource from monday.query_joins import create_update_query, get_update_query, get_updates_for_item_query @@ -8,12 +10,12 @@ def __init__(self, token): def create_update(self, item_id, update_value): query = create_update_query(item_id, update_value) - return self.client.execute(query) + return json.loads(self.client.execute(query)) def fetch_updates(self, limit, page=None): query = get_update_query(limit, page) - return self.client.execute(query) + return json.loads(self.client.execute(query)) def fetch_updates_for_item(self, board_id, item_id, limit=100): query = get_updates_for_item_query(board=board_id, item=item_id, limit=limit) - return self.client.execute(query) + return json.loads(self.client.execute(query)) diff --git a/monday/resources/users.py b/monday/resources/users.py index bc8e61a..8df2190 100644 --- a/monday/resources/users.py +++ b/monday/resources/users.py @@ -1,3 +1,5 @@ +import json + from monday.resources.base import BaseResource from monday.query_joins import get_users_query @@ -8,4 +10,4 @@ def __init__(self, token): def fetch_users(self, **kwargs): query = get_users_query(**kwargs) - return self.client.execute(query) + return json.loads(self.client.execute(query)) From cee40fd2241f662c0b3675275de73173140f87b4 Mon Sep 17 00:00:00 2001 From: Lucio Seki Date: Wed, 19 May 2021 18:32:23 -0300 Subject: [PATCH 2/4] Make GraphQL client return JSON response Currently, the library converts the response.text returned by the GraphQL client to a Python dict using json.loads. But the requests library already has a builtin JSON decoder that converts a JSON response to a Python dict. This patch makes the GraphQL return the response.json() result. The callers no longer need to convert the return value to a dict. --- monday/graphqlclient/client.py | 3 ++- monday/resources/base.py | 3 +-- monday/resources/boards.py | 10 ++++------ monday/resources/groups.py | 14 ++++++-------- monday/resources/items.py | 16 +++++++--------- monday/resources/tags.py | 4 +--- monday/resources/updates.py | 8 +++----- monday/resources/users.py | 4 +--- 8 files changed, 25 insertions(+), 37 deletions(-) diff --git a/monday/graphqlclient/client.py b/monday/graphqlclient/client.py index a3a1768..5836940 100644 --- a/monday/graphqlclient/client.py +++ b/monday/graphqlclient/client.py @@ -34,7 +34,8 @@ def _send(self, query, variables): try: response = requests.request("POST", self.endpoint, headers=headers, data=payload, files=files) - return response.text + response.raise_for_status() + return response.json() except requests.HTTPError as e: print(e) raise e diff --git a/monday/resources/base.py b/monday/resources/base.py index 363b2ae..d35b7e0 100644 --- a/monday/resources/base.py +++ b/monday/resources/base.py @@ -1,4 +1,3 @@ -import json from monday import __version__ from monday.exceptions import MondayError from monday.graphqlclient.client import GraphQLClient @@ -21,7 +20,7 @@ def _query(self, query): result = self.client.execute(query) if result: - return json.loads(result) + return result def __str__(self): return self.__class__.__name__ diff --git a/monday/resources/boards.py b/monday/resources/boards.py index 2257175..fe9afd6 100644 --- a/monday/resources/boards.py +++ b/monday/resources/boards.py @@ -1,5 +1,3 @@ -import json - from monday.resources.base import BaseResource from monday.query_joins import get_boards_query, get_boards_by_id_query, get_board_items_query, \ get_columns_by_board_query @@ -11,16 +9,16 @@ def __init__(self, token): def fetch_boards(self, **kwargs): query = get_boards_query(**kwargs) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def fetch_boards_by_id(self, board_ids): query = get_boards_by_id_query(board_ids) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def fetch_items_by_board_id(self, board_ids): query = get_board_items_query(board_ids) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def fetch_columns_by_board_id(self, board_ids): query = get_columns_by_board_query(board_ids) - return json.loads(self.client.execute(query)) + return self.client.execute(query) diff --git a/monday/resources/groups.py b/monday/resources/groups.py index b515e13..146d568 100644 --- a/monday/resources/groups.py +++ b/monday/resources/groups.py @@ -1,5 +1,3 @@ -import json - from monday.resources.base import BaseResource from monday.query_joins import get_groups_by_board_query, get_items_by_group_query, create_group_query, \ duplicate_group_query, archive_group_query, delete_group_query @@ -11,24 +9,24 @@ def __init__(self, token): def get_groups_by_board(self, board_ids): query = get_groups_by_board_query(board_ids=board_ids) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def get_items_by_group(self, board_id, group_id): query = get_items_by_group_query(board_id=board_id, group_id=group_id) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def create_group(self, board_id, group_name): query = create_group_query(board_id=board_id, group_name=group_name) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def duplicate_group(self, board_id, group_id): query = duplicate_group_query(board_id=board_id, group_id=group_id) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def archive_group(self, board_id, group_id): query = archive_group_query(board_id=board_id, group_id=group_id) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def delete_group(self, board_id, group_id): query = delete_group_query(board_id=board_id, group_id=group_id) - return json.loads(self.client.execute(query)) + return self.client.execute(query) diff --git a/monday/resources/items.py b/monday/resources/items.py index 94e1f87..66ce8f3 100644 --- a/monday/resources/items.py +++ b/monday/resources/items.py @@ -1,5 +1,3 @@ -import json - from monday.resources.base import BaseResource from monday.query_joins import mutate_item_query, get_item_query, update_item_query, get_item_by_id_query, \ update_multiple_column_values_query, mutate_subitem_query, add_file_to_column_query @@ -11,28 +9,28 @@ def __init__(self, token): def create_item(self, board_id, group_id, item_name, column_values=None): query = mutate_item_query(board_id, group_id, item_name, column_values) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def create_subitem(self, parent_item_id, subitem_name, column_values=None): query = mutate_subitem_query(parent_item_id, subitem_name, column_values) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def fetch_items_by_column_value(self, board_id, column_id, value): query = get_item_query(board_id, column_id, value) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def fetch_items_by_id(self, ids): query = get_item_by_id_query(ids) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def change_item_value(self, board_id, item_id, column_id, value): query = update_item_query(board_id, item_id, column_id, value) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def change_multiple_column_values(self, board_id, item_id, column_values): query = update_multiple_column_values_query(board_id, item_id, column_values) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def add_file_to_column(self, item_id, column_id, file): query = add_file_to_column_query(item_id, column_id) - return json.loads(self.file_upload_client.execute(query, variables={'file': file})) + return self.file_upload_client.execute(query, variables={'file': file}) diff --git a/monday/resources/tags.py b/monday/resources/tags.py index caa9c5e..7e3aade 100644 --- a/monday/resources/tags.py +++ b/monday/resources/tags.py @@ -1,5 +1,3 @@ -import json - from monday.resources.base import BaseResource from monday.query_joins import get_tags_query @@ -10,5 +8,5 @@ def __init__(self, token): def fetch_tags(self, tag_ids=None): query = get_tags_query(tag_ids) - return json.loads(self.client.execute(query)) + return self.client.execute(query) diff --git a/monday/resources/updates.py b/monday/resources/updates.py index ab13197..f8dc3cf 100644 --- a/monday/resources/updates.py +++ b/monday/resources/updates.py @@ -1,5 +1,3 @@ -import json - from monday.resources.base import BaseResource from monday.query_joins import create_update_query, get_update_query, get_updates_for_item_query @@ -10,12 +8,12 @@ def __init__(self, token): def create_update(self, item_id, update_value): query = create_update_query(item_id, update_value) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def fetch_updates(self, limit, page=None): query = get_update_query(limit, page) - return json.loads(self.client.execute(query)) + return self.client.execute(query) def fetch_updates_for_item(self, board_id, item_id, limit=100): query = get_updates_for_item_query(board=board_id, item=item_id, limit=limit) - return json.loads(self.client.execute(query)) + return self.client.execute(query) diff --git a/monday/resources/users.py b/monday/resources/users.py index 8df2190..bc8e61a 100644 --- a/monday/resources/users.py +++ b/monday/resources/users.py @@ -1,5 +1,3 @@ -import json - from monday.resources.base import BaseResource from monday.query_joins import get_users_query @@ -10,4 +8,4 @@ def __init__(self, token): def fetch_users(self, **kwargs): query = get_users_query(**kwargs) - return json.loads(self.client.execute(query)) + return self.client.execute(query) From b580806b77083fd7e5d389eeedfc787fe7e77277 Mon Sep 17 00:00:00 2001 From: Lemuel Boyce Date: Wed, 19 May 2021 12:02:26 -0400 Subject: [PATCH 3/4] automate publishing to pypi also some code cleanup --- .github/publish.yml | 26 ++++++++++++++++++++++++++ monday/query_joins.py | 6 +++--- monday/resources/base.py | 2 -- monday/utils.py | 1 + setup.py | 1 + 5 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 .github/publish.yml diff --git a/.github/publish.yml b/.github/publish.yml new file mode 100644 index 0000000..1a0c825 --- /dev/null +++ b/.github/publish.yml @@ -0,0 +1,26 @@ +name: Publish Package +on: + release: + types: [ created ] +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.7' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pipenv + pipenv install --dev + pip install setuptools wheel twine + - name: Build and publish + env: + TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} + TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} + run: | + python setup.py sdist bdist_wheel + twine upload dist/* diff --git a/monday/query_joins.py b/monday/query_joins.py index c1c8c1e..937f14b 100644 --- a/monday/query_joins.py +++ b/monday/query_joins.py @@ -1,4 +1,5 @@ import json + from monday.utils import monday_json_stringify @@ -121,8 +122,7 @@ def update_item_query(board_id, item_id, column_id, value): def update_multiple_column_values_query(board_id, item_id, column_values): - - query = '''mutation + query = '''mutation { change_multiple_column_values ( board_id: %s, @@ -138,7 +138,7 @@ def update_multiple_column_values_query(board_id, item_id, column_values): } }''' % (board_id, item_id, monday_json_stringify(column_values)) - return query + return query def add_file_to_column_query(item_id, column_id): diff --git a/monday/resources/base.py b/monday/resources/base.py index d35b7e0..cd39a4e 100644 --- a/monday/resources/base.py +++ b/monday/resources/base.py @@ -1,5 +1,3 @@ -from monday import __version__ -from monday.exceptions import MondayError from monday.graphqlclient.client import GraphQLClient _URLS = { diff --git a/monday/utils.py b/monday/utils.py index f1f0f8b..f9fd33a 100644 --- a/monday/utils.py +++ b/monday/utils.py @@ -1,5 +1,6 @@ import json + def monday_json_stringify(value): # This is necessary because Monday's API says that it requires a JSON encoded string for JSON values, # ala JSON.stringify, which doesn't quite work the same as json.dumps, unfortunately. diff --git a/setup.py b/setup.py index 02cf173..5503473 100644 --- a/setup.py +++ b/setup.py @@ -23,5 +23,6 @@ "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", "Operating System :: OS Independent", ]) From 669bb0e780ace11b491283cc444cf5cad79da6a3 Mon Sep 17 00:00:00 2001 From: Lemuel Boyce Date: Wed, 19 May 2021 13:52:45 -0400 Subject: [PATCH 4/4] publish.yml should be nested in .github/workflows --- .github/{ => workflows}/publish.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/{ => workflows}/publish.yml (100%) diff --git a/.github/publish.yml b/.github/workflows/publish.yml similarity index 100% rename from .github/publish.yml rename to .github/workflows/publish.yml