-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support to upload image and generate contents from image
- Loading branch information
1 parent
6c48d50
commit d2274c2
Showing
5 changed files
with
160 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from httpx import AsyncClient | ||
from pydantic import validate_call | ||
|
||
from .constant import UPLOAD_PUSHID | ||
|
||
|
||
@validate_call | ||
async def upload_file(file: bytes | str) -> str: | ||
""" | ||
Upload a file to Google's server and return its identifier. | ||
Parameters | ||
---------- | ||
file : `bytes` | `str` | ||
File data in bytes, or path to the file to be uploaded. | ||
Returns | ||
------- | ||
`str` | ||
Identifier of the uploaded file. | ||
E.g. "/contrib_service/ttl_1d/1709764705i7wdlyx3mdzndme3a767pluckv4flj" | ||
Raises | ||
------ | ||
`httpx.HTTPStatusError` | ||
If the upload request failed. | ||
""" | ||
|
||
if isinstance(file, str): | ||
with open(file, "rb") as f: | ||
file = f.read() | ||
|
||
async with AsyncClient() as client: | ||
response = await client.post( | ||
url="https://content-push.googleapis.com/upload/", | ||
headers={"Push-ID": UPLOAD_PUSHID}, | ||
files={"file": file}, | ||
follow_redirects=True, | ||
) | ||
response.raise_for_status() | ||
return response.text |
Oops, something went wrong.