lambdadb 0.1.2__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.
Potentially problematic release.
This version of lambdadb might be problematic. Click here for more details.
- lambdadb/__init__.py +17 -0
- lambdadb/_hooks/__init__.py +5 -0
- lambdadb/_hooks/registration.py +13 -0
- lambdadb/_hooks/sdkhooks.py +76 -0
- lambdadb/_hooks/types.py +106 -0
- lambdadb/_version.py +15 -0
- lambdadb/basesdk.py +358 -0
- lambdadb/collections.py +1630 -0
- lambdadb/docs.py +1328 -0
- lambdadb/errors/__init__.py +74 -0
- lambdadb/errors/apierror.py +22 -0
- lambdadb/errors/badrequest_error.py +20 -0
- lambdadb/errors/internalservererror.py +20 -0
- lambdadb/errors/resourcealreadyexists_error.py +20 -0
- lambdadb/errors/resourcenotfound_error.py +20 -0
- lambdadb/errors/toomanyrequests_error.py +20 -0
- lambdadb/errors/unauthenticated_error.py +20 -0
- lambdadb/httpclient.py +126 -0
- lambdadb/models/__init__.py +420 -0
- lambdadb/models/bulkupsertdocsop.py +59 -0
- lambdadb/models/collectionresponse.py +64 -0
- lambdadb/models/createcollectionop.py +64 -0
- lambdadb/models/createprojectop.py +39 -0
- lambdadb/models/deletecollectionop.py +43 -0
- lambdadb/models/deletedocsop.py +88 -0
- lambdadb/models/deleteprojectop.py +52 -0
- lambdadb/models/fetchdocsop.py +102 -0
- lambdadb/models/getbulkupsertdocsop.py +82 -0
- lambdadb/models/getcollectionop.py +30 -0
- lambdadb/models/getprojectop.py +39 -0
- lambdadb/models/indexconfigs_union.py +95 -0
- lambdadb/models/listcollectionsop.py +35 -0
- lambdadb/models/listprojectsop.py +38 -0
- lambdadb/models/projectresponse.py +38 -0
- lambdadb/models/querycollectionop.py +152 -0
- lambdadb/models/security.py +25 -0
- lambdadb/models/status.py +12 -0
- lambdadb/models/updatecollectionop.py +48 -0
- lambdadb/models/updateprojectop.py +58 -0
- lambdadb/models/upsertdocsop.py +67 -0
- lambdadb/projects.py +1228 -0
- lambdadb/py.typed +1 -0
- lambdadb/sdk.py +170 -0
- lambdadb/sdkconfiguration.py +56 -0
- lambdadb/types/__init__.py +21 -0
- lambdadb/types/basemodel.py +39 -0
- lambdadb/utils/__init__.py +187 -0
- lambdadb/utils/annotations.py +55 -0
- lambdadb/utils/datetimes.py +23 -0
- lambdadb/utils/enums.py +74 -0
- lambdadb/utils/eventstreaming.py +238 -0
- lambdadb/utils/forms.py +202 -0
- lambdadb/utils/headers.py +136 -0
- lambdadb/utils/logger.py +27 -0
- lambdadb/utils/metadata.py +118 -0
- lambdadb/utils/queryparams.py +205 -0
- lambdadb/utils/requestbodies.py +66 -0
- lambdadb/utils/retries.py +217 -0
- lambdadb/utils/security.py +192 -0
- lambdadb/utils/serializers.py +248 -0
- lambdadb/utils/url.py +155 -0
- lambdadb/utils/values.py +137 -0
- lambdadb-0.1.2.dist-info/LICENSE +201 -0
- lambdadb-0.1.2.dist-info/METADATA +514 -0
- lambdadb-0.1.2.dist-info/RECORD +66 -0
- lambdadb-0.1.2.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
|
|
5
|
+
from typing import (
|
|
6
|
+
Any,
|
|
7
|
+
Dict,
|
|
8
|
+
List,
|
|
9
|
+
Optional,
|
|
10
|
+
Tuple,
|
|
11
|
+
)
|
|
12
|
+
from pydantic import BaseModel
|
|
13
|
+
from pydantic.fields import FieldInfo
|
|
14
|
+
|
|
15
|
+
from .metadata import (
|
|
16
|
+
SecurityMetadata,
|
|
17
|
+
find_field_metadata,
|
|
18
|
+
)
|
|
19
|
+
import os
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def get_security(security: Any) -> Tuple[Dict[str, str], Dict[str, List[str]]]:
|
|
23
|
+
headers: Dict[str, str] = {}
|
|
24
|
+
query_params: Dict[str, List[str]] = {}
|
|
25
|
+
|
|
26
|
+
if security is None:
|
|
27
|
+
return headers, query_params
|
|
28
|
+
|
|
29
|
+
if not isinstance(security, BaseModel):
|
|
30
|
+
raise TypeError("security must be a pydantic model")
|
|
31
|
+
|
|
32
|
+
sec_fields: Dict[str, FieldInfo] = security.__class__.model_fields
|
|
33
|
+
for name in sec_fields:
|
|
34
|
+
sec_field = sec_fields[name]
|
|
35
|
+
|
|
36
|
+
value = getattr(security, name)
|
|
37
|
+
if value is None:
|
|
38
|
+
continue
|
|
39
|
+
|
|
40
|
+
metadata = find_field_metadata(sec_field, SecurityMetadata)
|
|
41
|
+
if metadata is None:
|
|
42
|
+
continue
|
|
43
|
+
if metadata.option:
|
|
44
|
+
_parse_security_option(headers, query_params, value)
|
|
45
|
+
return headers, query_params
|
|
46
|
+
if metadata.scheme:
|
|
47
|
+
# Special case for basic auth or custom auth which could be a flattened model
|
|
48
|
+
if metadata.sub_type in ["basic", "custom"] and not isinstance(
|
|
49
|
+
value, BaseModel
|
|
50
|
+
):
|
|
51
|
+
_parse_security_scheme(headers, query_params, metadata, name, security)
|
|
52
|
+
else:
|
|
53
|
+
_parse_security_scheme(headers, query_params, metadata, name, value)
|
|
54
|
+
|
|
55
|
+
return headers, query_params
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def get_security_from_env(security: Any, security_class: Any) -> Optional[BaseModel]:
|
|
59
|
+
if security is not None:
|
|
60
|
+
return security
|
|
61
|
+
|
|
62
|
+
if not issubclass(security_class, BaseModel):
|
|
63
|
+
raise TypeError("security_class must be a pydantic model class")
|
|
64
|
+
|
|
65
|
+
security_dict: Any = {}
|
|
66
|
+
|
|
67
|
+
if os.getenv("LAMBDADB_PROJECT_API_KEY"):
|
|
68
|
+
security_dict["project_api_key"] = os.getenv("LAMBDADB_PROJECT_API_KEY")
|
|
69
|
+
|
|
70
|
+
return security_class(**security_dict) if security_dict else None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _parse_security_option(
|
|
74
|
+
headers: Dict[str, str], query_params: Dict[str, List[str]], option: Any
|
|
75
|
+
):
|
|
76
|
+
if not isinstance(option, BaseModel):
|
|
77
|
+
raise TypeError("security option must be a pydantic model")
|
|
78
|
+
|
|
79
|
+
opt_fields: Dict[str, FieldInfo] = option.__class__.model_fields
|
|
80
|
+
for name in opt_fields:
|
|
81
|
+
opt_field = opt_fields[name]
|
|
82
|
+
|
|
83
|
+
metadata = find_field_metadata(opt_field, SecurityMetadata)
|
|
84
|
+
if metadata is None or not metadata.scheme:
|
|
85
|
+
continue
|
|
86
|
+
_parse_security_scheme(
|
|
87
|
+
headers, query_params, metadata, name, getattr(option, name)
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _parse_security_scheme(
|
|
92
|
+
headers: Dict[str, str],
|
|
93
|
+
query_params: Dict[str, List[str]],
|
|
94
|
+
scheme_metadata: SecurityMetadata,
|
|
95
|
+
field_name: str,
|
|
96
|
+
scheme: Any,
|
|
97
|
+
):
|
|
98
|
+
scheme_type = scheme_metadata.scheme_type
|
|
99
|
+
sub_type = scheme_metadata.sub_type
|
|
100
|
+
|
|
101
|
+
if isinstance(scheme, BaseModel):
|
|
102
|
+
if scheme_type == "http":
|
|
103
|
+
if sub_type == "basic":
|
|
104
|
+
_parse_basic_auth_scheme(headers, scheme)
|
|
105
|
+
return
|
|
106
|
+
if sub_type == "custom":
|
|
107
|
+
return
|
|
108
|
+
|
|
109
|
+
scheme_fields: Dict[str, FieldInfo] = scheme.__class__.model_fields
|
|
110
|
+
for name in scheme_fields:
|
|
111
|
+
scheme_field = scheme_fields[name]
|
|
112
|
+
|
|
113
|
+
metadata = find_field_metadata(scheme_field, SecurityMetadata)
|
|
114
|
+
if metadata is None or metadata.field_name is None:
|
|
115
|
+
continue
|
|
116
|
+
|
|
117
|
+
value = getattr(scheme, name)
|
|
118
|
+
|
|
119
|
+
_parse_security_scheme_value(
|
|
120
|
+
headers, query_params, scheme_metadata, metadata, name, value
|
|
121
|
+
)
|
|
122
|
+
else:
|
|
123
|
+
_parse_security_scheme_value(
|
|
124
|
+
headers, query_params, scheme_metadata, scheme_metadata, field_name, scheme
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _parse_security_scheme_value(
|
|
129
|
+
headers: Dict[str, str],
|
|
130
|
+
query_params: Dict[str, List[str]],
|
|
131
|
+
scheme_metadata: SecurityMetadata,
|
|
132
|
+
security_metadata: SecurityMetadata,
|
|
133
|
+
field_name: str,
|
|
134
|
+
value: Any,
|
|
135
|
+
):
|
|
136
|
+
scheme_type = scheme_metadata.scheme_type
|
|
137
|
+
sub_type = scheme_metadata.sub_type
|
|
138
|
+
|
|
139
|
+
header_name = security_metadata.get_field_name(field_name)
|
|
140
|
+
|
|
141
|
+
if scheme_type == "apiKey":
|
|
142
|
+
if sub_type == "header":
|
|
143
|
+
headers[header_name] = value
|
|
144
|
+
elif sub_type == "query":
|
|
145
|
+
query_params[header_name] = [value]
|
|
146
|
+
else:
|
|
147
|
+
raise ValueError("sub type {sub_type} not supported")
|
|
148
|
+
elif scheme_type == "openIdConnect":
|
|
149
|
+
headers[header_name] = _apply_bearer(value)
|
|
150
|
+
elif scheme_type == "oauth2":
|
|
151
|
+
if sub_type != "client_credentials":
|
|
152
|
+
headers[header_name] = _apply_bearer(value)
|
|
153
|
+
elif scheme_type == "http":
|
|
154
|
+
if sub_type == "bearer":
|
|
155
|
+
headers[header_name] = _apply_bearer(value)
|
|
156
|
+
elif sub_type == "custom":
|
|
157
|
+
return
|
|
158
|
+
else:
|
|
159
|
+
raise ValueError("sub type {sub_type} not supported")
|
|
160
|
+
else:
|
|
161
|
+
raise ValueError("scheme type {scheme_type} not supported")
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _apply_bearer(token: str) -> str:
|
|
165
|
+
return token.lower().startswith("bearer ") and token or f"Bearer {token}"
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _parse_basic_auth_scheme(headers: Dict[str, str], scheme: Any):
|
|
169
|
+
username = ""
|
|
170
|
+
password = ""
|
|
171
|
+
|
|
172
|
+
if not isinstance(scheme, BaseModel):
|
|
173
|
+
raise TypeError("basic auth scheme must be a pydantic model")
|
|
174
|
+
|
|
175
|
+
scheme_fields: Dict[str, FieldInfo] = scheme.__class__.model_fields
|
|
176
|
+
for name in scheme_fields:
|
|
177
|
+
scheme_field = scheme_fields[name]
|
|
178
|
+
|
|
179
|
+
metadata = find_field_metadata(scheme_field, SecurityMetadata)
|
|
180
|
+
if metadata is None or metadata.field_name is None:
|
|
181
|
+
continue
|
|
182
|
+
|
|
183
|
+
field_name = metadata.field_name
|
|
184
|
+
value = getattr(scheme, name)
|
|
185
|
+
|
|
186
|
+
if field_name == "username":
|
|
187
|
+
username = value
|
|
188
|
+
if field_name == "password":
|
|
189
|
+
password = value
|
|
190
|
+
|
|
191
|
+
data = f"{username}:{password}".encode()
|
|
192
|
+
headers["Authorization"] = f"Basic {base64.b64encode(data).decode()}"
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
|
|
2
|
+
|
|
3
|
+
from decimal import Decimal
|
|
4
|
+
import functools
|
|
5
|
+
import json
|
|
6
|
+
import typing
|
|
7
|
+
from typing import Any, Dict, List, Tuple, Union, get_args
|
|
8
|
+
import typing_extensions
|
|
9
|
+
from typing_extensions import get_origin
|
|
10
|
+
|
|
11
|
+
import httpx
|
|
12
|
+
from pydantic import ConfigDict, create_model
|
|
13
|
+
from pydantic_core import from_json
|
|
14
|
+
|
|
15
|
+
from ..types.basemodel import BaseModel, Nullable, OptionalNullable, Unset
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def serialize_decimal(as_str: bool):
|
|
19
|
+
def serialize(d):
|
|
20
|
+
# Optional[T] is a Union[T, None]
|
|
21
|
+
if is_union(type(d)) and type(None) in get_args(type(d)) and d is None:
|
|
22
|
+
return None
|
|
23
|
+
if isinstance(d, Unset):
|
|
24
|
+
return d
|
|
25
|
+
|
|
26
|
+
if not isinstance(d, Decimal):
|
|
27
|
+
raise ValueError("Expected Decimal object")
|
|
28
|
+
|
|
29
|
+
return str(d) if as_str else float(d)
|
|
30
|
+
|
|
31
|
+
return serialize
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def validate_decimal(d):
|
|
35
|
+
if d is None:
|
|
36
|
+
return None
|
|
37
|
+
|
|
38
|
+
if isinstance(d, (Decimal, Unset)):
|
|
39
|
+
return d
|
|
40
|
+
|
|
41
|
+
if not isinstance(d, (str, int, float)):
|
|
42
|
+
raise ValueError("Expected string, int or float")
|
|
43
|
+
|
|
44
|
+
return Decimal(str(d))
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def serialize_float(as_str: bool):
|
|
48
|
+
def serialize(f):
|
|
49
|
+
# Optional[T] is a Union[T, None]
|
|
50
|
+
if is_union(type(f)) and type(None) in get_args(type(f)) and f is None:
|
|
51
|
+
return None
|
|
52
|
+
if isinstance(f, Unset):
|
|
53
|
+
return f
|
|
54
|
+
|
|
55
|
+
if not isinstance(f, float):
|
|
56
|
+
raise ValueError("Expected float")
|
|
57
|
+
|
|
58
|
+
return str(f) if as_str else f
|
|
59
|
+
|
|
60
|
+
return serialize
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def validate_float(f):
|
|
64
|
+
if f is None:
|
|
65
|
+
return None
|
|
66
|
+
|
|
67
|
+
if isinstance(f, (float, Unset)):
|
|
68
|
+
return f
|
|
69
|
+
|
|
70
|
+
if not isinstance(f, str):
|
|
71
|
+
raise ValueError("Expected string")
|
|
72
|
+
|
|
73
|
+
return float(f)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def serialize_int(as_str: bool):
|
|
77
|
+
def serialize(i):
|
|
78
|
+
# Optional[T] is a Union[T, None]
|
|
79
|
+
if is_union(type(i)) and type(None) in get_args(type(i)) and i is None:
|
|
80
|
+
return None
|
|
81
|
+
if isinstance(i, Unset):
|
|
82
|
+
return i
|
|
83
|
+
|
|
84
|
+
if not isinstance(i, int):
|
|
85
|
+
raise ValueError("Expected int")
|
|
86
|
+
|
|
87
|
+
return str(i) if as_str else i
|
|
88
|
+
|
|
89
|
+
return serialize
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def validate_int(b):
|
|
93
|
+
if b is None:
|
|
94
|
+
return None
|
|
95
|
+
|
|
96
|
+
if isinstance(b, (int, Unset)):
|
|
97
|
+
return b
|
|
98
|
+
|
|
99
|
+
if not isinstance(b, str):
|
|
100
|
+
raise ValueError("Expected string")
|
|
101
|
+
|
|
102
|
+
return int(b)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def validate_open_enum(is_int: bool):
|
|
106
|
+
def validate(e):
|
|
107
|
+
if e is None:
|
|
108
|
+
return None
|
|
109
|
+
|
|
110
|
+
if isinstance(e, Unset):
|
|
111
|
+
return e
|
|
112
|
+
|
|
113
|
+
if is_int:
|
|
114
|
+
if not isinstance(e, int):
|
|
115
|
+
raise ValueError("Expected int")
|
|
116
|
+
else:
|
|
117
|
+
if not isinstance(e, str):
|
|
118
|
+
raise ValueError("Expected string")
|
|
119
|
+
|
|
120
|
+
return e
|
|
121
|
+
|
|
122
|
+
return validate
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def validate_const(v):
|
|
126
|
+
def validate(c):
|
|
127
|
+
# Optional[T] is a Union[T, None]
|
|
128
|
+
if is_union(type(c)) and type(None) in get_args(type(c)) and c is None:
|
|
129
|
+
return None
|
|
130
|
+
|
|
131
|
+
if v != c:
|
|
132
|
+
raise ValueError(f"Expected {v}")
|
|
133
|
+
|
|
134
|
+
return c
|
|
135
|
+
|
|
136
|
+
return validate
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def unmarshal_json(raw, typ: Any) -> Any:
|
|
140
|
+
return unmarshal(from_json(raw), typ)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def unmarshal(val, typ: Any) -> Any:
|
|
144
|
+
unmarshaller = create_model(
|
|
145
|
+
"Unmarshaller",
|
|
146
|
+
body=(typ, ...),
|
|
147
|
+
__config__=ConfigDict(populate_by_name=True, arbitrary_types_allowed=True),
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
m = unmarshaller(body=val)
|
|
151
|
+
|
|
152
|
+
# pyright: ignore[reportAttributeAccessIssue]
|
|
153
|
+
return m.body # type: ignore
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def marshal_json(val, typ):
|
|
157
|
+
if is_nullable(typ) and val is None:
|
|
158
|
+
return "null"
|
|
159
|
+
|
|
160
|
+
marshaller = create_model(
|
|
161
|
+
"Marshaller",
|
|
162
|
+
body=(typ, ...),
|
|
163
|
+
__config__=ConfigDict(populate_by_name=True, arbitrary_types_allowed=True),
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
m = marshaller(body=val)
|
|
167
|
+
|
|
168
|
+
d = m.model_dump(by_alias=True, mode="json", exclude_none=True)
|
|
169
|
+
|
|
170
|
+
if len(d) == 0:
|
|
171
|
+
return ""
|
|
172
|
+
|
|
173
|
+
return json.dumps(d[next(iter(d))], separators=(",", ":"))
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def is_nullable(field):
|
|
177
|
+
origin = get_origin(field)
|
|
178
|
+
if origin is Nullable or origin is OptionalNullable:
|
|
179
|
+
return True
|
|
180
|
+
|
|
181
|
+
if not origin is Union or type(None) not in get_args(field):
|
|
182
|
+
return False
|
|
183
|
+
|
|
184
|
+
for arg in get_args(field):
|
|
185
|
+
if get_origin(arg) is Nullable or get_origin(arg) is OptionalNullable:
|
|
186
|
+
return True
|
|
187
|
+
|
|
188
|
+
return False
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def is_union(obj: object) -> bool:
|
|
192
|
+
"""
|
|
193
|
+
Returns True if the given object is a typing.Union or typing_extensions.Union.
|
|
194
|
+
"""
|
|
195
|
+
return any(obj is typing_obj for typing_obj in _get_typing_objects_by_name_of("Union"))
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def stream_to_text(stream: httpx.Response) -> str:
|
|
199
|
+
return "".join(stream.iter_text())
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
async def stream_to_text_async(stream: httpx.Response) -> str:
|
|
203
|
+
return "".join([chunk async for chunk in stream.aiter_text()])
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def stream_to_bytes(stream: httpx.Response) -> bytes:
|
|
207
|
+
return stream.content
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
async def stream_to_bytes_async(stream: httpx.Response) -> bytes:
|
|
211
|
+
return await stream.aread()
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def get_pydantic_model(data: Any, typ: Any) -> Any:
|
|
215
|
+
if not _contains_pydantic_model(data):
|
|
216
|
+
return unmarshal(data, typ)
|
|
217
|
+
|
|
218
|
+
return data
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _contains_pydantic_model(data: Any) -> bool:
|
|
222
|
+
if isinstance(data, BaseModel):
|
|
223
|
+
return True
|
|
224
|
+
if isinstance(data, List):
|
|
225
|
+
return any(_contains_pydantic_model(item) for item in data)
|
|
226
|
+
if isinstance(data, Dict):
|
|
227
|
+
return any(_contains_pydantic_model(value) for value in data.values())
|
|
228
|
+
|
|
229
|
+
return False
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
@functools.cache
|
|
233
|
+
def _get_typing_objects_by_name_of(name: str) -> Tuple[Any, ...]:
|
|
234
|
+
"""
|
|
235
|
+
Get typing objects by name from typing and typing_extensions.
|
|
236
|
+
Reference: https://typing-extensions.readthedocs.io/en/latest/#runtime-use-of-types
|
|
237
|
+
"""
|
|
238
|
+
result = tuple(
|
|
239
|
+
getattr(module, name)
|
|
240
|
+
for module in (typing, typing_extensions)
|
|
241
|
+
if hasattr(module, name)
|
|
242
|
+
)
|
|
243
|
+
if not result:
|
|
244
|
+
raise ValueError(
|
|
245
|
+
f"Neither typing nor typing_extensions has an object called {name!r}"
|
|
246
|
+
)
|
|
247
|
+
return result
|
|
248
|
+
|
lambdadb/utils/url.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
|
|
2
|
+
|
|
3
|
+
from decimal import Decimal
|
|
4
|
+
from typing import (
|
|
5
|
+
Any,
|
|
6
|
+
Dict,
|
|
7
|
+
get_type_hints,
|
|
8
|
+
List,
|
|
9
|
+
Optional,
|
|
10
|
+
Union,
|
|
11
|
+
get_args,
|
|
12
|
+
get_origin,
|
|
13
|
+
)
|
|
14
|
+
from pydantic import BaseModel
|
|
15
|
+
from pydantic.fields import FieldInfo
|
|
16
|
+
|
|
17
|
+
from .metadata import (
|
|
18
|
+
PathParamMetadata,
|
|
19
|
+
find_field_metadata,
|
|
20
|
+
)
|
|
21
|
+
from .values import (
|
|
22
|
+
_get_serialized_params,
|
|
23
|
+
_is_set,
|
|
24
|
+
_populate_from_globals,
|
|
25
|
+
_val_to_string,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def generate_url(
|
|
30
|
+
server_url: str,
|
|
31
|
+
path: str,
|
|
32
|
+
path_params: Any,
|
|
33
|
+
gbls: Optional[Any] = None,
|
|
34
|
+
) -> str:
|
|
35
|
+
path_param_values: Dict[str, str] = {}
|
|
36
|
+
|
|
37
|
+
globals_already_populated = _populate_path_params(
|
|
38
|
+
path_params, gbls, path_param_values, []
|
|
39
|
+
)
|
|
40
|
+
if _is_set(gbls):
|
|
41
|
+
_populate_path_params(gbls, None, path_param_values, globals_already_populated)
|
|
42
|
+
|
|
43
|
+
for key, value in path_param_values.items():
|
|
44
|
+
path = path.replace("{" + key + "}", value, 1)
|
|
45
|
+
|
|
46
|
+
return remove_suffix(server_url, "/") + path
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _populate_path_params(
|
|
50
|
+
path_params: Any,
|
|
51
|
+
gbls: Any,
|
|
52
|
+
path_param_values: Dict[str, str],
|
|
53
|
+
skip_fields: List[str],
|
|
54
|
+
) -> List[str]:
|
|
55
|
+
globals_already_populated: List[str] = []
|
|
56
|
+
|
|
57
|
+
if not isinstance(path_params, BaseModel):
|
|
58
|
+
return globals_already_populated
|
|
59
|
+
|
|
60
|
+
path_param_fields: Dict[str, FieldInfo] = path_params.__class__.model_fields
|
|
61
|
+
path_param_field_types = get_type_hints(path_params.__class__)
|
|
62
|
+
for name in path_param_fields:
|
|
63
|
+
if name in skip_fields:
|
|
64
|
+
continue
|
|
65
|
+
|
|
66
|
+
field = path_param_fields[name]
|
|
67
|
+
|
|
68
|
+
param_metadata = find_field_metadata(field, PathParamMetadata)
|
|
69
|
+
if param_metadata is None:
|
|
70
|
+
continue
|
|
71
|
+
|
|
72
|
+
param = getattr(path_params, name) if _is_set(path_params) else None
|
|
73
|
+
param, global_found = _populate_from_globals(
|
|
74
|
+
name, param, PathParamMetadata, gbls
|
|
75
|
+
)
|
|
76
|
+
if global_found:
|
|
77
|
+
globals_already_populated.append(name)
|
|
78
|
+
|
|
79
|
+
if not _is_set(param):
|
|
80
|
+
continue
|
|
81
|
+
|
|
82
|
+
f_name = field.alias if field.alias is not None else name
|
|
83
|
+
serialization = param_metadata.serialization
|
|
84
|
+
if serialization is not None:
|
|
85
|
+
serialized_params = _get_serialized_params(
|
|
86
|
+
param_metadata, f_name, param, path_param_field_types[name]
|
|
87
|
+
)
|
|
88
|
+
for key, value in serialized_params.items():
|
|
89
|
+
path_param_values[key] = value
|
|
90
|
+
else:
|
|
91
|
+
pp_vals: List[str] = []
|
|
92
|
+
if param_metadata.style == "simple":
|
|
93
|
+
if isinstance(param, List):
|
|
94
|
+
for pp_val in param:
|
|
95
|
+
if not _is_set(pp_val):
|
|
96
|
+
continue
|
|
97
|
+
pp_vals.append(_val_to_string(pp_val))
|
|
98
|
+
path_param_values[f_name] = ",".join(pp_vals)
|
|
99
|
+
elif isinstance(param, Dict):
|
|
100
|
+
for pp_key in param:
|
|
101
|
+
if not _is_set(param[pp_key]):
|
|
102
|
+
continue
|
|
103
|
+
if param_metadata.explode:
|
|
104
|
+
pp_vals.append(f"{pp_key}={_val_to_string(param[pp_key])}")
|
|
105
|
+
else:
|
|
106
|
+
pp_vals.append(f"{pp_key},{_val_to_string(param[pp_key])}")
|
|
107
|
+
path_param_values[f_name] = ",".join(pp_vals)
|
|
108
|
+
elif not isinstance(param, (str, int, float, complex, bool, Decimal)):
|
|
109
|
+
param_fields: Dict[str, FieldInfo] = param.__class__.model_fields
|
|
110
|
+
for name in param_fields:
|
|
111
|
+
param_field = param_fields[name]
|
|
112
|
+
|
|
113
|
+
param_value_metadata = find_field_metadata(
|
|
114
|
+
param_field, PathParamMetadata
|
|
115
|
+
)
|
|
116
|
+
if param_value_metadata is None:
|
|
117
|
+
continue
|
|
118
|
+
|
|
119
|
+
param_name = (
|
|
120
|
+
param_field.alias if param_field.alias is not None else name
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
param_field_val = getattr(param, name)
|
|
124
|
+
if not _is_set(param_field_val):
|
|
125
|
+
continue
|
|
126
|
+
if param_metadata.explode:
|
|
127
|
+
pp_vals.append(
|
|
128
|
+
f"{param_name}={_val_to_string(param_field_val)}"
|
|
129
|
+
)
|
|
130
|
+
else:
|
|
131
|
+
pp_vals.append(
|
|
132
|
+
f"{param_name},{_val_to_string(param_field_val)}"
|
|
133
|
+
)
|
|
134
|
+
path_param_values[f_name] = ",".join(pp_vals)
|
|
135
|
+
elif _is_set(param):
|
|
136
|
+
path_param_values[f_name] = _val_to_string(param)
|
|
137
|
+
|
|
138
|
+
return globals_already_populated
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def is_optional(field):
|
|
142
|
+
return get_origin(field) is Union and type(None) in get_args(field)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def template_url(url_with_params: str, params: Dict[str, str]) -> str:
|
|
146
|
+
for key, value in params.items():
|
|
147
|
+
url_with_params = url_with_params.replace("{" + key + "}", value)
|
|
148
|
+
|
|
149
|
+
return url_with_params
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def remove_suffix(input_string, suffix):
|
|
153
|
+
if suffix and input_string.endswith(suffix):
|
|
154
|
+
return input_string[: -len(suffix)]
|
|
155
|
+
return input_string
|