Skip to content

Commit 2e9e89d

Browse files
committed
refactor: add resource clients base_url
This property will become powerfull with the addition of the Resource Actions Client Mixin.
1 parent 8de4107 commit 2e9e89d

File tree

17 files changed

+163
-158
lines changed

17 files changed

+163
-158
lines changed

hcloud/certificates/client.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class CertificatesPageResult(NamedTuple):
104104

105105

106106
class CertificatesClient(ResourceClientBase):
107+
_base_url = "/certificates"
107108

108109
actions: ResourceActionsClient
109110
"""Certificates scoped actions client
@@ -113,15 +114,15 @@ class CertificatesClient(ResourceClientBase):
113114

114115
def __init__(self, client: Client):
115116
super().__init__(client)
116-
self.actions = ResourceActionsClient(client, "/certificates")
117+
self.actions = ResourceActionsClient(client, self._base_url)
117118

118119
def get_by_id(self, id: int) -> BoundCertificate:
119120
"""Get a specific certificate by its ID.
120121
121122
:param id: int
122123
:return: :class:`BoundCertificate <hcloud.certificates.client.BoundCertificate>`
123124
"""
124-
response = self._client.request(url=f"/certificates/{id}", method="GET")
125+
response = self._client.request(url=f"{self._base_url}/{id}", method="GET")
125126
return BoundCertificate(self, response["certificate"])
126127

127128
def get_list(
@@ -156,9 +157,7 @@ def get_list(
156157
if per_page is not None:
157158
params["per_page"] = per_page
158159

159-
response = self._client.request(
160-
url="/certificates", method="GET", params=params
161-
)
160+
response = self._client.request(url=self._base_url, method="GET", params=params)
162161

163162
certificates = [
164163
BoundCertificate(self, certificate_data)
@@ -218,7 +217,7 @@ def create(
218217
}
219218
if labels is not None:
220219
data["labels"] = labels
221-
response = self._client.request(url="/certificates", method="POST", json=data)
220+
response = self._client.request(url=self._base_url, method="POST", json=data)
222221
return BoundCertificate(self, response["certificate"])
223222

224223
def create_managed(
@@ -244,7 +243,7 @@ def create_managed(
244243
}
245244
if labels is not None:
246245
data["labels"] = labels
247-
response = self._client.request(url="/certificates", method="POST", json=data)
246+
response = self._client.request(url=self._base_url, method="POST", json=data)
248247
return CreateManagedCertificateResponse(
249248
certificate=BoundCertificate(self, response["certificate"]),
250249
action=BoundAction(self._parent.actions, response["action"]),
@@ -271,7 +270,7 @@ def update(
271270
if labels is not None:
272271
data["labels"] = labels
273272
response = self._client.request(
274-
url=f"/certificates/{certificate.id}",
273+
url=f"{self._base_url}/{certificate.id}",
275274
method="PUT",
276275
json=data,
277276
)
@@ -284,7 +283,7 @@ def delete(self, certificate: Certificate | BoundCertificate) -> bool:
284283
:return: True
285284
"""
286285
self._client.request(
287-
url=f"/certificates/{certificate.id}",
286+
url=f"{self._base_url}/{certificate.id}",
288287
method="DELETE",
289288
)
290289
# Return always true, because the API does not return an action for it. When an error occurs a HcloudAPIException will be raised
@@ -322,7 +321,7 @@ def get_actions_list(
322321
params["per_page"] = per_page
323322

324323
response = self._client.request(
325-
url=f"/certificates/{certificate.id}/actions",
324+
url=f"{self._base_url}/{certificate.id}/actions",
326325
method="GET",
327326
params=params,
328327
)
@@ -364,7 +363,7 @@ def retry_issuance(
364363
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
365364
"""
366365
response = self._client.request(
367-
url=f"/certificates/{certificate.id}/actions/retry",
366+
url=f"{self._base_url}/{certificate.id}/actions/retry",
368367
method="POST",
369368
)
370369
return BoundAction(self._parent.actions, response["action"])

hcloud/core/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
from __future__ import annotations
22

33
import warnings
4-
from typing import TYPE_CHECKING, Any, Callable
4+
from typing import TYPE_CHECKING, Any, Callable, ClassVar
55

66
if TYPE_CHECKING:
77
from .._client import Client, ClientBase
88
from .domain import BaseDomain
99

1010

1111
class ResourceClientBase:
12+
_base_url: ClassVar[str]
1213
_parent: Client
1314
_client: ClientBase
1415

hcloud/datacenters/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ class DatacentersPageResult(NamedTuple):
5353

5454

5555
class DatacentersClient(ResourceClientBase):
56+
_base_url = "/datacenters"
5657

5758
def get_by_id(self, id: int) -> BoundDatacenter:
5859
"""Get a specific datacenter by its ID.
5960
6061
:param id: int
6162
:return: :class:`BoundDatacenter <hcloud.datacenters.client.BoundDatacenter>`
6263
"""
63-
response = self._client.request(url=f"/datacenters/{id}", method="GET")
64+
response = self._client.request(url=f"{self._base_url}/{id}", method="GET")
6465
return BoundDatacenter(self, response["datacenter"])
6566

6667
def get_list(
@@ -89,7 +90,7 @@ def get_list(
8990
if per_page is not None:
9091
params["per_page"] = per_page
9192

92-
response = self._client.request(url="/datacenters", method="GET", params=params)
93+
response = self._client.request(url=self._base_url, method="GET", params=params)
9394

9495
datacenters = [
9596
BoundDatacenter(self, datacenter_data)

hcloud/firewalls/client.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ class FirewallsPageResult(NamedTuple):
184184

185185

186186
class FirewallsClient(ResourceClientBase):
187+
_base_url = "/firewalls"
187188

188189
actions: ResourceActionsClient
189190
"""Firewalls scoped actions client
@@ -193,7 +194,7 @@ class FirewallsClient(ResourceClientBase):
193194

194195
def __init__(self, client: Client):
195196
super().__init__(client)
196-
self.actions = ResourceActionsClient(client, "/firewalls")
197+
self.actions = ResourceActionsClient(client, self._base_url)
197198

198199
def get_actions_list(
199200
self,
@@ -226,7 +227,7 @@ def get_actions_list(
226227
if per_page is not None:
227228
params["per_page"] = per_page
228229
response = self._client.request(
229-
url=f"/firewalls/{firewall.id}/actions",
230+
url=f"{self._base_url}/{firewall.id}/actions",
230231
method="GET",
231232
params=params,
232233
)
@@ -265,7 +266,7 @@ def get_by_id(self, id: int) -> BoundFirewall:
265266
:param id: int
266267
:return: :class:`BoundFirewall <hcloud.firewalls.client.BoundFirewall>`
267268
"""
268-
response = self._client.request(url=f"/firewalls/{id}", method="GET")
269+
response = self._client.request(url=f"{self._base_url}/{id}", method="GET")
269270
return BoundFirewall(self, response["firewall"])
270271

271272
def get_list(
@@ -302,7 +303,7 @@ def get_list(
302303
params["name"] = name
303304
if sort is not None:
304305
params["sort"] = sort
305-
response = self._client.request(url="/firewalls", method="GET", params=params)
306+
response = self._client.request(url=self._base_url, method="GET", params=params)
306307
firewalls = [
307308
BoundFirewall(self, firewall_data)
308309
for firewall_data in response["firewalls"]
@@ -372,7 +373,7 @@ def create(
372373
data.update({"apply_to": []})
373374
for resource in resources:
374375
data["apply_to"].append(resource.to_payload())
375-
response = self._client.request(url="/firewalls", json=data, method="POST")
376+
response = self._client.request(url=self._base_url, json=data, method="POST")
376377

377378
actions = []
378379
if response.get("actions") is not None:
@@ -408,7 +409,7 @@ def update(
408409
data["name"] = name
409410

410411
response = self._client.request(
411-
url=f"/firewalls/{firewall.id}",
412+
url=f"{self._base_url}/{firewall.id}",
412413
method="PUT",
413414
json=data,
414415
)
@@ -421,7 +422,7 @@ def delete(self, firewall: Firewall | BoundFirewall) -> bool:
421422
:return: boolean
422423
"""
423424
self._client.request(
424-
url=f"/firewalls/{firewall.id}",
425+
url=f"{self._base_url}/{firewall.id}",
425426
method="DELETE",
426427
)
427428
# Return always true, because the API does not return an action for it. When an error occurs a HcloudAPIException will be raised
@@ -442,7 +443,7 @@ def set_rules(
442443
for rule in rules:
443444
data["rules"].append(rule.to_payload())
444445
response = self._client.request(
445-
url=f"/firewalls/{firewall.id}/actions/set_rules",
446+
url=f"{self._base_url}/{firewall.id}/actions/set_rules",
446447
method="POST",
447448
json=data,
448449
)
@@ -466,7 +467,7 @@ def apply_to_resources(
466467
for resource in resources:
467468
data["apply_to"].append(resource.to_payload())
468469
response = self._client.request(
469-
url=f"/firewalls/{firewall.id}/actions/apply_to_resources",
470+
url=f"{self._base_url}/{firewall.id}/actions/apply_to_resources",
470471
method="POST",
471472
json=data,
472473
)
@@ -490,7 +491,7 @@ def remove_from_resources(
490491
for resource in resources:
491492
data["remove_from"].append(resource.to_payload())
492493
response = self._client.request(
493-
url=f"/firewalls/{firewall.id}/actions/remove_from_resources",
494+
url=f"{self._base_url}/{firewall.id}/actions/remove_from_resources",
494495
method="POST",
495496
json=data,
496497
)

hcloud/floating_ips/client.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class FloatingIPsPageResult(NamedTuple):
140140

141141

142142
class FloatingIPsClient(ResourceClientBase):
143+
_base_url = "/floating_ips"
143144

144145
actions: ResourceActionsClient
145146
"""Floating IPs scoped actions client
@@ -149,7 +150,7 @@ class FloatingIPsClient(ResourceClientBase):
149150

150151
def __init__(self, client: Client):
151152
super().__init__(client)
152-
self.actions = ResourceActionsClient(client, "/floating_ips")
153+
self.actions = ResourceActionsClient(client, self._base_url)
153154

154155
def get_actions_list(
155156
self,
@@ -182,7 +183,7 @@ def get_actions_list(
182183
if per_page is not None:
183184
params["per_page"] = per_page
184185
response = self._client.request(
185-
url=f"/floating_ips/{floating_ip.id}/actions",
186+
url=f"{self._base_url}/{floating_ip.id}/actions",
186187
method="GET",
187188
params=params,
188189
)
@@ -221,7 +222,7 @@ def get_by_id(self, id: int) -> BoundFloatingIP:
221222
:param id: int
222223
:return: :class:`BoundFloatingIP <hcloud.floating_ips.client.BoundFloatingIP>`
223224
"""
224-
response = self._client.request(url=f"/floating_ips/{id}", method="GET")
225+
response = self._client.request(url=f"{self._base_url}/{id}", method="GET")
225226
return BoundFloatingIP(self, response["floating_ip"])
226227

227228
def get_list(
@@ -254,9 +255,7 @@ def get_list(
254255
if name is not None:
255256
params["name"] = name
256257

257-
response = self._client.request(
258-
url="/floating_ips", method="GET", params=params
259-
)
258+
response = self._client.request(url=self._base_url, method="GET", params=params)
260259
floating_ips = [
261260
BoundFloatingIP(self, floating_ip_data)
262261
for floating_ip_data in response["floating_ips"]
@@ -324,7 +323,7 @@ def create(
324323
if name is not None:
325324
data["name"] = name
326325

327-
response = self._client.request(url="/floating_ips", json=data, method="POST")
326+
response = self._client.request(url=self._base_url, json=data, method="POST")
328327

329328
action = None
330329
if response.get("action") is not None:
@@ -362,7 +361,7 @@ def update(
362361
data["name"] = name
363362

364363
response = self._client.request(
365-
url=f"/floating_ips/{floating_ip.id}",
364+
url=f"{self._base_url}/{floating_ip.id}",
366365
method="PUT",
367366
json=data,
368367
)
@@ -375,7 +374,7 @@ def delete(self, floating_ip: FloatingIP | BoundFloatingIP) -> bool:
375374
:return: boolean
376375
"""
377376
self._client.request(
378-
url=f"/floating_ips/{floating_ip.id}",
377+
url=f"{self._base_url}/{floating_ip.id}",
379378
method="DELETE",
380379
)
381380
# Return always true, because the API does not return an action for it. When an error occurs a HcloudAPIException will be raised
@@ -398,7 +397,7 @@ def change_protection(
398397
data.update({"delete": delete})
399398

400399
response = self._client.request(
401-
url=f"/floating_ips/{floating_ip.id}/actions/change_protection",
400+
url=f"{self._base_url}/{floating_ip.id}/actions/change_protection",
402401
method="POST",
403402
json=data,
404403
)
@@ -417,7 +416,7 @@ def assign(
417416
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
418417
"""
419418
response = self._client.request(
420-
url=f"/floating_ips/{floating_ip.id}/actions/assign",
419+
url=f"{self._base_url}/{floating_ip.id}/actions/assign",
421420
method="POST",
422421
json={"server": server.id},
423422
)
@@ -430,7 +429,7 @@ def unassign(self, floating_ip: FloatingIP | BoundFloatingIP) -> BoundAction:
430429
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
431430
"""
432431
response = self._client.request(
433-
url=f"/floating_ips/{floating_ip.id}/actions/unassign",
432+
url=f"{self._base_url}/{floating_ip.id}/actions/unassign",
434433
method="POST",
435434
)
436435
return BoundAction(self._parent.actions, response["action"])
@@ -451,7 +450,7 @@ def change_dns_ptr(
451450
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
452451
"""
453452
response = self._client.request(
454-
url=f"/floating_ips/{floating_ip.id}/actions/change_dns_ptr",
453+
url=f"{self._base_url}/{floating_ip.id}/actions/change_dns_ptr",
455454
method="POST",
456455
json={"ip": ip, "dns_ptr": dns_ptr},
457456
)

0 commit comments

Comments
 (0)