pikostack 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pikostack/__init__.py +18 -0
- pikostack/_transport.py +174 -0
- pikostack/_version.py +3 -0
- pikostack/client.py +40776 -0
- pikostack/models.py +35084 -0
- pikostack/py.typed +0 -0
- pikostack-1.0.0.dist-info/METADATA +61 -0
- pikostack-1.0.0.dist-info/RECORD +10 -0
- pikostack-1.0.0.dist-info/WHEEL +5 -0
- pikostack-1.0.0.dist-info/top_level.txt +1 -0
pikostack/__init__.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Official Python client of the PikoStack API.
|
|
2
|
+
|
|
3
|
+
from pikostack import Client
|
|
4
|
+
|
|
5
|
+
c = Client("https://panel.example.com", token=os.environ["PIKO_TOKEN"])
|
|
6
|
+
for s in c.list_servers(status="running")["servers"]:
|
|
7
|
+
print(s["name"])
|
|
8
|
+
|
|
9
|
+
The methods of Client are generated from the published API description;
|
|
10
|
+
see https://docs.pikostack.com for the API reference.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from . import models
|
|
14
|
+
from ._transport import PikoStackError, RawResponse
|
|
15
|
+
from ._version import __version__
|
|
16
|
+
from .client import API_VERSION, Client
|
|
17
|
+
|
|
18
|
+
__all__ = ["API_VERSION", "Client", "PikoStackError", "RawResponse", "__version__", "models"]
|
pikostack/_transport.py
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"""HTTP transport of the client (standard library only)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import ssl
|
|
7
|
+
import urllib.error
|
|
8
|
+
import urllib.request
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from typing import IO, Any, Mapping
|
|
11
|
+
from urllib.parse import urlencode
|
|
12
|
+
|
|
13
|
+
from ._version import __version__
|
|
14
|
+
|
|
15
|
+
__all__ = ["PikoStackError", "RawResponse", "Transport"]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class PikoStackError(Exception):
|
|
19
|
+
"""An error answered by the API: ``{"error": {"code", "message"}}``."""
|
|
20
|
+
|
|
21
|
+
def __init__(self, status: int, code: str, message: str, request_id: str | None = None, body: bytes = b"") -> None:
|
|
22
|
+
super().__init__(f"pikostack: {message} ({status} {code})")
|
|
23
|
+
self.status = status
|
|
24
|
+
self.code = code
|
|
25
|
+
self.message = message
|
|
26
|
+
self.request_id = request_id
|
|
27
|
+
self.body = body
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def not_found(self) -> bool:
|
|
31
|
+
return self.status == 404
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def conflict(self) -> bool:
|
|
35
|
+
return self.status == 409
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass
|
|
39
|
+
class RawResponse:
|
|
40
|
+
"""An answer that is not JSON (files, archives, consoles)."""
|
|
41
|
+
|
|
42
|
+
status: int
|
|
43
|
+
headers: dict[str, str] = field(default_factory=dict)
|
|
44
|
+
body: bytes = b""
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def content_type(self) -> str:
|
|
48
|
+
return self.headers.get("content-type", "")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class Transport:
|
|
52
|
+
"""Sends calls to one panel with one API token.
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
base_url: the panel address, e.g. ``https://panel.example.com``.
|
|
56
|
+
token: an API token from Account, API tokens.
|
|
57
|
+
timeout: seconds per call.
|
|
58
|
+
headers: extra headers on every call.
|
|
59
|
+
ssl_context: for a panel with a private certificate authority.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
def __init__(
|
|
63
|
+
self,
|
|
64
|
+
base_url: str,
|
|
65
|
+
token: str | None = None,
|
|
66
|
+
*,
|
|
67
|
+
timeout: float = 60.0,
|
|
68
|
+
headers: Mapping[str, str] | None = None,
|
|
69
|
+
user_agent: str | None = None,
|
|
70
|
+
ssl_context: ssl.SSLContext | None = None,
|
|
71
|
+
) -> None:
|
|
72
|
+
self.base_url = base_url.rstrip("/")
|
|
73
|
+
self.token = token
|
|
74
|
+
self.timeout = timeout
|
|
75
|
+
self.headers = dict(headers or {})
|
|
76
|
+
self.user_agent = user_agent or f"pikostack-python/{__version__}"
|
|
77
|
+
self.ssl_context = ssl_context
|
|
78
|
+
|
|
79
|
+
def request(
|
|
80
|
+
self,
|
|
81
|
+
method: str,
|
|
82
|
+
path: str,
|
|
83
|
+
*,
|
|
84
|
+
query: Mapping[str, Any] | None = None,
|
|
85
|
+
body: Any = None,
|
|
86
|
+
headers: Mapping[str, str] | None = None,
|
|
87
|
+
idempotency_key: str | None = None,
|
|
88
|
+
) -> Any:
|
|
89
|
+
"""Calls any endpoint by method and path (for calls added to the API
|
|
90
|
+
after this package was released) and returns the decoded answer."""
|
|
91
|
+
return self._call(method, path, query=query, body=body, mode="json", idempotency_key=idempotency_key, headers=headers)
|
|
92
|
+
|
|
93
|
+
def request_raw(
|
|
94
|
+
self,
|
|
95
|
+
method: str,
|
|
96
|
+
path: str,
|
|
97
|
+
*,
|
|
98
|
+
query: Mapping[str, Any] | None = None,
|
|
99
|
+
body: bytes | IO[bytes] | None = None,
|
|
100
|
+
content_type: str = "application/octet-stream",
|
|
101
|
+
headers: Mapping[str, str] | None = None,
|
|
102
|
+
) -> RawResponse:
|
|
103
|
+
"""Calls any endpoint with a raw body and returns the answer unread."""
|
|
104
|
+
return self._call(method, path, query=query, body=body, content_type=content_type if body is not None else None, mode="raw", headers=headers) # type: ignore[no-any-return]
|
|
105
|
+
|
|
106
|
+
def _call(
|
|
107
|
+
self,
|
|
108
|
+
method: str,
|
|
109
|
+
path: str,
|
|
110
|
+
*,
|
|
111
|
+
query: Mapping[str, Any] | None = None,
|
|
112
|
+
body: Any = None,
|
|
113
|
+
content_type: str | None = None,
|
|
114
|
+
mode: str = "json",
|
|
115
|
+
idempotency_key: str | None = None,
|
|
116
|
+
headers: Mapping[str, str] | None = None,
|
|
117
|
+
) -> Any:
|
|
118
|
+
url = self.base_url + path
|
|
119
|
+
q = _query(query)
|
|
120
|
+
if q:
|
|
121
|
+
url += "?" + q
|
|
122
|
+
h = {"Accept": "application/json" if mode == "json" else "*/*", "User-Agent": self.user_agent}
|
|
123
|
+
h.update(self.headers)
|
|
124
|
+
if self.token:
|
|
125
|
+
h["Authorization"] = "Bearer " + self.token
|
|
126
|
+
if idempotency_key:
|
|
127
|
+
h["Idempotency-Key"] = idempotency_key
|
|
128
|
+
data: bytes | None = None
|
|
129
|
+
if content_type is not None:
|
|
130
|
+
data = bytes(body) if isinstance(body, (bytes, bytearray)) else body.read()
|
|
131
|
+
h["Content-Type"] = content_type
|
|
132
|
+
elif body is not None:
|
|
133
|
+
data = json.dumps(body, separators=(",", ":")).encode()
|
|
134
|
+
h["Content-Type"] = "application/json"
|
|
135
|
+
if headers:
|
|
136
|
+
h.update(headers)
|
|
137
|
+
req = urllib.request.Request(url, data=data, method=method, headers=h)
|
|
138
|
+
try:
|
|
139
|
+
with urllib.request.urlopen(req, timeout=self.timeout, context=self.ssl_context) as res:
|
|
140
|
+
status = res.status
|
|
141
|
+
raw = res.read()
|
|
142
|
+
rh = {k.lower(): v for k, v in res.headers.items()}
|
|
143
|
+
except urllib.error.HTTPError as e:
|
|
144
|
+
raise _error(e.code, e.reason, e.headers, e.read()) from None
|
|
145
|
+
if mode == "raw":
|
|
146
|
+
return RawResponse(status, rh, raw)
|
|
147
|
+
if mode == "none" or status == 204 or not raw:
|
|
148
|
+
return None
|
|
149
|
+
return json.loads(raw)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _query(query: Mapping[str, Any] | None) -> str:
|
|
153
|
+
if not query:
|
|
154
|
+
return ""
|
|
155
|
+
pairs = []
|
|
156
|
+
for k, v in query.items():
|
|
157
|
+
if v is None or v == "":
|
|
158
|
+
continue
|
|
159
|
+
if isinstance(v, bool):
|
|
160
|
+
v = "true" if v else "false"
|
|
161
|
+
pairs.append((k, str(v)))
|
|
162
|
+
return urlencode(pairs)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _error(status: int, reason: str, headers: Any, body: bytes) -> PikoStackError:
|
|
166
|
+
code, message = f"http_{status}", str(reason)
|
|
167
|
+
try:
|
|
168
|
+
e = json.loads(body).get("error") or {}
|
|
169
|
+
if e.get("code"):
|
|
170
|
+
code, message = e["code"], e.get("message", message)
|
|
171
|
+
except (ValueError, AttributeError):
|
|
172
|
+
pass
|
|
173
|
+
rid = headers.get("X-Request-ID") if headers is not None else None
|
|
174
|
+
return PikoStackError(status, code, message, rid, body)
|
pikostack/_version.py
ADDED