mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-27 14:08:53 +00:00
Add more type hints.
This commit is contained in:
parent
fe478bd1bb
commit
93cd3755a0
30 changed files with 354 additions and 298 deletions
|
|
@ -25,37 +25,37 @@ from ansible.module_utils.common.text.converters import to_text
|
|||
|
||||
|
||||
class HwcModuleException(Exception):
|
||||
def __init__(self, message):
|
||||
def __init__(self, message: str) -> None:
|
||||
super().__init__()
|
||||
|
||||
self._message = message
|
||||
|
||||
def __str__(self):
|
||||
def __str__(self) -> str:
|
||||
return f"[HwcClientException] message={self._message}"
|
||||
|
||||
|
||||
class HwcClientException(Exception):
|
||||
def __init__(self, code, message):
|
||||
def __init__(self, code: int, message: str) -> None:
|
||||
super().__init__()
|
||||
|
||||
self._code = code
|
||||
self._message = message
|
||||
|
||||
def __str__(self):
|
||||
def __str__(self) -> str:
|
||||
msg = f" code={self._code}," if self._code != 0 else ""
|
||||
return f"[HwcClientException]{msg} message={self._message}"
|
||||
|
||||
|
||||
class HwcClientException404(HwcClientException):
|
||||
def __init__(self, message):
|
||||
def __init__(self, message: str) -> None:
|
||||
super().__init__(404, message)
|
||||
|
||||
def __str__(self):
|
||||
def __str__(self) -> str:
|
||||
return f"[HwcClientException404] message={self._message}"
|
||||
|
||||
|
||||
def session_method_wrapper(f):
|
||||
def _wrap(self, url, *args, **kwargs):
|
||||
def _wrap(self, url: str, *args, **kwargs):
|
||||
try:
|
||||
url = self.endpoint + url
|
||||
r = f(self, url, *args, **kwargs)
|
||||
|
|
@ -92,7 +92,7 @@ def session_method_wrapper(f):
|
|||
|
||||
|
||||
class _ServiceClient:
|
||||
def __init__(self, client, endpoint, product):
|
||||
def __init__(self, client, endpoint: str, product):
|
||||
self._client = client
|
||||
self._endpoint = endpoint
|
||||
self._default_header = {
|
||||
|
|
@ -101,30 +101,30 @@ class _ServiceClient:
|
|||
}
|
||||
|
||||
@property
|
||||
def endpoint(self):
|
||||
def endpoint(self) -> str:
|
||||
return self._endpoint
|
||||
|
||||
@endpoint.setter
|
||||
def endpoint(self, e):
|
||||
def endpoint(self, e: str) -> None:
|
||||
self._endpoint = e
|
||||
|
||||
@session_method_wrapper
|
||||
def get(self, url, body=None, header=None, timeout=None):
|
||||
def get(self, url: str, body=None, header: dict[str, t.Any] | None = None, timeout=None):
|
||||
return self._client.get(url, json=body, timeout=timeout, headers=self._header(header))
|
||||
|
||||
@session_method_wrapper
|
||||
def post(self, url, body=None, header=None, timeout=None):
|
||||
def post(self, url: str, body=None, header: dict[str, t.Any] | None = None, timeout=None):
|
||||
return self._client.post(url, json=body, timeout=timeout, headers=self._header(header))
|
||||
|
||||
@session_method_wrapper
|
||||
def delete(self, url, body=None, header=None, timeout=None):
|
||||
def delete(self, url: str, body=None, header: dict[str, t.Any] | None = None, timeout=None):
|
||||
return self._client.delete(url, json=body, timeout=timeout, headers=self._header(header))
|
||||
|
||||
@session_method_wrapper
|
||||
def put(self, url, body=None, header=None, timeout=None):
|
||||
def put(self, url: str, body=None, header: dict[str, t.Any] | None = None, timeout=None):
|
||||
return self._client.put(url, json=body, timeout=timeout, headers=self._header(header))
|
||||
|
||||
def _header(self, header):
|
||||
def _header(self, header: dict[str, t.Any] | None) -> dict[str, t.Any]:
|
||||
if header and isinstance(header, dict):
|
||||
for k, v in self._default_header.items():
|
||||
if k not in header:
|
||||
|
|
@ -136,7 +136,7 @@ class _ServiceClient:
|
|||
|
||||
|
||||
class Config:
|
||||
def __init__(self, module: AnsibleModule, product):
|
||||
def __init__(self, module: AnsibleModule, product) -> None:
|
||||
self._project_client = None
|
||||
self._domain_client = None
|
||||
self._module = module
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue