From c3083a2adf766e5ef669fa8ec05e16a1598c603f Mon Sep 17 00:00:00 2001 From: Matt Manley Date: Thu, 11 Jun 2020 13:46:33 -0700 Subject: [PATCH] Started adding task endpoints --- maproulette/api/maproulette_server.py | 8 ++- maproulette/api/task.py | 95 +++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/maproulette/api/maproulette_server.py b/maproulette/api/maproulette_server.py index dfee469..9a407d2 100644 --- a/maproulette/api/maproulette_server.py +++ b/maproulette/api/maproulette_server.py @@ -182,13 +182,17 @@ def put(self, endpoint, body=None, params=None): "status": response.status_code } - def delete(self, endpoint): + def delete(self, endpoint, params=None): """Method that completes a DELETE request to the MapRoulette API :param endpoint: the server endpoint to use for the DELETE request + :param params: the parameters that pertain to the request (optional) :returns: a JSON object containing the API response """ - response = self.session.delete(self.url + endpoint) + response = self.session.delete( + self.url + endpoint, + params=params + ) try: response.raise_for_status() except requests.exceptions.HTTPError as e: diff --git a/maproulette/api/task.py b/maproulette/api/task.py index f8087a3..70690fd 100644 --- a/maproulette/api/task.py +++ b/maproulette/api/task.py @@ -21,6 +21,101 @@ def get_task_by_id(self, task_id): endpoint=f"/task/{task_id}") return response + def get_task_history(self, task_id): + """Method to retrieve task history using the corresponding task ID + + :param task_id: the ID corresponding with the task + :return: the API response from the GET request + """ + response = self.get( + endpoint=f"/task/{task_id}/history") + return response + + def create_tasks(self, data): + """Method to create a batch of tasks + + :param data: a JSON input containing task details + :return: the API response from the POST request + """ + response = self.post( + endpoint="/tasks", + body=data) + return response + + def update_tasks(self, data): + """Method to update a batch of tasks + + :param data: a JSON input containing task details + :return: the API response from the PUT request + """ + response = self.put( + endpoint="/tasks", + body=data) + return response + + def get_task_tags(self, task_id): + """Method to retrieve the tags for a task using the corresponding task ID + + :param task_id: the ID corresponding with the task + :return: the API response from the GET request + """ + response = self.get( + endpoint=f"/task/{task_id}/tags") + return response + + def delete_task_tags(self, task_id, tags): + """Method to delete the supplied tags from a task using the corresponding task ID + + :param task_id: the ID corresponding with the task + :param tags: a comma-separated list of tags to be deleted + :return: the API response from the DELETE request + """ + query_params = { + "tags": str(tags) + } + response = self.delete( + endpoint=f"/task/{task_id}/tags", + params=query_params + ) + return response + + def get_tasks_by_tags(self, tags, limit=10, page=0): + """Method to retrieve tasks that have the specified tags + + :param tags: a comma-separated list of tags to be searched for + :param limit: the limit to the number of results returned in the response. Default is 10 + :param page: used in conjunction with the limit parameter to page through X number of responses. Default is 0. + :return: the API response from the GET request + """ + query_params = { + "tags": str(tags), + "limit": str(limit), + "page": str(page) + } + response = self.get( + endpoint="/tasks/tags", + params=query_params + ) + return response + + def update_task_tags(self, task_id, tags): + """Method to update a task's tags using the supplied tags and corresponding task ID + + :param task_id: the ID corresponding with the task + :param tags: a comma-separated list of tags to be updated. If empty all tags will be removed. + :return: the API response from the GET request + """ + query_params = { + "tags": str(tags) + } + response = self.get( + endpoint=f"/task/{task_id}/tags/update", + params=query_params + ) + return response + + + @staticmethod def is_task_model(input_object): """Method to determine whether user input is a valid task model