Repository: hetznercloud/hcloud-python

Issue:
Allow Endpoint to be set
We should support the same Client Configuration as the hcloud-go. 

So we should also allow the user to customize the endpoint, instead of hardcoding it.



Discussion:

Patch:
diff --git a/.travis.yml b/.travis.yml
index 3524174..eb82890 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,7 +1,6 @@
 language: python
 python:
   - "2.7"
-  - "3.4"
   - "3.5"
   - "3.6"
 matrix:
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index be90790..1c5a58d 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -100,7 +100,7 @@ Before you submit a pull request, check that it meets these guidelines:
 2. If the pull request adds functionality, the docs should be updated. Put
    your new functionality into a function with a docstring, and add the
    feature to the list in README.rst.
-3. The pull request should work for Python 2.7, 3.4, 3.5 and 3.6, and for PyPy. Check
+3. The pull request should work for Python 2.7, 3.5 and 3.6, and for PyPy. Check
    https://travis-ci.org/hetznercloud/hetznercloud-python/pull_requests
    and make sure that the tests pass for all supported Python versions.
 
diff --git a/hcloud/hcloud.py b/hcloud/hcloud.py
index 4e20f24..c580534 100644
--- a/hcloud/hcloud.py
+++ b/hcloud/hcloud.py
@@ -15,7 +15,7 @@ from hcloud.images.client import ImagesClient
 from hcloud.locations.client import LocationsClient
 from hcloud.datacenters.client import DatacentersClient
 
-from .version import VERSION
+from .version import VERSION, USER_AGENT_PREFIX
 
 
 class HcloudAPIException(Exception):
@@ -29,9 +29,11 @@ class HcloudClient(object):
     version = VERSION
     retry_wait_time = 0.5
 
-    def __init__(self, token):
+    def __init__(self, token, api_endpoint="https://api.hetzner.cloud/v1", application_name=None, application_version=None):
         self.token = token
-        self.api_endpoint = "https://api.hetzner.cloud/v1"
+        self._api_endpoint = api_endpoint
+        self._application_name = application_name
+        self._application_version = application_version
 
         self.datacenters = DatacentersClient(self)
         self.locations = LocationsClient(self)
@@ -45,7 +47,18 @@ class HcloudClient(object):
         self.floating_ips = FloatingIPsClient(self)
 
     def _get_user_agent(self):
-        return "hcloud-python/" + self.version
+        if self._application_name is not None and self._application_version is None:
+            return "{application_name} {prefix}/{version}".format(application_name=self._application_name,
+                                                                  prefix=USER_AGENT_PREFIX,
+                                                                  version=self.version)
+        elif self._application_name is not None and self._application_version is not None:
+            return "{application_name}/{application_version} {prefix}/{version}".format(
+                application_name=self._application_name,
+                application_version=self._application_version,
+                prefix=USER_AGENT_PREFIX,
+                version=self.version)
+        else:
+            return "{prefix}/{version}".format(prefix=USER_AGENT_PREFIX, version=self.version)
 
     def _get_headers(self):
         headers = {
@@ -73,7 +86,7 @@ class HcloudClient(object):
     def request(self, method, url, tries=1, **kwargs):
         response = requests.request(
             method,
-            self.api_endpoint + url,
+            self._api_endpoint + url,
             headers=self._get_headers(),
             **kwargs
         )
diff --git a/hcloud/version.py b/hcloud/version.py
index 1f6518e..471dc57 100644
--- a/hcloud/version.py
+++ b/hcloud/version.py
@@ -1,1 +1,2 @@
 VERSION = '0.1.0'
+USER_AGENT_PREFIX = 'hcloud-python'


Test patch:
diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py
index 5d87f6e..3caffc2 100644
--- a/tests/integration/conftest.py
+++ b/tests/integration/conftest.py
@@ -6,6 +6,5 @@ from hcloud import HcloudClient
 
 @pytest.fixture(autouse=True, scope='function')
 def hetzner_client():
-    hetzner_client = HcloudClient(token="test-token")
-    hetzner_client.api_endpoint = os.getenv("FAKE_API_ENDPOINT", default="http://localhost:8080")
+    hetzner_client = HcloudClient(token="test-token", api_endpoint=os.getenv("FAKE_API_ENDPOINT", default="http://localhost:4000"))
     return hetzner_client
diff --git a/tests/unit/test_hcloud.py b/tests/unit/test_hcloud.py
index 2215c11..df1996f 100644
--- a/tests/unit/test_hcloud.py
+++ b/tests/unit/test_hcloud.py
@@ -55,6 +55,16 @@ class TestHetznerClient(object):
         user_agent = client._get_user_agent()
         assert user_agent == "hcloud-python/0.0.0"
 
+    def test__get_user_agent_with_application_name(self, client):
+        client = HcloudClient(token="project_token", application_name="my-app")
+        user_agent = client._get_user_agent()
+        assert user_agent == "my-app hcloud-python/0.0.0"
+
+    def test__get_user_agent_with_application_name_and_version(self, client):
+        client = HcloudClient(token="project_token", application_name="my-app", application_version="1.0.0")
+        user_agent = client._get_user_agent()
+        assert user_agent == "my-app/1.0.0 hcloud-python/0.0.0"
+
     def test__get_headers(self, client):
         headers = client._get_headers()
         assert headers == {