Repository: SebRut/pygrocy
Pull request #233 (2022-05-07 17:46:47)

Add get task method
## Description

It adds a get task method to retrieve a single task from Grocy, without having to return the whole list. It uses the GET `objects/tasks/{task_id}` endpoint.

**Related issue (if applicable):** fixes #<issue number goes here>

## Checklist
  - [x] The code change is tested and works locally.
  - [x] tests pass. **Your PR won't be merged unless tests pass**
  - [x] There is no commented out code in this PR



Patch:
diff --git a/pygrocy/grocy.py b/pygrocy/grocy.py
index 3ad311b..9fdcf57 100644
--- a/pygrocy/grocy.py
+++ b/pygrocy/grocy.py
@@ -285,6 +285,10 @@ class Grocy(object):
         raw_tasks = self._api_client.get_tasks()
         return [Task(task) for task in raw_tasks]
 
+    def task(self, task_id: int) -> Task:
+        resp = self._api_client.get_task(task_id)
+        return Task(resp)
+
     def complete_task(self, task_id, done_time: datetime = datetime.now()):
         return self._api_client.complete_task(task_id, done_time)
 
diff --git a/pygrocy/grocy_api_client.py b/pygrocy/grocy_api_client.py
index 7a162dc..30ba1a1 100644
--- a/pygrocy/grocy_api_client.py
+++ b/pygrocy/grocy_api_client.py
@@ -572,7 +572,6 @@ class GrocyApiClient(object):
         }
         if quantity_unit_id:
             data["qu_id"] = quantity_unit_id
-        print(data)
         self._do_post_request("stock/shoppinglist/add-product", data)
 
     def clear_shopping_list(self, shopping_list_id: int = 1):
@@ -622,6 +621,11 @@ class GrocyApiClient(object):
         parsed_json = self._do_get_request("tasks")
         return [TaskResponse(**data) for data in parsed_json]
 
+    def get_task(self, task_id: int) -> TaskResponse:
+        url = f"objects/tasks/{task_id}"
+        parsed_json = self._do_get_request(url)
+        return TaskResponse(**parsed_json)
+
     def complete_task(self, task_id: int, done_time: datetime = datetime.now()):
         url = f"tasks/{task_id}/complete"
 


Test patch:
diff --git a/test/cassettes/test_tasks/TestTasks.test_get_task_valid.yaml b/test/cassettes/test_tasks/TestTasks.test_get_task_valid.yaml
new file mode 100644
index 0000000..826b8ff
--- /dev/null
+++ b/test/cassettes/test_tasks/TestTasks.test_get_task_valid.yaml
@@ -0,0 +1,43 @@
+interactions:
+- request:
+    body: null
+    headers:
+      Accept-Encoding:
+      - gzip, deflate
+      Connection:
+      - keep-alive
+      User-Agent:
+      - python-requests/2.27.1
+      accept:
+      - application/json
+    method: GET
+    uri: https://localhost/api/objects/tasks/2
+  response:
+    body:
+      string: '{"id":"2","name":"Task2","description":null,"due_date":"2022-04-21","done":"0","done_timestamp":null,"category_id":"1","assigned_to_user_id":"1","row_created_timestamp":"2022-04-22
+        08:37:58","assigned_to_user":{"id":"1","username":"Demo User","first_name":null,"last_name":null,"row_created_timestamp":"2022-04-22
+        08:37:55","display_name":"Demo User","picture_file_name":null},"category":{"id":"1","name":"Category1","description":null,"row_created_timestamp":"2022-04-22
+        08:37:58"}}'
+    headers:
+      Access-Control-Allow-Headers:
+      - '*'
+      Access-Control-Allow-Methods:
+      - GET, POST, PUT, DELETE, OPTIONS
+      Access-Control-Allow-Origin:
+      - '*'
+      Connection:
+      - keep-alive
+      Content-Type:
+      - application/json
+      Date:
+      - Fri, 22 Apr 2022 08:40:26 GMT
+      Server:
+      - nginx/1.20.2
+      Transfer-Encoding:
+      - chunked
+      X-Powered-By:
+      - PHP/8.0.13
+    status:
+      code: 200
+      message: OK
+version: 1
diff --git a/test/test_tasks.py b/test/test_tasks.py
index 0fb045b..0dd149b 100644
--- a/test/test_tasks.py
+++ b/test/test_tasks.py
@@ -21,6 +21,17 @@ class TestTasks:
         assert task.category.id == 1
         assert task.category.name == "Category1"
 
+    @pytest.mark.vcr
+    def test_get_task_valid(self, grocy):
+        task = grocy.task(2)
+
+        assert task.id == 2
+        assert task.name == "Task2"
+        assert isinstance(task.assigned_to_user, User)
+        assert isinstance(task.category, TaskCategory)
+        assert task.category.id == 1
+        assert task.category.name == "Category1"
+
     @pytest.mark.vcr
     def test_complete_task_valid_with_defaults(self, grocy):
         grocy.complete_task(3)