Prepare NPM references and mutation API
This commit is contained in:
@@ -129,6 +129,29 @@ class NpmClient:
|
||||
)
|
||||
return response.json()
|
||||
|
||||
@staticmethod
|
||||
def _json_object(
|
||||
response: requests.Response,
|
||||
label: str,
|
||||
) -> dict[str, Any]:
|
||||
data = response.json()
|
||||
if not isinstance(data, dict):
|
||||
raise RuntimeError(
|
||||
f"NPM {label} response was not an object"
|
||||
)
|
||||
return data
|
||||
|
||||
@staticmethod
|
||||
def _local_id(value: str | int, label: str) -> str:
|
||||
if isinstance(value, bool):
|
||||
raise RuntimeError(f"NPM {label} id is invalid")
|
||||
|
||||
local_id = str(value).strip()
|
||||
if not local_id or not local_id.isdigit():
|
||||
raise RuntimeError(f"NPM {label} id is invalid")
|
||||
|
||||
return local_id
|
||||
|
||||
def proxy_hosts(self) -> list[dict[str, Any]]:
|
||||
data = self.get_json("/nginx/proxy-hosts")
|
||||
|
||||
@@ -148,3 +171,133 @@ class NpmClient:
|
||||
)
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def proxy_host(
|
||||
self,
|
||||
local_id: str | int,
|
||||
) -> dict[str, Any]:
|
||||
host_id = self._local_id(local_id, "proxy host")
|
||||
data = self.get_json(
|
||||
f"/nginx/proxy-hosts/{host_id}"
|
||||
)
|
||||
if not isinstance(data, dict):
|
||||
raise RuntimeError(
|
||||
"NPM proxy-host response was not an object"
|
||||
)
|
||||
return data
|
||||
|
||||
def create_proxy_host(
|
||||
self,
|
||||
payload: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
response = self.request(
|
||||
"POST",
|
||||
"/nginx/proxy-hosts",
|
||||
expected_status=201,
|
||||
json=payload,
|
||||
)
|
||||
return self._json_object(
|
||||
response,
|
||||
"proxy-host create",
|
||||
)
|
||||
|
||||
def update_proxy_host(
|
||||
self,
|
||||
local_id: str | int,
|
||||
payload: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
host_id = self._local_id(local_id, "proxy host")
|
||||
response = self.request(
|
||||
"PUT",
|
||||
f"/nginx/proxy-hosts/{host_id}",
|
||||
expected_status=200,
|
||||
json=payload,
|
||||
)
|
||||
return self._json_object(
|
||||
response,
|
||||
"proxy-host update",
|
||||
)
|
||||
|
||||
def certificate(
|
||||
self,
|
||||
local_id: str | int,
|
||||
) -> dict[str, Any]:
|
||||
certificate_id = self._local_id(
|
||||
local_id,
|
||||
"certificate",
|
||||
)
|
||||
data = self.get_json(
|
||||
f"/nginx/certificates/{certificate_id}"
|
||||
)
|
||||
if not isinstance(data, dict):
|
||||
raise RuntimeError(
|
||||
"NPM certificate response was not an object"
|
||||
)
|
||||
return data
|
||||
|
||||
def create_certificate(
|
||||
self,
|
||||
payload: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
response = self.request(
|
||||
"POST",
|
||||
"/nginx/certificates",
|
||||
expected_status=201,
|
||||
json=payload,
|
||||
)
|
||||
return self._json_object(
|
||||
response,
|
||||
"certificate create",
|
||||
)
|
||||
|
||||
def upload_certificate(
|
||||
self,
|
||||
local_id: str | int,
|
||||
*,
|
||||
certificate: bytes,
|
||||
certificate_key: bytes,
|
||||
intermediate_certificate: bytes | None = None,
|
||||
) -> dict[str, Any]:
|
||||
certificate_id = self._local_id(
|
||||
local_id,
|
||||
"certificate",
|
||||
)
|
||||
|
||||
if not certificate:
|
||||
raise RuntimeError(
|
||||
"NPM certificate upload requires certificate data"
|
||||
)
|
||||
if not certificate_key:
|
||||
raise RuntimeError(
|
||||
"NPM certificate upload requires certificate key data"
|
||||
)
|
||||
|
||||
files: dict[str, tuple[str, bytes]] = {
|
||||
"certificate": (
|
||||
"certificate.pem",
|
||||
certificate,
|
||||
),
|
||||
"certificate_key": (
|
||||
"certificate_key.pem",
|
||||
certificate_key,
|
||||
),
|
||||
}
|
||||
|
||||
if intermediate_certificate:
|
||||
files["intermediate_certificate"] = (
|
||||
"intermediate_certificate.pem",
|
||||
intermediate_certificate,
|
||||
)
|
||||
|
||||
response = self.request(
|
||||
"POST",
|
||||
f"/nginx/certificates/{certificate_id}/upload",
|
||||
expected_status=200,
|
||||
files=files,
|
||||
)
|
||||
|
||||
return self._json_object(
|
||||
response,
|
||||
"certificate upload",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user