Repository: SebRut/pygrocy
Pull request #115 (2020-08-14 19:05:17)

Add endpoint for adding generic entity
## Description

Add endpoint for https://demo.grocy.info/api#/Generic%20entity%20interactions/post_objects__entity_

**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


pull-assistant[bot]: 
<img align="right" src="https://pullassistant.com/assets/score10.svg" alt="Score: 1.00"/>

Best reviewed:  **commit by commit** 
___
<details>
<summary>
Optimal code review plan 
<br/>
<br/>
</summary>
   
    
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="https://github.com/SebRut/pygrocy/pull/115/commits/fd36ac899f618f386388ce69925673a2043cbc83">Add endpoint for adding generic entity</a>
   
   
</details>

> Powered by [Pull Assistant](https://pullassistant.com). Last update [fd36ac8](https://github.com/SebRut/pygrocy/commit/fd36ac899f618f386388ce69925673a2043cbc83) ... [fd36ac8](https://github.com/SebRut/pygrocy/commit/fd36ac899f618f386388ce69925673a2043cbc83). Read the [comment docs](https://pullassistant.com).


Patch:
diff --git a/pygrocy/grocy.py b/pygrocy/grocy.py
index 5506f68..d7b7090 100644
--- a/pygrocy/grocy.py
+++ b/pygrocy/grocy.py
@@ -607,3 +607,6 @@ class Grocy(object):
         recipe = self._api_client.get_recipe(recipe_id)
         if recipe:
             return RecipeItem(recipe)
+
+    def add_generic(self, entity_type, data):
+        return self._api_client.add_generic(entity_type, data)
diff --git a/pygrocy/grocy_api_client.py b/pygrocy/grocy_api_client.py
index aa4e7cd..4ffe858 100644
--- a/pygrocy/grocy_api_client.py
+++ b/pygrocy/grocy_api_client.py
@@ -672,3 +672,6 @@ class GrocyApiClient(object):
         parsed_json = self._do_get_request(f"objects/recipes/{object_id}")
         if parsed_json:
             return RecipeDetailsResponse(parsed_json)
+
+    def add_generic(self, entity_type: str, data: object):
+        self._do_post_request(f"objects/{entity_type}", data)


Test patch:
diff --git a/test/test_grocy.py b/test/test_grocy.py
index 2a742cc..c89676a 100644
--- a/test/test_grocy.py
+++ b/test/test_grocy.py
@@ -21,6 +21,7 @@ class TestGrocy(TestCase):
         )
         self.base_url = f"{CONST_BASE_URL}:{CONST_PORT}/api"
         self.date_test = datetime.strptime("2019-05-04 11:31:04", "%Y-%m-%d %H:%M:%S")
+        self.add_generic_data = {"name": "This is a task"}
 
     def test_init(self):
         self.assertIsInstance(self.grocy, Grocy)
@@ -432,6 +433,22 @@ class TestGrocy(TestCase):
             HTTPError, self.grocy.add_product, 1, 1.3, 2.44, self.date_test
         )
 
+    @responses.activate
+    def test_add_generic_valid(self):
+        responses.add(
+            responses.POST, f"{self.base_url}/objects/tasks", status=200
+        )
+        self.assertIsNone(self.grocy.add_generic("tasks", self.add_generic_data))
+
+    @responses.activate
+    def test_add_generic_error(self):
+        responses.add(
+            responses.POST, f"{self.base_url}/objects/tasks", status=400
+        )
+        self.assertRaises(
+            HTTPError, self.grocy.add_generic, "tasks", self.add_generic_data
+        )
+
     @responses.activate
     def test_consume_product_valid(self):
         responses.add(