tsurara-client 0.1.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.
- tsurara_client/__init__.py +111 -0
- tsurara_client/_auth.py +109 -0
- tsurara_client/_client.py +680 -0
- tsurara_client/_codec.py +301 -0
- tsurara_client/_def.py +68 -0
- tsurara_client/_file.py +13 -0
- tsurara_client/_info.py +40 -0
- tsurara_client/_missing.py +24 -0
- tsurara_client/_result.py +118 -0
- tsurara_client/_retry.py +59 -0
- tsurara_client/_sequences.py +116 -0
- tsurara_client/_serialize.py +311 -0
- tsurara_client/_unwrap.py +44 -0
- tsurara_client/py.typed +0 -0
- tsurara_client/support.py +12 -0
- tsurara_client/testing.py +212 -0
- tsurara_client-0.1.0.dist-info/METADATA +118 -0
- tsurara_client-0.1.0.dist-info/RECORD +20 -0
- tsurara_client-0.1.0.dist-info/WHEEL +4 -0
- tsurara_client-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""The runtime library for Python code generated by tsurara (design.md 18.9).
|
|
2
|
+
|
|
3
|
+
Two layers: the types layer (``TypeDefs``, ``File``, ``MISSING``, ``OperationDef``,
|
|
4
|
+
``SecurityScheme``) needs nothing but the standard library; the client layer (``Client``, the
|
|
5
|
+
Results, ``Retry``, ``unwrap``) needs httpx, installed with the ``http`` extra
|
|
6
|
+
(``tsurara-client[http]``). Its names are imported on first use, so code generated with
|
|
7
|
+
``operations: false`` imports this package without httpx.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import importlib
|
|
13
|
+
from typing import TYPE_CHECKING, Any
|
|
14
|
+
|
|
15
|
+
from ._auth import SecurityScheme
|
|
16
|
+
from ._codec import ConversionError, Converter, TypeDefs
|
|
17
|
+
from ._def import OPERATION_DEF_VERSION, OperationDef
|
|
18
|
+
from ._file import File
|
|
19
|
+
from ._missing import MISSING, Missing
|
|
20
|
+
|
|
21
|
+
if TYPE_CHECKING:
|
|
22
|
+
from ._client import AsyncClient, Auth, Client, HeaderValues
|
|
23
|
+
from ._info import OperationInfo, RequestInfo, UndocumentedStatusInfo
|
|
24
|
+
from ._result import (
|
|
25
|
+
AnyResult,
|
|
26
|
+
HttpError,
|
|
27
|
+
NetworkFailure,
|
|
28
|
+
ParseFailure,
|
|
29
|
+
Success,
|
|
30
|
+
TimeoutFailure,
|
|
31
|
+
TransportFailure,
|
|
32
|
+
UndocumentedStatus,
|
|
33
|
+
UnexpectedFailure,
|
|
34
|
+
)
|
|
35
|
+
from ._retry import Retry, retry_after
|
|
36
|
+
from ._unwrap import ApiError, unwrap
|
|
37
|
+
|
|
38
|
+
__version__ = "0.1.0"
|
|
39
|
+
|
|
40
|
+
_CLIENT: dict[str, str] = {
|
|
41
|
+
"AsyncClient": "._client",
|
|
42
|
+
"Auth": "._client",
|
|
43
|
+
"Client": "._client",
|
|
44
|
+
"HeaderValues": "._client",
|
|
45
|
+
"OperationInfo": "._info",
|
|
46
|
+
"RequestInfo": "._info",
|
|
47
|
+
"UndocumentedStatusInfo": "._info",
|
|
48
|
+
"AnyResult": "._result",
|
|
49
|
+
"HttpError": "._result",
|
|
50
|
+
"NetworkFailure": "._result",
|
|
51
|
+
"ParseFailure": "._result",
|
|
52
|
+
"Success": "._result",
|
|
53
|
+
"TimeoutFailure": "._result",
|
|
54
|
+
"TransportFailure": "._result",
|
|
55
|
+
"UndocumentedStatus": "._result",
|
|
56
|
+
"UnexpectedFailure": "._result",
|
|
57
|
+
"Retry": "._retry",
|
|
58
|
+
"retry_after": "._retry",
|
|
59
|
+
"ApiError": "._unwrap",
|
|
60
|
+
"unwrap": "._unwrap",
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def __getattr__(name: str) -> Any:
|
|
65
|
+
module = _CLIENT.get(name)
|
|
66
|
+
if module is None:
|
|
67
|
+
raise AttributeError(f"module 'tsurara_client' has no attribute {name!r}")
|
|
68
|
+
try:
|
|
69
|
+
value = getattr(importlib.import_module(module, __name__), name)
|
|
70
|
+
except ModuleNotFoundError as e:
|
|
71
|
+
if e.name != "httpx":
|
|
72
|
+
raise
|
|
73
|
+
raise ImportError(
|
|
74
|
+
f"tsurara_client.{name} needs httpx: install tsurara-client[http]",
|
|
75
|
+
name=name,
|
|
76
|
+
) from e
|
|
77
|
+
globals()[name] = value
|
|
78
|
+
return value
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
__all__ = [
|
|
82
|
+
"MISSING",
|
|
83
|
+
"OPERATION_DEF_VERSION",
|
|
84
|
+
"AnyResult",
|
|
85
|
+
"ApiError",
|
|
86
|
+
"AsyncClient",
|
|
87
|
+
"Auth",
|
|
88
|
+
"Client",
|
|
89
|
+
"ConversionError",
|
|
90
|
+
"Converter",
|
|
91
|
+
"File",
|
|
92
|
+
"HeaderValues",
|
|
93
|
+
"HttpError",
|
|
94
|
+
"Missing",
|
|
95
|
+
"NetworkFailure",
|
|
96
|
+
"OperationDef",
|
|
97
|
+
"OperationInfo",
|
|
98
|
+
"ParseFailure",
|
|
99
|
+
"Retry",
|
|
100
|
+
"SecurityScheme",
|
|
101
|
+
"RequestInfo",
|
|
102
|
+
"Success",
|
|
103
|
+
"TimeoutFailure",
|
|
104
|
+
"TransportFailure",
|
|
105
|
+
"TypeDefs",
|
|
106
|
+
"UndocumentedStatus",
|
|
107
|
+
"UndocumentedStatusInfo",
|
|
108
|
+
"UnexpectedFailure",
|
|
109
|
+
"retry_after",
|
|
110
|
+
"unwrap",
|
|
111
|
+
]
|
tsurara_client/_auth.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""Authentication (spec/README.md): the credentials of the first alternative of an operation's
|
|
2
|
+
`security` for which the auth hook gives every one, placed by scheme."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import base64
|
|
7
|
+
import inspect
|
|
8
|
+
from collections.abc import Awaitable, Callable, Mapping, Sequence
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from typing import Any
|
|
11
|
+
from urllib.parse import quote
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class SecurityScheme:
|
|
16
|
+
"""A security scheme an operation uses, as the auth hook receives it."""
|
|
17
|
+
|
|
18
|
+
scheme: str
|
|
19
|
+
"""The name in `components.securitySchemes`."""
|
|
20
|
+
type: str
|
|
21
|
+
"""`apiKey`, `http`, `oauth2`, `openIdConnect` or `mutualTLS`."""
|
|
22
|
+
in_: str | None = None
|
|
23
|
+
"""`apiKey`: `header`, `query` or `cookie`."""
|
|
24
|
+
name: str | None = None
|
|
25
|
+
"""`apiKey`: the header, query or cookie name."""
|
|
26
|
+
http_scheme: str | None = None
|
|
27
|
+
"""`http`: the Authorization scheme, such as `bearer` or `basic`."""
|
|
28
|
+
scopes: tuple[str, ...] = ()
|
|
29
|
+
|
|
30
|
+
@staticmethod
|
|
31
|
+
def of(use: Mapping[str, Any]) -> SecurityScheme:
|
|
32
|
+
return SecurityScheme(
|
|
33
|
+
scheme=use["scheme"],
|
|
34
|
+
type=use["type"],
|
|
35
|
+
in_=use.get("in"),
|
|
36
|
+
name=use.get("name"),
|
|
37
|
+
http_scheme=use.get("httpScheme"),
|
|
38
|
+
scopes=tuple(use.get("scopes", ())),
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass
|
|
43
|
+
class Credentials:
|
|
44
|
+
headers: dict[str, str] = field(default_factory=lambda: {})
|
|
45
|
+
query: list[str] = field(default_factory=lambda: [])
|
|
46
|
+
cookies: list[str] = field(default_factory=lambda: [])
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
Get = Callable[[SecurityScheme], "str | None | Awaitable[str | None]"]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _place(out: Credentials, s: SecurityScheme, value: str) -> None:
|
|
53
|
+
if s.type == "apiKey":
|
|
54
|
+
name = s.name or s.scheme
|
|
55
|
+
if s.in_ == "query":
|
|
56
|
+
out.query.append(f"{quote(name, safe='')}={quote(value, safe='')}")
|
|
57
|
+
elif s.in_ == "cookie":
|
|
58
|
+
out.cookies.append(f"{name}={quote(value, safe='')}")
|
|
59
|
+
else:
|
|
60
|
+
out.headers[name.lower()] = value
|
|
61
|
+
return
|
|
62
|
+
http = (s.http_scheme or "bearer") if s.type == "http" else "bearer"
|
|
63
|
+
if http.lower() == "basic":
|
|
64
|
+
out.headers["authorization"] = "Basic " + base64.b64encode(value.encode()).decode()
|
|
65
|
+
elif http.lower() == "bearer":
|
|
66
|
+
out.headers["authorization"] = f"Bearer {value}"
|
|
67
|
+
else:
|
|
68
|
+
out.headers["authorization"] = f"{http} {value}"
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _alternatives(security: Sequence[Sequence[Mapping[str, Any]]] | None) -> list[list[SecurityScheme]]:
|
|
72
|
+
# mutualTLS is handled by the transport.
|
|
73
|
+
return [[SecurityScheme.of(u) for u in alt if u["type"] != "mutualTLS"] for alt in security or []]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def authenticate(security: Sequence[Sequence[Mapping[str, Any]]] | None, get: Get) -> Credentials:
|
|
77
|
+
out = Credentials()
|
|
78
|
+
for alternative in _alternatives(security):
|
|
79
|
+
values: list[tuple[SecurityScheme, str]] = []
|
|
80
|
+
for s in alternative:
|
|
81
|
+
value = get(s)
|
|
82
|
+
if inspect.isawaitable(value):
|
|
83
|
+
raise TypeError("an async auth function needs AsyncClient")
|
|
84
|
+
if value is None:
|
|
85
|
+
break
|
|
86
|
+
values.append((s, value))
|
|
87
|
+
if len(values) == len(alternative):
|
|
88
|
+
for s, v in values:
|
|
89
|
+
_place(out, s, v)
|
|
90
|
+
return out
|
|
91
|
+
return out
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
async def authenticate_async(security: Sequence[Sequence[Mapping[str, Any]]] | None, get: Get) -> Credentials:
|
|
95
|
+
out = Credentials()
|
|
96
|
+
for alternative in _alternatives(security):
|
|
97
|
+
values: list[tuple[SecurityScheme, str]] = []
|
|
98
|
+
for s in alternative:
|
|
99
|
+
value = get(s)
|
|
100
|
+
if inspect.isawaitable(value):
|
|
101
|
+
value = await value
|
|
102
|
+
if value is None:
|
|
103
|
+
break
|
|
104
|
+
values.append((s, value))
|
|
105
|
+
if len(values) == len(alternative):
|
|
106
|
+
for s, v in values:
|
|
107
|
+
_place(out, s, v)
|
|
108
|
+
return out
|
|
109
|
+
return out
|