magic-hour 0.0.1__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.
- magic_hour/__init__.py +10 -0
- magic_hour/client.py +75 -0
- magic_hour/core/__init__.py +43 -0
- magic_hour/core/api_error.py +19 -0
- magic_hour/core/auth.py +82 -0
- magic_hour/core/base_client.py +296 -0
- magic_hour/core/binary_response.py +16 -0
- magic_hour/core/request.py +77 -0
- magic_hour/core/response.py +27 -0
- magic_hour/core/utils.py +42 -0
- magic_hour/environment.py +9 -0
- magic_hour/resources/v1/__init__.py +8 -0
- magic_hour/resources/v1/ai_image_generator/__init__.py +8 -0
- magic_hour/resources/v1/ai_image_generator/client.py +80 -0
- magic_hour/resources/v1/client.py +74 -0
- magic_hour/resources/v1/face_swap/__init__.py +8 -0
- magic_hour/resources/v1/face_swap/client.py +82 -0
- magic_hour/resources/v1/face_swap_photo/__init__.py +8 -0
- magic_hour/resources/v1/face_swap_photo/client.py +80 -0
- magic_hour/resources/v1/files/__init__.py +8 -0
- magic_hour/resources/v1/files/client.py +27 -0
- magic_hour/resources/v1/files/upload_urls/__init__.py +8 -0
- magic_hour/resources/v1/files/upload_urls/client.py +118 -0
- magic_hour/resources/v1/image_projects/__init__.py +8 -0
- magic_hour/resources/v1/image_projects/client.py +83 -0
- magic_hour/resources/v1/image_to_video/__init__.py +8 -0
- magic_hour/resources/v1/image_to_video/client.py +86 -0
- magic_hour/resources/v1/lip_sync/__init__.py +8 -0
- magic_hour/resources/v1/lip_sync/client.py +82 -0
- magic_hour/resources/v1/text_to_video/__init__.py +8 -0
- magic_hour/resources/v1/text_to_video/client.py +86 -0
- magic_hour/resources/v1/video_projects/__init__.py +8 -0
- magic_hour/resources/v1/video_projects/client.py +83 -0
- magic_hour/resources/v1/video_to_video/__init__.py +8 -0
- magic_hour/resources/v1/video_to_video/client.py +86 -0
- magic_hour/types/models/__init__.py +40 -0
- magic_hour/types/models/get_v1_image_projects_id_response.py +33 -0
- magic_hour/types/models/get_v1_image_projects_id_response_downloads_item.py +15 -0
- magic_hour/types/models/get_v1_video_projects_id_response.py +42 -0
- magic_hour/types/models/get_v1_video_projects_id_response_download.py +15 -0
- magic_hour/types/models/post_v1_ai_image_generator_response.py +15 -0
- magic_hour/types/models/post_v1_face_swap_photo_response.py +15 -0
- magic_hour/types/models/post_v1_face_swap_response.py +15 -0
- magic_hour/types/models/post_v1_files_upload_urls_response.py +21 -0
- magic_hour/types/models/post_v1_files_upload_urls_response_items_item.py +16 -0
- magic_hour/types/models/post_v1_image_to_video_response.py +15 -0
- magic_hour/types/models/post_v1_lip_sync_response.py +15 -0
- magic_hour/types/models/post_v1_text_to_video_response.py +15 -0
- magic_hour/types/models/post_v1_video_to_video_response.py +15 -0
- magic_hour/types/params/__init__.py +110 -0
- magic_hour/types/params/post_v1_ai_image_generator_body.py +41 -0
- magic_hour/types/params/post_v1_ai_image_generator_body_style.py +25 -0
- magic_hour/types/params/post_v1_face_swap_body.py +41 -0
- magic_hour/types/params/post_v1_face_swap_body_assets.py +42 -0
- magic_hour/types/params/post_v1_face_swap_photo_body.py +33 -0
- magic_hour/types/params/post_v1_face_swap_photo_body_assets.py +29 -0
- magic_hour/types/params/post_v1_files_upload_urls_body.py +33 -0
- magic_hour/types/params/post_v1_files_upload_urls_body_items_item.py +31 -0
- magic_hour/types/params/post_v1_image_to_video_body.py +45 -0
- magic_hour/types/params/post_v1_image_to_video_body_assets.py +27 -0
- magic_hour/types/params/post_v1_image_to_video_body_style.py +26 -0
- magic_hour/types/params/post_v1_lip_sync_body.py +41 -0
- magic_hour/types/params/post_v1_lip_sync_body_assets.py +42 -0
- magic_hour/types/params/post_v1_text_to_video_body.py +41 -0
- magic_hour/types/params/post_v1_text_to_video_body_style.py +25 -0
- magic_hour/types/params/post_v1_video_to_video_body.py +53 -0
- magic_hour/types/params/post_v1_video_to_video_body_assets.py +40 -0
- magic_hour/types/params/post_v1_video_to_video_body_style.py +170 -0
- magic_hour-0.0.1.dist-info/LICENSE +21 -0
- magic_hour-0.0.1.dist-info/METADATA +68 -0
- magic_hour-0.0.1.dist-info/RECORD +72 -0
- magic_hour-0.0.1.dist-info/WHEEL +4 -0
magic_hour/__init__.py
ADDED
magic_hour/client.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Generated by Sideko (sideko.dev)
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
import typing
|
|
7
|
+
|
|
8
|
+
from magic_hour.core import AsyncBaseClient, AuthBearer, SyncBaseClient
|
|
9
|
+
from magic_hour.environment import Environment
|
|
10
|
+
from magic_hour.resources.v1 import AsyncV1Client, V1Client
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Client:
|
|
14
|
+
def __init__(
|
|
15
|
+
self,
|
|
16
|
+
*,
|
|
17
|
+
base_url: typing.Optional[str] = None,
|
|
18
|
+
timeout: typing.Optional[float] = 60,
|
|
19
|
+
httpx_client: typing.Optional[httpx.Client] = None,
|
|
20
|
+
environment: Environment = Environment.ENVIRONMENT,
|
|
21
|
+
token: typing.Optional[str] = None,
|
|
22
|
+
):
|
|
23
|
+
self._base_client = SyncBaseClient(
|
|
24
|
+
base_url=_get_base_url(base_url=base_url, environment=environment),
|
|
25
|
+
httpx_client=(
|
|
26
|
+
httpx.Client(timeout=timeout) if httpx_client is None else httpx_client
|
|
27
|
+
),
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
# register auth methods (keep comment for code generation)
|
|
31
|
+
self._base_client.register_auth("bearerAuth", AuthBearer(val=token))
|
|
32
|
+
|
|
33
|
+
# register sync resources (keep comment for code generation)
|
|
34
|
+
self.v1 = V1Client(base_client=self._base_client)
|
|
35
|
+
|
|
36
|
+
# register sync api methods (keep comment for code generation)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class AsyncClient:
|
|
40
|
+
def __init__(
|
|
41
|
+
self,
|
|
42
|
+
*,
|
|
43
|
+
base_url: typing.Optional[str] = None,
|
|
44
|
+
timeout: typing.Optional[float] = 60,
|
|
45
|
+
httpx_client: typing.Optional[httpx.AsyncClient] = None,
|
|
46
|
+
environment: Environment = Environment.ENVIRONMENT,
|
|
47
|
+
token: typing.Optional[str] = None,
|
|
48
|
+
):
|
|
49
|
+
self._base_client = AsyncBaseClient(
|
|
50
|
+
base_url=_get_base_url(base_url=base_url, environment=environment),
|
|
51
|
+
httpx_client=(
|
|
52
|
+
httpx.AsyncClient(timeout=timeout)
|
|
53
|
+
if httpx_client is None
|
|
54
|
+
else httpx_client
|
|
55
|
+
),
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
# register auth methods (keep comment for code generation)
|
|
59
|
+
self._base_client.register_auth("bearerAuth", AuthBearer(val=token))
|
|
60
|
+
|
|
61
|
+
# register async resources (keep comment for code generation)
|
|
62
|
+
self.v1 = AsyncV1Client(base_client=self._base_client)
|
|
63
|
+
|
|
64
|
+
# register async api methods (keep comment for code generation)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _get_base_url(
|
|
68
|
+
*, base_url: typing.Optional[str] = None, environment: Environment
|
|
69
|
+
) -> str:
|
|
70
|
+
if base_url is not None:
|
|
71
|
+
return base_url
|
|
72
|
+
elif environment is not None:
|
|
73
|
+
return environment.value
|
|
74
|
+
else:
|
|
75
|
+
raise Exception("Must include a base_url or environment arguments")
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Generated by Sideko (sideko.dev)
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from .api_error import ApiError
|
|
6
|
+
from .auth import (
|
|
7
|
+
AuthKeyQuery,
|
|
8
|
+
AuthBasic,
|
|
9
|
+
AuthBearer,
|
|
10
|
+
AuthProvider,
|
|
11
|
+
AuthKeyCookie,
|
|
12
|
+
AuthKeyHeader,
|
|
13
|
+
)
|
|
14
|
+
from .base_client import AsyncBaseClient, BaseClient, SyncBaseClient
|
|
15
|
+
from .binary_response import BinaryResponse
|
|
16
|
+
from .request import (
|
|
17
|
+
encode_param,
|
|
18
|
+
to_encodable,
|
|
19
|
+
RequestOptions,
|
|
20
|
+
default_request_options,
|
|
21
|
+
QueryParams,
|
|
22
|
+
)
|
|
23
|
+
from .response import from_encodable
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"ApiError",
|
|
27
|
+
"AsyncBaseClient",
|
|
28
|
+
"BaseClient",
|
|
29
|
+
"BinaryResponse",
|
|
30
|
+
"RequestOptions",
|
|
31
|
+
"default_request_options",
|
|
32
|
+
"SyncBaseClient",
|
|
33
|
+
"AuthKeyQuery",
|
|
34
|
+
"AuthBasic",
|
|
35
|
+
"AuthBearer",
|
|
36
|
+
"AuthProvider",
|
|
37
|
+
"AuthKeyCookie",
|
|
38
|
+
"AuthKeyHeader",
|
|
39
|
+
"to_encodable",
|
|
40
|
+
"encode_param",
|
|
41
|
+
"from_encodable",
|
|
42
|
+
"QueryParams",
|
|
43
|
+
]
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Generated by Sideko (sideko.dev)
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import typing
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ApiError(Exception):
|
|
9
|
+
status_code: typing.Optional[int]
|
|
10
|
+
body: typing.Any
|
|
11
|
+
|
|
12
|
+
def __init__(
|
|
13
|
+
self, *, status_code: typing.Optional[int] = None, body: typing.Any = None
|
|
14
|
+
):
|
|
15
|
+
self.status_code = status_code
|
|
16
|
+
self.body = body
|
|
17
|
+
|
|
18
|
+
def __str__(self) -> str:
|
|
19
|
+
return f"status_code: {self.status_code}, body: {self.body}"
|
magic_hour/core/auth.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Generated by Sideko (sideko.dev)
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import abc
|
|
6
|
+
import typing
|
|
7
|
+
|
|
8
|
+
from pydantic import BaseModel
|
|
9
|
+
|
|
10
|
+
from .request import RequestConfig
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class AuthProvider(abc.ABC):
|
|
14
|
+
@abc.abstractmethod
|
|
15
|
+
def add_to_request(self, cfg: RequestConfig) -> RequestConfig:
|
|
16
|
+
"""Adds relevant auth to request kwargs"""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class AuthBasic(BaseModel, AuthProvider):
|
|
20
|
+
username: typing.Optional[str]
|
|
21
|
+
password: typing.Optional[str]
|
|
22
|
+
|
|
23
|
+
def add_to_request(self, cfg: RequestConfig) -> RequestConfig:
|
|
24
|
+
if self.username is not None and self.password is not None:
|
|
25
|
+
cfg["auth"] = (self.username, self.password)
|
|
26
|
+
|
|
27
|
+
return cfg
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class AuthBearer(BaseModel, AuthProvider):
|
|
31
|
+
val: typing.Optional[str]
|
|
32
|
+
|
|
33
|
+
def add_to_request(self, cfg: RequestConfig) -> RequestConfig:
|
|
34
|
+
if self.val is not None:
|
|
35
|
+
# Add bearer header auth val
|
|
36
|
+
headers = cfg.get("headers", dict())
|
|
37
|
+
headers["Authorization"] = f"Bearer {self.val}"
|
|
38
|
+
cfg["headers"] = headers
|
|
39
|
+
|
|
40
|
+
return cfg
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class AuthKeyQuery(BaseModel, AuthProvider):
|
|
44
|
+
query_name: str
|
|
45
|
+
val: typing.Optional[str]
|
|
46
|
+
|
|
47
|
+
def add_to_request(self, cfg: RequestConfig) -> RequestConfig:
|
|
48
|
+
if self.val is not None:
|
|
49
|
+
# Add query auth value
|
|
50
|
+
params = cfg.get("params", dict())
|
|
51
|
+
params[self.query_name] = self.val
|
|
52
|
+
cfg["params"] = params
|
|
53
|
+
|
|
54
|
+
return cfg
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class AuthKeyHeader(BaseModel, AuthProvider):
|
|
58
|
+
header_name: str
|
|
59
|
+
val: typing.Optional[str]
|
|
60
|
+
|
|
61
|
+
def add_to_request(self, cfg: RequestConfig) -> RequestConfig:
|
|
62
|
+
if self.val is not None:
|
|
63
|
+
# Add header auth val
|
|
64
|
+
headers = cfg.get("headers", {})
|
|
65
|
+
headers[self.header_name] = self.val
|
|
66
|
+
cfg["headers"] = headers
|
|
67
|
+
|
|
68
|
+
return cfg
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class AuthKeyCookie(BaseModel, AuthProvider):
|
|
72
|
+
cookie_name: str
|
|
73
|
+
val: typing.Optional[str]
|
|
74
|
+
|
|
75
|
+
def add_to_request(self, cfg: RequestConfig) -> RequestConfig:
|
|
76
|
+
if self.val is not None:
|
|
77
|
+
# Add cookie auth val
|
|
78
|
+
cookies = cfg.get("cookies", dict())
|
|
79
|
+
cookies[self.cookie_name] = self.val
|
|
80
|
+
cfg["cookies"] = cookies
|
|
81
|
+
|
|
82
|
+
return cfg
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Generated by Sideko (sideko.dev)
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from json import JSONDecodeError
|
|
6
|
+
from typing import Any, List, TypeVar, Dict, Optional, Type, Union, cast
|
|
7
|
+
|
|
8
|
+
import httpx
|
|
9
|
+
from pydantic import BaseModel
|
|
10
|
+
|
|
11
|
+
from .api_error import ApiError
|
|
12
|
+
from .auth import AuthProvider
|
|
13
|
+
from .request import RequestConfig, RequestOptions, default_request_options, QueryParams
|
|
14
|
+
from .response import from_encodable
|
|
15
|
+
from .utils import is_binary_content_type, get_content_type
|
|
16
|
+
from .binary_response import BinaryResponse
|
|
17
|
+
|
|
18
|
+
NoneType = type(None)
|
|
19
|
+
T = TypeVar(
|
|
20
|
+
"T",
|
|
21
|
+
bound=Union[object, None, str, "BaseModel", List[Any], Dict[str, Any], Any],
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class BaseClient:
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
*,
|
|
29
|
+
base_url: str,
|
|
30
|
+
):
|
|
31
|
+
self._base_url = base_url
|
|
32
|
+
self._auths: Dict[str, AuthProvider] = {}
|
|
33
|
+
|
|
34
|
+
def register_auth(self, auth_id: str, provider: AuthProvider):
|
|
35
|
+
self._auths[auth_id] = provider
|
|
36
|
+
|
|
37
|
+
def default_headers(self) -> Dict[str, str]:
|
|
38
|
+
headers: Dict[str, str] = {
|
|
39
|
+
"x-sideko-sdk-language": "Python",
|
|
40
|
+
}
|
|
41
|
+
return headers
|
|
42
|
+
|
|
43
|
+
def get_base_url(self) -> str:
|
|
44
|
+
return self._base_url
|
|
45
|
+
|
|
46
|
+
def build_url(self, path: str) -> str:
|
|
47
|
+
base = self._base_url
|
|
48
|
+
if base.endswith("/"):
|
|
49
|
+
base = base[:-1]
|
|
50
|
+
if path.startswith("/"):
|
|
51
|
+
path = path[1:]
|
|
52
|
+
|
|
53
|
+
return f"{base}/{path}"
|
|
54
|
+
|
|
55
|
+
def _apply_auth(
|
|
56
|
+
self, *, cfg: RequestConfig, auth_names: List[str]
|
|
57
|
+
) -> RequestConfig:
|
|
58
|
+
for auth_name in auth_names:
|
|
59
|
+
auth_provider = self._auths.get(auth_name)
|
|
60
|
+
if auth_provider is not None:
|
|
61
|
+
cfg = auth_provider.add_to_request(cfg)
|
|
62
|
+
|
|
63
|
+
return cfg
|
|
64
|
+
|
|
65
|
+
def _apply_headers(
|
|
66
|
+
self,
|
|
67
|
+
*,
|
|
68
|
+
cfg: RequestConfig,
|
|
69
|
+
opts: RequestOptions,
|
|
70
|
+
content_type: Optional[str] = None,
|
|
71
|
+
explicit_headers: Optional[Dict[str, str]] = None,
|
|
72
|
+
) -> RequestConfig:
|
|
73
|
+
headers = cfg.get("headers", {})
|
|
74
|
+
headers.update(self.default_headers())
|
|
75
|
+
|
|
76
|
+
if content_type is not None:
|
|
77
|
+
headers["content-type"] = content_type
|
|
78
|
+
|
|
79
|
+
if explicit_headers is not None:
|
|
80
|
+
headers.update(explicit_headers)
|
|
81
|
+
|
|
82
|
+
additional_headers = opts.get("additional_headers", None)
|
|
83
|
+
if additional_headers is not None:
|
|
84
|
+
headers.update(additional_headers)
|
|
85
|
+
|
|
86
|
+
if len(headers) > 0:
|
|
87
|
+
cfg["headers"] = headers
|
|
88
|
+
|
|
89
|
+
return cfg
|
|
90
|
+
|
|
91
|
+
def _apply_query_params(
|
|
92
|
+
self,
|
|
93
|
+
*,
|
|
94
|
+
cfg: RequestConfig,
|
|
95
|
+
opts: RequestOptions,
|
|
96
|
+
query_params: Optional[QueryParams] = None,
|
|
97
|
+
) -> RequestConfig:
|
|
98
|
+
params = cfg.get("params", {})
|
|
99
|
+
|
|
100
|
+
if query_params is not None:
|
|
101
|
+
params.update(query_params)
|
|
102
|
+
|
|
103
|
+
additional_params = opts.get("additional_params", None)
|
|
104
|
+
if additional_params is not None:
|
|
105
|
+
params.update(additional_params)
|
|
106
|
+
|
|
107
|
+
if len(params) > 0:
|
|
108
|
+
cfg["params"] = params
|
|
109
|
+
|
|
110
|
+
return cfg
|
|
111
|
+
|
|
112
|
+
def _apply_timeout(
|
|
113
|
+
self,
|
|
114
|
+
*,
|
|
115
|
+
cfg: RequestConfig,
|
|
116
|
+
opts: RequestOptions,
|
|
117
|
+
) -> RequestConfig:
|
|
118
|
+
timeout = opts.get("timeout", None)
|
|
119
|
+
|
|
120
|
+
# TODO: timeout from base class
|
|
121
|
+
if timeout is not None:
|
|
122
|
+
cfg["timeout"] = timeout
|
|
123
|
+
|
|
124
|
+
return cfg
|
|
125
|
+
|
|
126
|
+
def _apply_body(
|
|
127
|
+
self,
|
|
128
|
+
*,
|
|
129
|
+
cfg: RequestConfig,
|
|
130
|
+
data: Optional[httpx._types.RequestData] = None,
|
|
131
|
+
files: Optional[httpx._types.RequestFiles] = None,
|
|
132
|
+
json: Optional[Any] = None,
|
|
133
|
+
content: Optional[httpx._types.RequestContent] = None,
|
|
134
|
+
) -> RequestConfig:
|
|
135
|
+
if data is not None:
|
|
136
|
+
cfg["data"] = data
|
|
137
|
+
|
|
138
|
+
if files is not None:
|
|
139
|
+
cfg["files"] = files
|
|
140
|
+
|
|
141
|
+
if json is not None:
|
|
142
|
+
cfg["json"] = json
|
|
143
|
+
|
|
144
|
+
if content is not None:
|
|
145
|
+
cfg["content"] = content
|
|
146
|
+
|
|
147
|
+
return cfg
|
|
148
|
+
|
|
149
|
+
def build_request(
|
|
150
|
+
self,
|
|
151
|
+
*,
|
|
152
|
+
method: str,
|
|
153
|
+
path: str,
|
|
154
|
+
auth_names: Optional[List[str]] = None,
|
|
155
|
+
query_params: Optional[QueryParams] = None,
|
|
156
|
+
headers: Optional[Dict[str, str]] = None,
|
|
157
|
+
data: Optional[httpx._types.RequestData] = None,
|
|
158
|
+
files: Optional[httpx._types.RequestFiles] = None,
|
|
159
|
+
json: Optional[Any] = None,
|
|
160
|
+
content_type: Optional[str] = None,
|
|
161
|
+
content: Optional[httpx._types.RequestContent] = None,
|
|
162
|
+
request_options: Optional[RequestOptions] = None,
|
|
163
|
+
) -> RequestConfig:
|
|
164
|
+
opts = request_options or default_request_options()
|
|
165
|
+
req_cfg: RequestConfig = {"method": method, "url": self.build_url(path)}
|
|
166
|
+
req_cfg = self._apply_auth(cfg=req_cfg, auth_names=auth_names or [])
|
|
167
|
+
req_cfg = self._apply_headers(
|
|
168
|
+
cfg=req_cfg, opts=opts, content_type=content_type, explicit_headers=headers
|
|
169
|
+
)
|
|
170
|
+
req_cfg = self._apply_query_params(
|
|
171
|
+
cfg=req_cfg, opts=opts, query_params=query_params
|
|
172
|
+
)
|
|
173
|
+
req_cfg = self._apply_body(
|
|
174
|
+
cfg=req_cfg, data=data, files=files, json=json, content=content
|
|
175
|
+
)
|
|
176
|
+
req_cfg = self._apply_timeout(cfg=req_cfg, opts=opts)
|
|
177
|
+
|
|
178
|
+
return req_cfg
|
|
179
|
+
|
|
180
|
+
def process_response(self, *, response=httpx.Response, cast_to: Type[T]) -> T:
|
|
181
|
+
if 200 <= response.status_code < 300:
|
|
182
|
+
content_type = get_content_type(response.headers)
|
|
183
|
+
if response.status_code == 204 or cast_to == NoneType:
|
|
184
|
+
return cast(T, None)
|
|
185
|
+
elif cast_to == BinaryResponse:
|
|
186
|
+
return cast(
|
|
187
|
+
T,
|
|
188
|
+
BinaryResponse(content=response.content, headers=response.headers),
|
|
189
|
+
)
|
|
190
|
+
else:
|
|
191
|
+
if "json" in content_type or "form" in content_type:
|
|
192
|
+
return from_encodable(data=response.json(), load_with=cast_to)
|
|
193
|
+
elif is_binary_content_type(content_type):
|
|
194
|
+
return cast(
|
|
195
|
+
T,
|
|
196
|
+
BinaryResponse(
|
|
197
|
+
content=response.content, headers=response.headers
|
|
198
|
+
),
|
|
199
|
+
)
|
|
200
|
+
else:
|
|
201
|
+
return from_encodable(data=response.content, load_with=cast_to)
|
|
202
|
+
else:
|
|
203
|
+
try:
|
|
204
|
+
response_json = response.json()
|
|
205
|
+
except JSONDecodeError:
|
|
206
|
+
raise ApiError(status_code=response.status_code, body=response.text)
|
|
207
|
+
raise ApiError(status_code=response.status_code, body=response_json)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
class SyncBaseClient(BaseClient):
|
|
211
|
+
def __init__(
|
|
212
|
+
self,
|
|
213
|
+
*,
|
|
214
|
+
base_url: str,
|
|
215
|
+
httpx_client: httpx.Client,
|
|
216
|
+
):
|
|
217
|
+
super().__init__(base_url=base_url)
|
|
218
|
+
self.httpx_client = httpx_client
|
|
219
|
+
|
|
220
|
+
def request(
|
|
221
|
+
self,
|
|
222
|
+
*,
|
|
223
|
+
method: str,
|
|
224
|
+
path: str,
|
|
225
|
+
cast_to: Type[T],
|
|
226
|
+
auth_names: Optional[List[str]] = None,
|
|
227
|
+
query_params: Optional[QueryParams] = None,
|
|
228
|
+
headers: Optional[Dict[str, str]] = None,
|
|
229
|
+
data: Optional[httpx._types.RequestData] = None,
|
|
230
|
+
files: Optional[httpx._types.RequestFiles] = None,
|
|
231
|
+
json: Optional[Any] = None,
|
|
232
|
+
content_type: Optional[str] = None,
|
|
233
|
+
content: Optional[httpx._types.RequestContent] = None,
|
|
234
|
+
request_options: Optional[RequestOptions] = None,
|
|
235
|
+
) -> T:
|
|
236
|
+
req_cfg = self.build_request(
|
|
237
|
+
method=method,
|
|
238
|
+
path=path,
|
|
239
|
+
auth_names=auth_names,
|
|
240
|
+
query_params=query_params,
|
|
241
|
+
headers=headers,
|
|
242
|
+
data=data,
|
|
243
|
+
files=files,
|
|
244
|
+
json=json,
|
|
245
|
+
content_type=content_type,
|
|
246
|
+
content=content,
|
|
247
|
+
request_options=request_options,
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
response = self.httpx_client.request(**req_cfg)
|
|
251
|
+
|
|
252
|
+
return self.process_response(response=response, cast_to=cast_to)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
class AsyncBaseClient(BaseClient):
|
|
256
|
+
def __init__(
|
|
257
|
+
self,
|
|
258
|
+
*,
|
|
259
|
+
base_url: str,
|
|
260
|
+
httpx_client: httpx.AsyncClient,
|
|
261
|
+
):
|
|
262
|
+
super().__init__(base_url=base_url)
|
|
263
|
+
self.httpx_client = httpx_client
|
|
264
|
+
|
|
265
|
+
async def request(
|
|
266
|
+
self,
|
|
267
|
+
method: str,
|
|
268
|
+
path: str,
|
|
269
|
+
cast_to: Type[T],
|
|
270
|
+
auth_names: Optional[List[str]] = None,
|
|
271
|
+
query_params: Optional[QueryParams] = None,
|
|
272
|
+
headers: Optional[Dict[str, str]] = None,
|
|
273
|
+
data: Optional[httpx._types.RequestData] = None,
|
|
274
|
+
files: Optional[httpx._types.RequestFiles] = None,
|
|
275
|
+
json: Optional[Any] = None,
|
|
276
|
+
content_type: Optional[str] = None,
|
|
277
|
+
content: Optional[httpx._types.RequestContent] = None,
|
|
278
|
+
request_options: Optional[RequestOptions] = None,
|
|
279
|
+
) -> T:
|
|
280
|
+
req_cfg = self.build_request(
|
|
281
|
+
method=method,
|
|
282
|
+
path=path,
|
|
283
|
+
auth_names=auth_names,
|
|
284
|
+
query_params=query_params,
|
|
285
|
+
headers=headers,
|
|
286
|
+
data=data,
|
|
287
|
+
files=files,
|
|
288
|
+
json=json,
|
|
289
|
+
content_type=content_type,
|
|
290
|
+
content=content,
|
|
291
|
+
request_options=request_options,
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
response = await self.httpx_client.request(**req_cfg)
|
|
295
|
+
|
|
296
|
+
return self.process_response(response=response, cast_to=cast_to)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Generated by Sideko (sideko.dev)
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
"""File Generated by Sideko (sideko.dev)"""
|
|
6
|
+
|
|
7
|
+
from httpx._models import Headers
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class BinaryResponse:
|
|
11
|
+
content: bytes
|
|
12
|
+
headers: Headers
|
|
13
|
+
|
|
14
|
+
def __init__(self, content: bytes, headers: Headers) -> None:
|
|
15
|
+
self.content = content
|
|
16
|
+
self.headers = headers
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Generated by Sideko (sideko.dev)
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import Any, Dict, Type, Union, Sequence
|
|
6
|
+
from urllib.parse import quote_plus
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
import httpx
|
|
10
|
+
from typing_extensions import TypedDict, Required, NotRequired
|
|
11
|
+
from pydantic import TypeAdapter, BaseModel
|
|
12
|
+
|
|
13
|
+
QueryParams = Dict[
|
|
14
|
+
str, Union[httpx._types.PrimitiveData, Sequence[httpx._types.PrimitiveData]]
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class RequestConfig(TypedDict):
|
|
19
|
+
method: Required[str]
|
|
20
|
+
url: Required[httpx._types.URLTypes]
|
|
21
|
+
content: NotRequired[httpx._types.RequestContent]
|
|
22
|
+
data: NotRequired[httpx._types.RequestData]
|
|
23
|
+
files: NotRequired[httpx._types.RequestFiles]
|
|
24
|
+
json: NotRequired[Any]
|
|
25
|
+
params: NotRequired[QueryParams]
|
|
26
|
+
headers: NotRequired[Dict[str, str]]
|
|
27
|
+
cookies: NotRequired[Dict[str, str]]
|
|
28
|
+
auth: NotRequired[httpx._types.AuthTypes]
|
|
29
|
+
follow_redirects: NotRequired[bool]
|
|
30
|
+
timeout: NotRequired[httpx._types.TimeoutTypes]
|
|
31
|
+
extensions: NotRequired[httpx._types.RequestExtensions]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class RequestOptions(TypedDict):
|
|
35
|
+
"""
|
|
36
|
+
Attributes:
|
|
37
|
+
- timeout: int. The number of seconds to await an API call before timing out.
|
|
38
|
+
|
|
39
|
+
- additional_headers: typing.Dict[str, typing.Any]. A dictionary containing additional parameters to spread into the request's header dict
|
|
40
|
+
|
|
41
|
+
- additional_params: typing.Dict[str, typing.Any]. A dictionary containing additional parameters to spread into the request's query parameters dict
|
|
42
|
+
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
timeout: NotRequired[int]
|
|
46
|
+
additional_headers: NotRequired[Dict[str, str]]
|
|
47
|
+
additional_params: NotRequired[QueryParams]
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def default_request_options() -> RequestOptions:
|
|
51
|
+
"""Returns default request options"""
|
|
52
|
+
return {}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def model_dump(item: Any) -> Any:
|
|
56
|
+
if isinstance(item, list):
|
|
57
|
+
return [model_dump(i) for i in item]
|
|
58
|
+
|
|
59
|
+
if isinstance(item, BaseModel):
|
|
60
|
+
return item.model_dump(exclude_unset=True, by_alias=True)
|
|
61
|
+
else:
|
|
62
|
+
return item
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def to_encodable(*, item: Any, dump_with: Type) -> Any:
|
|
66
|
+
adapter = TypeAdapter(dump_with)
|
|
67
|
+
validated_item = adapter.validate_python(item)
|
|
68
|
+
return model_dump(validated_item)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def encode_param(
|
|
72
|
+
value: Any, explode: bool
|
|
73
|
+
) -> Union[httpx._types.PrimitiveData, Sequence[httpx._types.PrimitiveData]]:
|
|
74
|
+
if isinstance(value, (list, dict)) and not explode:
|
|
75
|
+
return quote_plus(",".join(map(str, value)))
|
|
76
|
+
else:
|
|
77
|
+
return value
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Generated by Sideko (sideko.dev)
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import Any, Union, Dict, Type, TypeVar, List
|
|
6
|
+
from pydantic import BaseModel
|
|
7
|
+
|
|
8
|
+
EncodableT = TypeVar(
|
|
9
|
+
"EncodableT",
|
|
10
|
+
bound=Union[
|
|
11
|
+
object,
|
|
12
|
+
str,
|
|
13
|
+
int,
|
|
14
|
+
float,
|
|
15
|
+
None,
|
|
16
|
+
BaseModel,
|
|
17
|
+
List[Any],
|
|
18
|
+
Dict[str, Any],
|
|
19
|
+
],
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def from_encodable(*, data: Any, load_with: Type[EncodableT]) -> Any:
|
|
24
|
+
class Caster(BaseModel):
|
|
25
|
+
data: load_with # type: ignore
|
|
26
|
+
|
|
27
|
+
return Caster(data=data).data
|
magic_hour/core/utils.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Generated by Sideko (sideko.dev)
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import typing
|
|
6
|
+
import httpx
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def remove_none_from_dict(
|
|
10
|
+
original: typing.Dict[str, typing.Optional[typing.Any]],
|
|
11
|
+
) -> typing.Dict[str, typing.Any]:
|
|
12
|
+
new: typing.Dict[str, typing.Any] = {}
|
|
13
|
+
for key, value in original.items():
|
|
14
|
+
if value is not None:
|
|
15
|
+
new[key] = value
|
|
16
|
+
return new
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def is_binary_content_type(content_type: str) -> bool:
|
|
20
|
+
"""Check if the content type indicates binary data."""
|
|
21
|
+
binary_types = [
|
|
22
|
+
"application/octet-stream",
|
|
23
|
+
"application/pdf",
|
|
24
|
+
"application/zip",
|
|
25
|
+
"image/",
|
|
26
|
+
"audio/",
|
|
27
|
+
"video/",
|
|
28
|
+
"application/msword",
|
|
29
|
+
"application/vnd.openxmlformats-officedocument",
|
|
30
|
+
"application/x-binary",
|
|
31
|
+
"application/vnd.ms-excel",
|
|
32
|
+
"application/vnd.ms-powerpoint",
|
|
33
|
+
]
|
|
34
|
+
return any(binary_type in content_type for binary_type in binary_types)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def get_content_type(headers: httpx.Headers) -> str:
|
|
38
|
+
"""Get content type in a case-insensitive manner."""
|
|
39
|
+
for key, value in headers.items():
|
|
40
|
+
if key.lower() == "content-type":
|
|
41
|
+
return value.lower()
|
|
42
|
+
return ""
|