2023-08-08 16:17:22 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING, Any, NamedTuple
|
|
|
|
|
|
|
|
from ..core import BoundModelBase, ClientEntityBase, Meta
|
2023-07-11 09:15:08 +00:00
|
|
|
from .domain import Location
|
|
|
|
|
2023-08-08 16:17:22 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .._client import Client
|
|
|
|
|
2023-07-11 09:15:08 +00:00
|
|
|
|
2023-09-25 11:34:49 +00:00
|
|
|
class BoundLocation(BoundModelBase, Location):
|
2023-08-08 16:17:22 +00:00
|
|
|
_client: LocationsClient
|
|
|
|
|
2023-07-11 09:15:08 +00:00
|
|
|
model = Location
|
|
|
|
|
|
|
|
|
2023-08-08 16:17:22 +00:00
|
|
|
class LocationsPageResult(NamedTuple):
|
|
|
|
locations: list[BoundLocation]
|
|
|
|
meta: Meta | None
|
|
|
|
|
|
|
|
|
|
|
|
class LocationsClient(ClientEntityBase):
|
|
|
|
_client: Client
|
2023-07-11 09:15:08 +00:00
|
|
|
|
2023-08-08 16:17:22 +00:00
|
|
|
def get_by_id(self, id: int) -> BoundLocation:
|
2023-07-11 09:15:08 +00:00
|
|
|
"""Get a specific location by its ID.
|
|
|
|
|
|
|
|
:param id: int
|
|
|
|
:return: :class:`BoundLocation <hcloud.locations.client.BoundLocation>`
|
|
|
|
"""
|
|
|
|
response = self._client.request(url=f"/locations/{id}", method="GET")
|
|
|
|
return BoundLocation(self, response["location"])
|
|
|
|
|
2023-08-08 16:17:22 +00:00
|
|
|
def get_list(
|
|
|
|
self,
|
|
|
|
name: str | None = None,
|
|
|
|
page: int | None = None,
|
|
|
|
per_page: int | None = None,
|
|
|
|
) -> LocationsPageResult:
|
2023-07-11 09:15:08 +00:00
|
|
|
"""Get a list of locations
|
|
|
|
|
|
|
|
:param name: str (optional)
|
|
|
|
Can be used to filter locations by their name.
|
|
|
|
:param page: int (optional)
|
|
|
|
Specifies the page to fetch
|
|
|
|
:param per_page: int (optional)
|
|
|
|
Specifies how many results are returned by page
|
|
|
|
:return: (List[:class:`BoundLocation <hcloud.locations.client.BoundLocation>`], :class:`Meta <hcloud.core.domain.Meta>`)
|
|
|
|
"""
|
2023-08-08 16:17:22 +00:00
|
|
|
params: dict[str, Any] = {}
|
2023-07-11 09:15:08 +00:00
|
|
|
if name is not None:
|
|
|
|
params["name"] = name
|
|
|
|
if page is not None:
|
|
|
|
params["page"] = page
|
|
|
|
if per_page is not None:
|
|
|
|
params["per_page"] = per_page
|
|
|
|
|
|
|
|
response = self._client.request(url="/locations", method="GET", params=params)
|
|
|
|
locations = [
|
|
|
|
BoundLocation(self, location_data)
|
|
|
|
for location_data in response["locations"]
|
|
|
|
]
|
2023-08-08 16:17:22 +00:00
|
|
|
return LocationsPageResult(locations, Meta.parse_meta(response))
|
2023-07-11 09:15:08 +00:00
|
|
|
|
2023-08-08 16:17:22 +00:00
|
|
|
def get_all(self, name: str | None = None) -> list[BoundLocation]:
|
2023-07-11 09:15:08 +00:00
|
|
|
"""Get all locations
|
|
|
|
|
|
|
|
:param name: str (optional)
|
|
|
|
Can be used to filter locations by their name.
|
|
|
|
:return: List[:class:`BoundLocation <hcloud.locations.client.BoundLocation>`]
|
|
|
|
"""
|
2023-08-08 16:17:22 +00:00
|
|
|
return self._iter_pages(self.get_list, name=name)
|
2023-07-11 09:15:08 +00:00
|
|
|
|
2023-08-08 16:17:22 +00:00
|
|
|
def get_by_name(self, name: str) -> BoundLocation | None:
|
2023-07-11 09:15:08 +00:00
|
|
|
"""Get location by name
|
|
|
|
|
|
|
|
:param name: str
|
|
|
|
Used to get location by name.
|
|
|
|
:return: :class:`BoundLocation <hcloud.locations.client.BoundLocation>`
|
|
|
|
"""
|
2023-08-08 16:17:22 +00:00
|
|
|
return self._get_first_by(name=name)
|