Repository: SebRut/pygrocy (python)

Issue:
Add path in request when creating base url
If grocy in not host at the root of a web server but in an other directory every request failed due to the base_url not complete.
This is done in line https://github.com/SebRut/pygrocy/blob/8787acc6d4f5f16169894810e21ba09b0b4f831d/pygrocy/grocy_api_client.py#L294

In this case the port may be add behind a full path like http://grocy.localdomain/grocy/public/:80/api/stock
Wich can't be available.

Pull request description:
support path for url
fixes #121 

Patch:
diff --git a/pygrocy/grocy.py b/pygrocy/grocy.py
index ed5a5f9..cd1a559 100644
--- a/pygrocy/grocy.py
+++ b/pygrocy/grocy.py
@@ -36,10 +36,13 @@ class Grocy(object):
         base_url,
         api_key,
         port: int = DEFAULT_PORT_NUMBER,
+        path: str = None,
         verify_ssl=True,
         debug=False,
     ):
-        self._api_client = GrocyApiClient(base_url, api_key, port, verify_ssl, debug)
+        self._api_client = GrocyApiClient(
+            base_url, api_key, port, path, verify_ssl, debug
+        )
 
         if debug:
             _LOGGER.setLevel(logging.DEBUG)
diff --git a/pygrocy/grocy_api_client.py b/pygrocy/grocy_api_client.py
index ddf4ddb..8999160 100644
--- a/pygrocy/grocy_api_client.py
+++ b/pygrocy/grocy_api_client.py
@@ -233,13 +233,17 @@ class GrocyApiClient(object):
         base_url,
         api_key,
         port: int = DEFAULT_PORT_NUMBER,
+        path: str = None,
         verify_ssl=True,
         debug=False,
     ):
         if debug:
             _enable_debug_mode()
 
-        self._base_url = "{}:{}/api/".format(base_url, port)
+        if path:
+            self._base_url = "{}:{}/{}/api/".format(base_url, port, path)
+        else:
+            self._base_url = "{}:{}/api/".format(base_url, port)
         _LOGGER.debug(f"generated base url: {self._base_url}")
 
         self._api_key = api_key


Test patch:
diff --git a/test/test_grocy_api_client.py b/test/test_grocy_api_client.py
new file mode 100644
index 0000000..4b7f311
--- /dev/null
+++ b/test/test_grocy_api_client.py
@@ -0,0 +1,23 @@
+from pygrocy.grocy_api_client import GrocyApiClient
+
+
+class TestGrocyApiClient:
+    def test_url_only(self):
+        client = GrocyApiClient(api_key="", base_url="http://grocy.de")
+        assert client._base_url == "http://grocy.de:9192/api/"
+
+    def test_url_and_port(self):
+        client = GrocyApiClient(api_key="", base_url="http://grocy.de", port=1234)
+        assert client._base_url == "http://grocy.de:1234/api/"
+
+    def test_url_and_port_and_path(self):
+        client = GrocyApiClient(
+            api_key="", base_url="http://grocy.de", port=1234, path="my/custom/path"
+        )
+        assert client._base_url == "http://grocy.de:1234/my/custom/path/api/"
+
+    def test_url_and_path(self):
+        client = GrocyApiClient(
+            api_key="", base_url="http://grocy.de", path="my/custom/path"
+        )
+        assert client._base_url == "http://grocy.de:9192/my/custom/path/api/"