Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions hcloud/certificates/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class CertificatesPageResult(NamedTuple):


class CertificatesClient(ResourceClientBase):
_base_url = "/certificates"

actions: ResourceActionsClient
"""Certificates scoped actions client
Expand All @@ -113,15 +114,15 @@ class CertificatesClient(ResourceClientBase):

def __init__(self, client: Client):
super().__init__(client)
self.actions = ResourceActionsClient(client, "/certificates")
self.actions = ResourceActionsClient(client, self._base_url)

def get_by_id(self, id: int) -> BoundCertificate:
"""Get a specific certificate by its ID.

:param id: int
:return: :class:`BoundCertificate <hcloud.certificates.client.BoundCertificate>`
"""
response = self._client.request(url=f"/certificates/{id}", method="GET")
response = self._client.request(url=f"{self._base_url}/{id}", method="GET")
return BoundCertificate(self, response["certificate"])

def get_list(
Expand Down Expand Up @@ -156,9 +157,7 @@ def get_list(
if per_page is not None:
params["per_page"] = per_page

response = self._client.request(
url="/certificates", method="GET", params=params
)
response = self._client.request(url=self._base_url, method="GET", params=params)

certificates = [
BoundCertificate(self, certificate_data)
Expand Down Expand Up @@ -218,7 +217,7 @@ def create(
}
if labels is not None:
data["labels"] = labels
response = self._client.request(url="/certificates", method="POST", json=data)
response = self._client.request(url=self._base_url, method="POST", json=data)
return BoundCertificate(self, response["certificate"])

def create_managed(
Expand All @@ -244,7 +243,7 @@ def create_managed(
}
if labels is not None:
data["labels"] = labels
response = self._client.request(url="/certificates", method="POST", json=data)
response = self._client.request(url=self._base_url, method="POST", json=data)
return CreateManagedCertificateResponse(
certificate=BoundCertificate(self, response["certificate"]),
action=BoundAction(self._parent.actions, response["action"]),
Expand All @@ -271,7 +270,7 @@ def update(
if labels is not None:
data["labels"] = labels
response = self._client.request(
url=f"/certificates/{certificate.id}",
url=f"{self._base_url}/{certificate.id}",
method="PUT",
json=data,
)
Expand All @@ -284,7 +283,7 @@ def delete(self, certificate: Certificate | BoundCertificate) -> bool:
:return: True
"""
self._client.request(
url=f"/certificates/{certificate.id}",
url=f"{self._base_url}/{certificate.id}",
method="DELETE",
)
# Return always true, because the API does not return an action for it. When an error occurs a HcloudAPIException will be raised
Expand Down Expand Up @@ -322,7 +321,7 @@ def get_actions_list(
params["per_page"] = per_page

response = self._client.request(
url=f"/certificates/{certificate.id}/actions",
url=f"{self._base_url}/{certificate.id}/actions",
method="GET",
params=params,
)
Expand Down Expand Up @@ -364,7 +363,7 @@ def retry_issuance(
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
"""
response = self._client.request(
url=f"/certificates/{certificate.id}/actions/retry",
url=f"{self._base_url}/{certificate.id}/actions/retry",
method="POST",
)
return BoundAction(self._parent.actions, response["action"])
3 changes: 2 additions & 1 deletion hcloud/core/client.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from __future__ import annotations

import warnings
from typing import TYPE_CHECKING, Any, Callable
from typing import TYPE_CHECKING, Any, Callable, ClassVar

if TYPE_CHECKING:
from .._client import Client, ClientBase
from .domain import BaseDomain


class ResourceClientBase:
_base_url: ClassVar[str]
_parent: Client
_client: ClientBase

Expand Down
5 changes: 3 additions & 2 deletions hcloud/datacenters/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ class DatacentersPageResult(NamedTuple):


class DatacentersClient(ResourceClientBase):
_base_url = "/datacenters"

def get_by_id(self, id: int) -> BoundDatacenter:
"""Get a specific datacenter by its ID.

:param id: int
:return: :class:`BoundDatacenter <hcloud.datacenters.client.BoundDatacenter>`
"""
response = self._client.request(url=f"/datacenters/{id}", method="GET")
response = self._client.request(url=f"{self._base_url}/{id}", method="GET")
return BoundDatacenter(self, response["datacenter"])

def get_list(
Expand Down Expand Up @@ -89,7 +90,7 @@ def get_list(
if per_page is not None:
params["per_page"] = per_page

response = self._client.request(url="/datacenters", method="GET", params=params)
response = self._client.request(url=self._base_url, method="GET", params=params)

datacenters = [
BoundDatacenter(self, datacenter_data)
Expand Down
21 changes: 11 additions & 10 deletions hcloud/firewalls/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class FirewallsPageResult(NamedTuple):


class FirewallsClient(ResourceClientBase):
_base_url = "/firewalls"

actions: ResourceActionsClient
"""Firewalls scoped actions client
Expand All @@ -193,7 +194,7 @@ class FirewallsClient(ResourceClientBase):

def __init__(self, client: Client):
super().__init__(client)
self.actions = ResourceActionsClient(client, "/firewalls")
self.actions = ResourceActionsClient(client, self._base_url)

def get_actions_list(
self,
Expand Down Expand Up @@ -226,7 +227,7 @@ def get_actions_list(
if per_page is not None:
params["per_page"] = per_page
response = self._client.request(
url=f"/firewalls/{firewall.id}/actions",
url=f"{self._base_url}/{firewall.id}/actions",
method="GET",
params=params,
)
Expand Down Expand Up @@ -265,7 +266,7 @@ def get_by_id(self, id: int) -> BoundFirewall:
:param id: int
:return: :class:`BoundFirewall <hcloud.firewalls.client.BoundFirewall>`
"""
response = self._client.request(url=f"/firewalls/{id}", method="GET")
response = self._client.request(url=f"{self._base_url}/{id}", method="GET")
return BoundFirewall(self, response["firewall"])

def get_list(
Expand Down Expand Up @@ -302,7 +303,7 @@ def get_list(
params["name"] = name
if sort is not None:
params["sort"] = sort
response = self._client.request(url="/firewalls", method="GET", params=params)
response = self._client.request(url=self._base_url, method="GET", params=params)
firewalls = [
BoundFirewall(self, firewall_data)
for firewall_data in response["firewalls"]
Expand Down Expand Up @@ -372,7 +373,7 @@ def create(
data.update({"apply_to": []})
for resource in resources:
data["apply_to"].append(resource.to_payload())
response = self._client.request(url="/firewalls", json=data, method="POST")
response = self._client.request(url=self._base_url, json=data, method="POST")

actions = []
if response.get("actions") is not None:
Expand Down Expand Up @@ -408,7 +409,7 @@ def update(
data["name"] = name

response = self._client.request(
url=f"/firewalls/{firewall.id}",
url=f"{self._base_url}/{firewall.id}",
method="PUT",
json=data,
)
Expand All @@ -421,7 +422,7 @@ def delete(self, firewall: Firewall | BoundFirewall) -> bool:
:return: boolean
"""
self._client.request(
url=f"/firewalls/{firewall.id}",
url=f"{self._base_url}/{firewall.id}",
method="DELETE",
)
# Return always true, because the API does not return an action for it. When an error occurs a HcloudAPIException will be raised
Expand All @@ -442,7 +443,7 @@ def set_rules(
for rule in rules:
data["rules"].append(rule.to_payload())
response = self._client.request(
url=f"/firewalls/{firewall.id}/actions/set_rules",
url=f"{self._base_url}/{firewall.id}/actions/set_rules",
method="POST",
json=data,
)
Expand All @@ -466,7 +467,7 @@ def apply_to_resources(
for resource in resources:
data["apply_to"].append(resource.to_payload())
response = self._client.request(
url=f"/firewalls/{firewall.id}/actions/apply_to_resources",
url=f"{self._base_url}/{firewall.id}/actions/apply_to_resources",
method="POST",
json=data,
)
Expand All @@ -490,7 +491,7 @@ def remove_from_resources(
for resource in resources:
data["remove_from"].append(resource.to_payload())
response = self._client.request(
url=f"/firewalls/{firewall.id}/actions/remove_from_resources",
url=f"{self._base_url}/{firewall.id}/actions/remove_from_resources",
method="POST",
json=data,
)
Expand Down
25 changes: 12 additions & 13 deletions hcloud/floating_ips/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class FloatingIPsPageResult(NamedTuple):


class FloatingIPsClient(ResourceClientBase):
_base_url = "/floating_ips"

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

def __init__(self, client: Client):
super().__init__(client)
self.actions = ResourceActionsClient(client, "/floating_ips")
self.actions = ResourceActionsClient(client, self._base_url)

def get_actions_list(
self,
Expand Down Expand Up @@ -182,7 +183,7 @@ def get_actions_list(
if per_page is not None:
params["per_page"] = per_page
response = self._client.request(
url=f"/floating_ips/{floating_ip.id}/actions",
url=f"{self._base_url}/{floating_ip.id}/actions",
method="GET",
params=params,
)
Expand Down Expand Up @@ -221,7 +222,7 @@ def get_by_id(self, id: int) -> BoundFloatingIP:
:param id: int
:return: :class:`BoundFloatingIP <hcloud.floating_ips.client.BoundFloatingIP>`
"""
response = self._client.request(url=f"/floating_ips/{id}", method="GET")
response = self._client.request(url=f"{self._base_url}/{id}", method="GET")
return BoundFloatingIP(self, response["floating_ip"])

def get_list(
Expand Down Expand Up @@ -254,9 +255,7 @@ def get_list(
if name is not None:
params["name"] = name

response = self._client.request(
url="/floating_ips", method="GET", params=params
)
response = self._client.request(url=self._base_url, method="GET", params=params)
floating_ips = [
BoundFloatingIP(self, floating_ip_data)
for floating_ip_data in response["floating_ips"]
Expand Down Expand Up @@ -324,7 +323,7 @@ def create(
if name is not None:
data["name"] = name

response = self._client.request(url="/floating_ips", json=data, method="POST")
response = self._client.request(url=self._base_url, json=data, method="POST")

action = None
if response.get("action") is not None:
Expand Down Expand Up @@ -362,7 +361,7 @@ def update(
data["name"] = name

response = self._client.request(
url=f"/floating_ips/{floating_ip.id}",
url=f"{self._base_url}/{floating_ip.id}",
method="PUT",
json=data,
)
Expand All @@ -375,7 +374,7 @@ def delete(self, floating_ip: FloatingIP | BoundFloatingIP) -> bool:
:return: boolean
"""
self._client.request(
url=f"/floating_ips/{floating_ip.id}",
url=f"{self._base_url}/{floating_ip.id}",
method="DELETE",
)
# Return always true, because the API does not return an action for it. When an error occurs a HcloudAPIException will be raised
Expand All @@ -398,7 +397,7 @@ def change_protection(
data.update({"delete": delete})

response = self._client.request(
url=f"/floating_ips/{floating_ip.id}/actions/change_protection",
url=f"{self._base_url}/{floating_ip.id}/actions/change_protection",
method="POST",
json=data,
)
Expand All @@ -417,7 +416,7 @@ def assign(
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
"""
response = self._client.request(
url=f"/floating_ips/{floating_ip.id}/actions/assign",
url=f"{self._base_url}/{floating_ip.id}/actions/assign",
method="POST",
json={"server": server.id},
)
Expand All @@ -430,7 +429,7 @@ def unassign(self, floating_ip: FloatingIP | BoundFloatingIP) -> BoundAction:
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
"""
response = self._client.request(
url=f"/floating_ips/{floating_ip.id}/actions/unassign",
url=f"{self._base_url}/{floating_ip.id}/actions/unassign",
method="POST",
)
return BoundAction(self._parent.actions, response["action"])
Expand All @@ -451,7 +450,7 @@ def change_dns_ptr(
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
"""
response = self._client.request(
url=f"/floating_ips/{floating_ip.id}/actions/change_dns_ptr",
url=f"{self._base_url}/{floating_ip.id}/actions/change_dns_ptr",
method="POST",
json={"ip": ip, "dns_ptr": dns_ptr},
)
Expand Down
Loading