-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds async client and refactors API implementation
- Loading branch information
Showing
11 changed files
with
989 additions
and
188 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import asyncio | ||
from io import BytesIO | ||
from typing import List | ||
|
||
from upyloadthing.async_client import AsyncUTApi | ||
from upyloadthing.schemas import UploadResult | ||
|
||
|
||
async def main(): | ||
print("🚀 UploadThing API Demo (Async)\n") | ||
|
||
# Initialize the client | ||
api = AsyncUTApi() | ||
|
||
# Get usage info | ||
print("📊 Getting usage info...") | ||
usage_info = await api.get_usage_info() | ||
print(f"Total bytes used: {usage_info.total_bytes}") | ||
print(f"Files uploaded: {usage_info.files_uploaded}") | ||
print(f"Storage limit: {usage_info.limit_bytes}\n") | ||
|
||
# List files | ||
print("📋 Listing files...") | ||
file_list = await api.list_files(limit=5) | ||
print( | ||
f"Fetched {len(file_list.files)} files, has more: {file_list.has_more}" | ||
) | ||
for file in file_list.files: | ||
print(file) | ||
print() | ||
|
||
# Prepare test files | ||
print("📤 Uploading test images...") | ||
|
||
# Prepare PNG file | ||
with open("./examples/test.png", "rb") as f: | ||
image_content = f.read() | ||
png_file = BytesIO(image_content) | ||
png_file.name = "test.png" | ||
|
||
# Prepare Jpeg file | ||
with open("./examples/test.jpg", "rb") as f: | ||
image_content = f.read() | ||
jpeg_file = BytesIO(image_content) | ||
jpeg_file.name = "test.jpg" | ||
|
||
# Upload both files | ||
upload_results: List[UploadResult] = await api.upload_files( | ||
[png_file, jpeg_file], acl="public-read" | ||
) | ||
|
||
print("Upload results:") | ||
for result in upload_results: | ||
print(f"- {result.name}: {result.file_key}") | ||
print() | ||
|
||
# Delete the uploaded files | ||
print("🗑️ Deleting test files...") | ||
file_keys = [result.file_key for result in upload_results] | ||
delete_result = await api.delete_files(file_keys) | ||
print(f"Deleted {delete_result.deleted_count} file(s)") | ||
print(f"Success: {delete_result.success}\n") | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.