pikostack 1.0.0__tar.gz
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-1.0.0/PKG-INFO +61 -0
- pikostack-1.0.0/README.md +47 -0
- pikostack-1.0.0/pyproject.toml +28 -0
- pikostack-1.0.0/setup.cfg +4 -0
- pikostack-1.0.0/src/pikostack/__init__.py +18 -0
- pikostack-1.0.0/src/pikostack/_transport.py +174 -0
- pikostack-1.0.0/src/pikostack/_version.py +3 -0
- pikostack-1.0.0/src/pikostack/client.py +40776 -0
- pikostack-1.0.0/src/pikostack/models.py +35084 -0
- pikostack-1.0.0/src/pikostack/py.typed +0 -0
- pikostack-1.0.0/src/pikostack.egg-info/PKG-INFO +61 -0
- pikostack-1.0.0/src/pikostack.egg-info/SOURCES.txt +13 -0
- pikostack-1.0.0/src/pikostack.egg-info/dependency_links.txt +1 -0
- pikostack-1.0.0/src/pikostack.egg-info/top_level.txt +1 -0
- pikostack-1.0.0/tests/test_client.py +130 -0
pikostack-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pikostack
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Official Python client of the PikoStack API
|
|
5
|
+
Author: PikoStack
|
|
6
|
+
License-Expression: LicenseRef-Proprietary
|
|
7
|
+
Project-URL: Homepage, https://pikostack.com
|
|
8
|
+
Project-URL: Documentation, https://docs.pikostack.com
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Typing :: Typed
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# PikoStack Python SDK
|
|
16
|
+
|
|
17
|
+
The official Python client of the PikoStack API: one method per call of the
|
|
18
|
+
published API description, typed answers (`TypedDict`), no dependencies
|
|
19
|
+
outside the standard library. Python 3.10 or newer.
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
pip install pikostack
|
|
23
|
+
# or from the mirror:
|
|
24
|
+
pip install --index-url https://mirror.pikostack.com/sdk/python/simple/ pikostack
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
import os
|
|
29
|
+
from pikostack import Client, PikoStackError
|
|
30
|
+
|
|
31
|
+
c = Client("https://panel.example.com", token=os.environ["PIKO_TOKEN"])
|
|
32
|
+
|
|
33
|
+
for s in c.list_servers(status="running")["servers"]:
|
|
34
|
+
print(s["id"], s["name"])
|
|
35
|
+
|
|
36
|
+
out = c.create_server(
|
|
37
|
+
{"name": "web-1", "plan_id": "piko.small", "template_id": "ubuntu-24.04"},
|
|
38
|
+
idempotency_key="order-1234", # a retry returns the first answer
|
|
39
|
+
)
|
|
40
|
+
print(out["server"]["id"], out.get("root_password"))
|
|
41
|
+
|
|
42
|
+
try:
|
|
43
|
+
c.get_server("srv-missing")
|
|
44
|
+
except PikoStackError as e:
|
|
45
|
+
if e.not_found:
|
|
46
|
+
print("no such server", e.request_id)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
- Method names are the operation IDs of the API description in snake_case
|
|
50
|
+
(`listServers` is `list_servers`); path parameters are positional, the
|
|
51
|
+
JSON body is a dict, query parameters are keyword arguments.
|
|
52
|
+
- Calls that return a file answer a `RawResponse` (`status`, `headers`,
|
|
53
|
+
`body`); uploads take `bytes` or a binary file.
|
|
54
|
+
- `Client.request()` and `Client.request_raw()` reach calls added to the API
|
|
55
|
+
after this release.
|
|
56
|
+
- A panel with a private certificate authority: pass
|
|
57
|
+
`ssl_context=ssl.create_default_context(cafile="ca.pem")`.
|
|
58
|
+
|
|
59
|
+
The code in `client.py` and `models.py` is generated by
|
|
60
|
+
`pikod openapi write`; do not edit it by hand. Tests:
|
|
61
|
+
`python3 -m unittest discover -s tests`. More examples: `examples/`.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# PikoStack Python SDK
|
|
2
|
+
|
|
3
|
+
The official Python client of the PikoStack API: one method per call of the
|
|
4
|
+
published API description, typed answers (`TypedDict`), no dependencies
|
|
5
|
+
outside the standard library. Python 3.10 or newer.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pip install pikostack
|
|
9
|
+
# or from the mirror:
|
|
10
|
+
pip install --index-url https://mirror.pikostack.com/sdk/python/simple/ pikostack
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
import os
|
|
15
|
+
from pikostack import Client, PikoStackError
|
|
16
|
+
|
|
17
|
+
c = Client("https://panel.example.com", token=os.environ["PIKO_TOKEN"])
|
|
18
|
+
|
|
19
|
+
for s in c.list_servers(status="running")["servers"]:
|
|
20
|
+
print(s["id"], s["name"])
|
|
21
|
+
|
|
22
|
+
out = c.create_server(
|
|
23
|
+
{"name": "web-1", "plan_id": "piko.small", "template_id": "ubuntu-24.04"},
|
|
24
|
+
idempotency_key="order-1234", # a retry returns the first answer
|
|
25
|
+
)
|
|
26
|
+
print(out["server"]["id"], out.get("root_password"))
|
|
27
|
+
|
|
28
|
+
try:
|
|
29
|
+
c.get_server("srv-missing")
|
|
30
|
+
except PikoStackError as e:
|
|
31
|
+
if e.not_found:
|
|
32
|
+
print("no such server", e.request_id)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- Method names are the operation IDs of the API description in snake_case
|
|
36
|
+
(`listServers` is `list_servers`); path parameters are positional, the
|
|
37
|
+
JSON body is a dict, query parameters are keyword arguments.
|
|
38
|
+
- Calls that return a file answer a `RawResponse` (`status`, `headers`,
|
|
39
|
+
`body`); uploads take `bytes` or a binary file.
|
|
40
|
+
- `Client.request()` and `Client.request_raw()` reach calls added to the API
|
|
41
|
+
after this release.
|
|
42
|
+
- A panel with a private certificate authority: pass
|
|
43
|
+
`ssl_context=ssl.create_default_context(cafile="ca.pem")`.
|
|
44
|
+
|
|
45
|
+
The code in `client.py` and `models.py` is generated by
|
|
46
|
+
`pikod openapi write`; do not edit it by hand. Tests:
|
|
47
|
+
`python3 -m unittest discover -s tests`. More examples: `examples/`.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pikostack"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Official Python client of the PikoStack API"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "LicenseRef-Proprietary"
|
|
12
|
+
authors = [{ name = "PikoStack" }]
|
|
13
|
+
dependencies = []
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Typing :: Typed",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
Homepage = "https://pikostack.com"
|
|
22
|
+
Documentation = "https://docs.pikostack.com"
|
|
23
|
+
|
|
24
|
+
[tool.setuptools.packages.find]
|
|
25
|
+
where = ["src"]
|
|
26
|
+
|
|
27
|
+
[tool.setuptools.package-data]
|
|
28
|
+
pikostack = ["py.typed"]
|
|
@@ -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"]
|
|
@@ -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)
|