-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathclient.py
More file actions
82 lines (63 loc) · 2.58 KB
/
client.py
File metadata and controls
82 lines (63 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from __future__ import annotations
from typing import TYPE_CHECKING, Any, NamedTuple
from ..core import BoundModelBase, ClientEntityBase, Meta
from .domain import Location
if TYPE_CHECKING:
from .._client import Client
class BoundLocation(BoundModelBase, Location):
_client: LocationsClient
model = Location
class LocationsPageResult(NamedTuple):
locations: list[BoundLocation]
meta: Meta
class LocationsClient(ClientEntityBase):
_client: Client
def get_by_id(self, id: int) -> BoundLocation:
"""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"])
def get_list(
self,
name: str | None = None,
page: int | None = None,
per_page: int | None = None,
) -> LocationsPageResult:
"""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>`)
"""
params: dict[str, Any] = {}
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"]
]
return LocationsPageResult(locations, Meta.parse_meta(response))
def get_all(self, name: str | None = None) -> list[BoundLocation]:
"""Get all locations
:param name: str (optional)
Can be used to filter locations by their name.
:return: List[:class:`BoundLocation <hcloud.locations.client.BoundLocation>`]
"""
return self._iter_pages(self.get_list, name=name)
def get_by_name(self, name: str) -> BoundLocation | None:
"""Get location by name
:param name: str
Used to get location by name.
:return: :class:`BoundLocation <hcloud.locations.client.BoundLocation>`
"""
return self._get_first_by(name=name)