magic-hour 0.0.1__tar.gz
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-0.0.1/LICENSE +21 -0
- magic_hour-0.0.1/PKG-INFO +68 -0
- magic_hour-0.0.1/README.md +51 -0
- magic_hour-0.0.1/magic_hour/__init__.py +10 -0
- magic_hour-0.0.1/magic_hour/client.py +75 -0
- magic_hour-0.0.1/magic_hour/core/__init__.py +43 -0
- magic_hour-0.0.1/magic_hour/core/api_error.py +19 -0
- magic_hour-0.0.1/magic_hour/core/auth.py +82 -0
- magic_hour-0.0.1/magic_hour/core/base_client.py +296 -0
- magic_hour-0.0.1/magic_hour/core/binary_response.py +16 -0
- magic_hour-0.0.1/magic_hour/core/request.py +77 -0
- magic_hour-0.0.1/magic_hour/core/response.py +27 -0
- magic_hour-0.0.1/magic_hour/core/utils.py +42 -0
- magic_hour-0.0.1/magic_hour/environment.py +9 -0
- magic_hour-0.0.1/magic_hour/resources/v1/__init__.py +8 -0
- magic_hour-0.0.1/magic_hour/resources/v1/ai_image_generator/__init__.py +8 -0
- magic_hour-0.0.1/magic_hour/resources/v1/ai_image_generator/client.py +80 -0
- magic_hour-0.0.1/magic_hour/resources/v1/client.py +74 -0
- magic_hour-0.0.1/magic_hour/resources/v1/face_swap/__init__.py +8 -0
- magic_hour-0.0.1/magic_hour/resources/v1/face_swap/client.py +82 -0
- magic_hour-0.0.1/magic_hour/resources/v1/face_swap_photo/__init__.py +8 -0
- magic_hour-0.0.1/magic_hour/resources/v1/face_swap_photo/client.py +80 -0
- magic_hour-0.0.1/magic_hour/resources/v1/files/__init__.py +8 -0
- magic_hour-0.0.1/magic_hour/resources/v1/files/client.py +27 -0
- magic_hour-0.0.1/magic_hour/resources/v1/files/upload_urls/__init__.py +8 -0
- magic_hour-0.0.1/magic_hour/resources/v1/files/upload_urls/client.py +118 -0
- magic_hour-0.0.1/magic_hour/resources/v1/image_projects/__init__.py +8 -0
- magic_hour-0.0.1/magic_hour/resources/v1/image_projects/client.py +83 -0
- magic_hour-0.0.1/magic_hour/resources/v1/image_to_video/__init__.py +8 -0
- magic_hour-0.0.1/magic_hour/resources/v1/image_to_video/client.py +86 -0
- magic_hour-0.0.1/magic_hour/resources/v1/lip_sync/__init__.py +8 -0
- magic_hour-0.0.1/magic_hour/resources/v1/lip_sync/client.py +82 -0
- magic_hour-0.0.1/magic_hour/resources/v1/text_to_video/__init__.py +8 -0
- magic_hour-0.0.1/magic_hour/resources/v1/text_to_video/client.py +86 -0
- magic_hour-0.0.1/magic_hour/resources/v1/video_projects/__init__.py +8 -0
- magic_hour-0.0.1/magic_hour/resources/v1/video_projects/client.py +83 -0
- magic_hour-0.0.1/magic_hour/resources/v1/video_to_video/__init__.py +8 -0
- magic_hour-0.0.1/magic_hour/resources/v1/video_to_video/client.py +86 -0
- magic_hour-0.0.1/magic_hour/types/models/__init__.py +40 -0
- magic_hour-0.0.1/magic_hour/types/models/get_v1_image_projects_id_response.py +33 -0
- magic_hour-0.0.1/magic_hour/types/models/get_v1_image_projects_id_response_downloads_item.py +15 -0
- magic_hour-0.0.1/magic_hour/types/models/get_v1_video_projects_id_response.py +42 -0
- magic_hour-0.0.1/magic_hour/types/models/get_v1_video_projects_id_response_download.py +15 -0
- magic_hour-0.0.1/magic_hour/types/models/post_v1_ai_image_generator_response.py +15 -0
- magic_hour-0.0.1/magic_hour/types/models/post_v1_face_swap_photo_response.py +15 -0
- magic_hour-0.0.1/magic_hour/types/models/post_v1_face_swap_response.py +15 -0
- magic_hour-0.0.1/magic_hour/types/models/post_v1_files_upload_urls_response.py +21 -0
- magic_hour-0.0.1/magic_hour/types/models/post_v1_files_upload_urls_response_items_item.py +16 -0
- magic_hour-0.0.1/magic_hour/types/models/post_v1_image_to_video_response.py +15 -0
- magic_hour-0.0.1/magic_hour/types/models/post_v1_lip_sync_response.py +15 -0
- magic_hour-0.0.1/magic_hour/types/models/post_v1_text_to_video_response.py +15 -0
- magic_hour-0.0.1/magic_hour/types/models/post_v1_video_to_video_response.py +15 -0
- magic_hour-0.0.1/magic_hour/types/params/__init__.py +110 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_ai_image_generator_body.py +41 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_ai_image_generator_body_style.py +25 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_face_swap_body.py +41 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_face_swap_body_assets.py +42 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_face_swap_photo_body.py +33 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_face_swap_photo_body_assets.py +29 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_files_upload_urls_body.py +33 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_files_upload_urls_body_items_item.py +31 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_image_to_video_body.py +45 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_image_to_video_body_assets.py +27 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_image_to_video_body_style.py +26 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_lip_sync_body.py +41 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_lip_sync_body_assets.py +42 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_text_to_video_body.py +41 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_text_to_video_body_style.py +25 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_video_to_video_body.py +53 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_video_to_video_body_assets.py +40 -0
- magic_hour-0.0.1/magic_hour/types/params/post_v1_video_to_video_body_style.py +170 -0
- magic_hour-0.0.1/pyproject.toml +22 -0
magic_hour-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Magic Hour AI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: magic_hour
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary:
|
|
5
|
+
Requires-Python: >=3.8,<4.0
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Requires-Dist: httpx (>=0.26.0,<0.27.0)
|
|
14
|
+
Requires-Dist: pydantic (>=2.5.0,<3.0.0)
|
|
15
|
+
Requires-Dist: typing_extensions (>=4.0.0)
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# Magic Hour API Python SDK
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## Overview
|
|
23
|
+
|
|
24
|
+
# Introduction
|
|
25
|
+
|
|
26
|
+
Magic Hour provides an API (beta) that can be integrated into your own application to generate videos using AI.
|
|
27
|
+
|
|
28
|
+
Webhook documentation can be found [here](https://magichour.ai/docs/webhook).
|
|
29
|
+
|
|
30
|
+
If you have any questions, please reach out to us via [discord](https://discord.gg/JX5rgsZaJp).
|
|
31
|
+
|
|
32
|
+
# Authentication
|
|
33
|
+
|
|
34
|
+
Every request requires an API key.
|
|
35
|
+
|
|
36
|
+
To get started, first generate your API key [here](https://magichour.ai/settings/developer).
|
|
37
|
+
|
|
38
|
+
Then, add the `Authorization` header to the request.
|
|
39
|
+
|
|
40
|
+
| Key | Value |
|
|
41
|
+
|-|-|
|
|
42
|
+
| Authorization | Bearer mhk_live_apikey |
|
|
43
|
+
|
|
44
|
+
> **Warning**: any API call that renders a video will utilize frames in your account.
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
### Synchronous Client
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from magic_hour import Client
|
|
52
|
+
from os import getenv
|
|
53
|
+
|
|
54
|
+
client = Client(token=getenv("API_TOKEN"))
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
### Asynchronous Client
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from magic_hour import AsyncClient
|
|
62
|
+
from os import getenv
|
|
63
|
+
|
|
64
|
+
client = AsyncClient(token=getenv("API_TOKEN"))
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### SDK Usage
|
|
68
|
+
See [SDK Examples](SDK_EXAMPLES.md) for example usage of all SDK functionality
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
|
|
2
|
+
# Magic Hour API Python SDK
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
# Introduction
|
|
8
|
+
|
|
9
|
+
Magic Hour provides an API (beta) that can be integrated into your own application to generate videos using AI.
|
|
10
|
+
|
|
11
|
+
Webhook documentation can be found [here](https://magichour.ai/docs/webhook).
|
|
12
|
+
|
|
13
|
+
If you have any questions, please reach out to us via [discord](https://discord.gg/JX5rgsZaJp).
|
|
14
|
+
|
|
15
|
+
# Authentication
|
|
16
|
+
|
|
17
|
+
Every request requires an API key.
|
|
18
|
+
|
|
19
|
+
To get started, first generate your API key [here](https://magichour.ai/settings/developer).
|
|
20
|
+
|
|
21
|
+
Then, add the `Authorization` header to the request.
|
|
22
|
+
|
|
23
|
+
| Key | Value |
|
|
24
|
+
|-|-|
|
|
25
|
+
| Authorization | Bearer mhk_live_apikey |
|
|
26
|
+
|
|
27
|
+
> **Warning**: any API call that renders a video will utilize frames in your account.
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### Synchronous Client
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from magic_hour import Client
|
|
35
|
+
from os import getenv
|
|
36
|
+
|
|
37
|
+
client = Client(token=getenv("API_TOKEN"))
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
### Asynchronous Client
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from magic_hour import AsyncClient
|
|
45
|
+
from os import getenv
|
|
46
|
+
|
|
47
|
+
client = AsyncClient(token=getenv("API_TOKEN"))
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### SDK Usage
|
|
51
|
+
See [SDK Examples](SDK_EXAMPLES.md) for example usage of all SDK functionality
|
|
@@ -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}"
|
|
@@ -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
|