Repository: SebRut/pygrocy
Pull request #46 (2019-12-09 13:05:19)

Update shopping list functions
## Description

Update remove product from shopping list
Add add product to shopping list

**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


BlueBlueBlob: Last commit (2/2)

Patch:
diff --git a/.gitignore b/.gitignore
index 8e937d8..288a087 100644
--- a/.gitignore
+++ b/.gitignore
@@ -93,4 +93,7 @@ modules.xml
 
 venv/
 __pycache__
-html/
\ No newline at end of file
+html/
+build/
+pygrocy.egg-info/
+
diff --git a/pygrocy/grocy.py b/pygrocy/grocy.py
index 9c71573..12296ac 100644
--- a/pygrocy/grocy.py
+++ b/pygrocy/grocy.py
@@ -232,11 +232,14 @@ class Grocy(object):
     def add_missing_product_to_shopping_list(self, shopping_list_id: int = 1):
         return self._api_client.add_missing_product_to_shopping_list(shopping_list_id)
         
+    def add_product_to_shopping_list(self, product_id: int, shopping_list_id: int = 1, amount: int = 1):
+        return self._api_client.add_product_to_shopping_list(product_id, shopping_list_id, amount)
+        
     def clear_shopping_list(self, shopping_list_id: int = 1):
         return self._api_client.clear_shopping_list(shopping_list_id)
 
-    def remove_product_in_shopping_list(self, shopping_list_product_id: int):
-        return self._api_client.remove_product_in_sl(shopping_list_product_id)
+    def remove_product_in_shopping_list(self, product_id: int, shopping_list_id: int = 1, amount: int = 1):
+        return self._api_client.remove_product_in_shopping_list(product_id, shopping_list_id, amount)
         
     def product_groups(self) -> List[Group]:
         raw_groups = self._api_client.get_product_groups()
diff --git a/pygrocy/grocy_api_client.py b/pygrocy/grocy_api_client.py
index 11568cb..0cdaf93 100644
--- a/pygrocy/grocy_api_client.py
+++ b/pygrocy/grocy_api_client.py
@@ -386,7 +386,18 @@ class GrocyApiClient(object):
         req_url = urljoin(self._base_url, "stock/shoppinglist/add-missing-products")
         resp = requests.post(req_url, verify=self._verify_ssl, headers=self._headers, data=data)
         return resp
-            
+    
+    def add_product_to_shopping_list(self, product_id: int, shopping_list_id: int = 1, amount: int = 1):
+        data = {
+            "product_id": product_id,
+            "list_id": shopping_list_id,
+            "product_amount": amount
+        }
+
+        req_url = urljoin(self._base_url, "stock/shoppinglist/add-product")
+        resp = requests.post(req_url, verify=self._verify_ssl, headers=self._headers, data=data)
+        return resp
+    
     def clear_shopping_list(self, shopping_list_id: int = 1):
         data = {
             "list_id": shopping_list_id
@@ -396,10 +407,14 @@ class GrocyApiClient(object):
         resp = requests.post(req_url, verify=self._verify_ssl, headers=self._headers, data=data)
         return resp
             
-    def remove_product_in_sl(self, sl_product_id: int):
-        
-        req_url = urljoin(urljoin(self._base_url, "objects/shopping_list/"), str(sl_product_id))
-        resp = requests.delete(req_url, verify=self._verify_ssl, headers=self._headers)
+    def remove_product_in_shopping_list(self, product_id: int, shopping_list_id: int = 1, amount: int = 1):
+        data = {
+            "product_id": product_id,
+            "list_id": shopping_list_id,
+            "product_amount": amount
+        }
+        req_url = urljoin(self._base_url, "stock/shoppinglist/remove-product")
+        resp = requests.post(req_url, verify=self._verify_ssl, headers=self._headers, data=data)
         return resp
         
     def get_product_groups(self) -> List[LocationData]:
@@ -453,3 +468,4 @@ class GrocyApiClient(object):
         last_change_timestamp = parse_date(resp.json().get('changed_time'))
         return last_change_timestamp
         
+       


Test patch:
diff --git a/test/test_grocy.py b/test/test_grocy.py
index 22871ee..bf01f21 100644
--- a/test/test_grocy.py
+++ b/test/test_grocy.py
@@ -212,6 +212,16 @@ class TestGrocy(TestCase):
         responses.add(responses.POST, "https://example.com:9192/api/stock/shoppinglist/add-missing-products", status=400)
         assert self.grocy.add_missing_product_to_shopping_list().status_code != 204
         
+    @responses.activate
+    def test_add_product_to_shopping_list_valid(self):
+        responses.add(responses.POST, "https://example.com:9192/api/stock/shoppinglist/add-product", status=204)
+        assert self.grocy.add_product_to_shopping_list(1).status_code == 204
+        
+    @responses.activate
+    def test_add_product_to_shopping_list_error(self):
+        responses.add(responses.POST, "https://example.com:9192/api/stock/shoppinglist/add-product", status=400)
+        assert self.grocy.add_product_to_shopping_list(1).status_code != 204
+        
     @responses.activate
     def test_clear_shopping_list_valid(self):
         responses.add(responses.POST, "https://example.com:9192/api/stock/shoppinglist/clear", status=204)
@@ -224,12 +234,12 @@ class TestGrocy(TestCase):
         
     @responses.activate
     def test_remove_product_in_shopping_list_valid(self):
-        responses.add(responses.DELETE, "https://example.com:9192/api/objects/shopping_list/1", status=204)
+        responses.add(responses.POST, "https://example.com:9192/api/stock/shoppinglist/remove-product", status=204)
         assert self.grocy.remove_product_in_shopping_list(1).status_code == 204
         
     @responses.activate
     def test_remove_product_in_shopping_list_error(self):
-        responses.add(responses.DELETE, "https://example.com:9192/api/objects/shopping_list/1", status=400)
+        responses.add(responses.POST, "https://example.com:9192/api/stock/shoppinglist/remove-product", status=400)
         assert self.grocy.remove_product_in_shopping_list(1).status_code != 204
         
     @responses.activate