Repository: SebRut/pygrocy
Pull request #30 (2019-09-17 12:47:35)

Userfields exposed
## Description

Userfields API, read/write but no delete or new one

**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 13a9b7e..6e953de 100644
--- a/pygrocy/grocy.py
+++ b/pygrocy/grocy.py
@@ -249,3 +249,10 @@ class Grocy(object):
             return
         return self._api_client.update_product_pic(product_id)
         
+    def get_userfields(self, entity: str, object_id: int):
+        return self._api_client.get_userfields(entity, object_id)
+        
+    def set_userfields(self, entity: str, object_id: int, key: str, value):
+        return self._api_client.set_userfields(entity, object_id, key, value)
+        
+        
\ No newline at end of file
diff --git a/pygrocy/grocy_api_client.py b/pygrocy/grocy_api_client.py
index f73aac1..a181945 100644
--- a/pygrocy/grocy_api_client.py
+++ b/pygrocy/grocy_api_client.py
@@ -430,3 +430,17 @@ class GrocyApiClient(object):
         resp = requests.put(req_url, verify=self._verify_ssl, headers=up_header , data=json.dumps(data))
         return resp
             
+    def get_userfields(self, entity: str, object_id: int):
+        req_url = urljoin(urljoin(urljoin(self._base_url, "userfields/"), entity + "/"), str(object_id))
+        resp = requests.get(req_url, verify=self._verify_ssl, headers=self._headers)
+        if not resp:
+            return resp
+        return resp.json()
+        
+    def set_userfields(self, entity: str, object_id: int, key: str, value):
+        data = {
+            key: value
+        }
+        req_url = urljoin(urljoin(urljoin(self._base_url, "userfields/"), entity + "/"), str(object_id))
+        resp = requests.put(req_url, verify=self._verify_ssl, headers=self._headers, data=data)
+        return resp


Test patch:
diff --git a/test/test_grocy.py b/test/test_grocy.py
index bc8c7f9..cd1d14e 100644
--- a/test/test_grocy.py
+++ b/test/test_grocy.py
@@ -381,3 +381,34 @@ class TestGrocy(TestCase):
         resp = {}
         responses.add(responses.GET, "https://example.com/api/stock/volatile", json=resp, status=200)
         
+    @responses.activate
+    def test_get_userfields_valid(self):
+        resp =  {
+                "uf1": 0,
+                "uf2": "string"
+            }
+        
+        responses.add(responses.GET, "https://example.com/api/userfields/chores/1", json=resp, status=200)
+
+        a_chore_uf = self.grocy.get_userfields("chores",1)
+
+        assert a_chore_uf['uf1'] == 0
+
+
+    @responses.activate
+    def test_get_userfields_invalid_no_data(self):
+        resp = []
+        responses.add(responses.GET, "https://example.com/api/userfields/chores/1", json=resp ,status=200)
+
+        assert not self.grocy.get_userfields("chores",1) 
+
+    @responses.activate
+    def test_set_userfields_valid(self):
+        responses.add(responses.PUT, "https://example.com/api/userfields/chores/1", status=204)
+        assert self.grocy.set_userfields("chores",1,"auserfield","value").status_code == 204
+        
+    @responses.activate
+    def test_set_userfields_error(self):
+        responses.add(responses.PUT, "https://example.com/api/userfields/chores/1", status=400)
+        assert self.grocy.set_userfields("chores",1,"auserfield","value").status_code != 204
+        
\ No newline at end of file