Merge pull request #686 from Misha20062006/patch-1

Rename TememtAPI to TelemtAPI (fix typo)
This commit is contained in:
Alexey 2026-04-11 20:09:24 +03:00 committed by GitHub
commit e48e1b141d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 56 additions and 55 deletions

View File

@ -24,7 +24,7 @@ from urllib.request import Request, urlopen
# Exceptions # Exceptions
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
class TememtAPIError(Exception): class TelemtAPIError(Exception):
"""Raised when the API returns an error envelope or a transport error.""" """Raised when the API returns an error envelope or a transport error."""
def __init__(self, message: str, code: str | None = None, def __init__(self, message: str, code: str | None = None,
@ -35,7 +35,7 @@ class TememtAPIError(Exception):
self.request_id = request_id self.request_id = request_id
def __repr__(self) -> str: def __repr__(self) -> str:
return (f"TememtAPIError(message={str(self)!r}, code={self.code!r}, " return (f"TelemtAPIError(message={str(self)!r}, code={self.code!r}, "
f"http_status={self.http_status}, request_id={self.request_id})") f"http_status={self.http_status}, request_id={self.request_id})")
@ -58,7 +58,7 @@ class APIResponse:
# Main client # Main client
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
class TememtAPI: class TelemtAPI:
""" """
HTTP client for the Telemt Control API. HTTP client for the Telemt Control API.
@ -133,22 +133,22 @@ class TememtAPI:
try: try:
payload = json.loads(raw) payload = json.loads(raw)
except Exception: except Exception:
raise TememtAPIError( raise TelemtAPIError(
str(exc), http_status=exc.code str(exc), http_status=exc.code
) from exc ) from exc
err = payload.get("error", {}) err = payload.get("error", {})
raise TememtAPIError( raise TelemtAPIError(
err.get("message", str(exc)), err.get("message", str(exc)),
code=err.get("code"), code=err.get("code"),
http_status=exc.code, http_status=exc.code,
request_id=payload.get("request_id"), request_id=payload.get("request_id"),
) from exc ) from exc
except URLError as exc: except URLError as exc:
raise TememtAPIError(str(exc)) from exc raise TelemtAPIError(str(exc)) from exc
if not payload.get("ok"): if not payload.get("ok"):
err = payload.get("error", {}) err = payload.get("error", {})
raise TememtAPIError( raise TelemtAPIError(
err.get("message", "unknown error"), err.get("message", "unknown error"),
code=err.get("code"), code=err.get("code"),
request_id=payload.get("request_id"), request_id=payload.get("request_id"),
@ -564,10 +564,10 @@ if __name__ == "__main__":
sys.exit(0) sys.exit(0)
if cmd == "gen-secret": if cmd == "gen-secret":
print(TememtAPI.generate_secret()) print(TelemtAPI.generate_secret())
sys.exit(0) sys.exit(0)
api = TememtAPI(args.url, auth_header=args.auth, timeout=args.timeout) api = TelemtAPI(args.url, auth_header=args.auth, timeout=args.timeout)
try: try:
# -- read endpoints -------------------------------------------------- # -- read endpoints --------------------------------------------------
@ -690,7 +690,8 @@ if __name__ == "__main__":
parser.error("patch command requires <username>") parser.error("patch command requires <username>")
if not any([args.secret, args.ad_tag, args.max_conns, if not any([args.secret, args.ad_tag, args.max_conns,
args.expires, args.quota, args.max_ips]): args.expires, args.quota, args.max_ips]):
parser.error("patch requires at least one field (--secret, --max-conns, --expires, --quota, --max-ips, --ad-tag)") parser.error(
"patch requires at least one field (--secret, --max-conns, --expires, --quota, --max-ips, --ad-tag)")
_print(api.patch_user( _print(api.patch_user(
args.arg, args.arg,
secret=args.secret, secret=args.secret,
@ -721,7 +722,7 @@ if __name__ == "__main__":
file=sys.stderr) file=sys.stderr)
sys.exit(1) sys.exit(1)
except TememtAPIError as exc: except TelemtAPIError as exc:
print(f"API error [{exc.http_status}] {exc.code}: {exc}", file=sys.stderr) print(f"API error [{exc.http_status}] {exc.code}: {exc}", file=sys.stderr)
sys.exit(1) sys.exit(1)
except KeyboardInterrupt: except KeyboardInterrupt: