Skip to content

Commit d66ce7c

Browse files
authored
Add possibility to configure endpoint and application name (#9)
* Add possibility to set the application name #6 Add possibility to configure the endpoint #7 * Allow fluent usage of with_endpoint and with_application Fix some issues from review * Fix tests * remove with_* methods * remove with_endpoint test
1 parent ccb2e3b commit d66ce7c

4 files changed

Lines changed: 30 additions & 7 deletions

File tree

hcloud/hcloud.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from hcloud.locations.client import LocationsClient
1616
from hcloud.datacenters.client import DatacentersClient
1717

18-
from .version import VERSION
18+
from .version import VERSION, USER_AGENT_PREFIX
1919

2020

2121
class HcloudAPIException(Exception):
@@ -29,9 +29,11 @@ class HcloudClient(object):
2929
version = VERSION
3030
retry_wait_time = 0.5
3131

32-
def __init__(self, token):
32+
def __init__(self, token, api_endpoint="https://api.hetzner.cloud/v1", application_name=None, application_version=None):
3333
self.token = token
34-
self.api_endpoint = "https://api.hetzner.cloud/v1"
34+
self._api_endpoint = api_endpoint
35+
self._application_name = application_name
36+
self._application_version = application_version
3537

3638
self.datacenters = DatacentersClient(self)
3739
self.locations = LocationsClient(self)
@@ -45,7 +47,18 @@ def __init__(self, token):
4547
self.floating_ips = FloatingIPsClient(self)
4648

4749
def _get_user_agent(self):
48-
return "hcloud-python/" + self.version
50+
if self._application_name is not None and self._application_version is None:
51+
return "{application_name} {prefix}/{version}".format(application_name=self._application_name,
52+
prefix=USER_AGENT_PREFIX,
53+
version=self.version)
54+
elif self._application_name is not None and self._application_version is not None:
55+
return "{application_name}/{application_version} {prefix}/{version}".format(
56+
application_name=self._application_name,
57+
application_version=self._application_version,
58+
prefix=USER_AGENT_PREFIX,
59+
version=self.version)
60+
else:
61+
return "{prefix}/{version}".format(prefix=USER_AGENT_PREFIX, version=self.version)
4962

5063
def _get_headers(self):
5164
headers = {
@@ -73,7 +86,7 @@ def _raise_exception_from_json_content(self, json_content):
7386
def request(self, method, url, tries=1, **kwargs):
7487
response = requests.request(
7588
method,
76-
self.api_endpoint + url,
89+
self._api_endpoint + url,
7790
headers=self._get_headers(),
7891
**kwargs
7992
)

hcloud/version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
VERSION = '0.1.0'
2+
USER_AGENT_PREFIX = 'hcloud-python'

tests/integration/conftest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@
66

77
@pytest.fixture(autouse=True, scope='function')
88
def hetzner_client():
9-
hetzner_client = HcloudClient(token="test-token")
10-
hetzner_client.api_endpoint = os.getenv("FAKE_API_ENDPOINT", default="http://localhost:8080")
9+
hetzner_client = HcloudClient(token="test-token", api_endpoint=os.getenv("FAKE_API_ENDPOINT", default="http://localhost:4000"))
1110
return hetzner_client

tests/unit/test_hcloud.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ def test__get_user_agent(self, client):
5555
user_agent = client._get_user_agent()
5656
assert user_agent == "hcloud-python/0.0.0"
5757

58+
def test__get_user_agent_with_application_name(self, client):
59+
client = HcloudClient(token="project_token", application_name="my-app")
60+
user_agent = client._get_user_agent()
61+
assert user_agent == "my-app hcloud-python/0.0.0"
62+
63+
def test__get_user_agent_with_application_name_and_version(self, client):
64+
client = HcloudClient(token="project_token", application_name="my-app", application_version="1.0.0")
65+
user_agent = client._get_user_agent()
66+
assert user_agent == "my-app/1.0.0 hcloud-python/0.0.0"
67+
5868
def test__get_headers(self, client):
5969
headers = client._get_headers()
6070
assert headers == {

0 commit comments

Comments
 (0)