-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[UPY-7] Support async operations #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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}") | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+47
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for concurrent file uploads. The concurrent upload implementation looks good, but consider adding error handling for individual file failures. # Upload both files
- upload_results: List[UploadResult] = await api.upload_files(
- [png_file, jpeg_file], acl="public-read"
- )
+ try:
+ upload_results: List[UploadResult] = await api.upload_files(
+ [png_file, jpeg_file], acl="public-read"
+ )
+ except Exception as e:
+ print(f"Error uploading files: {e}")
+ return 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
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()) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Method signatures are well documented, but error handling needs update.
The method signatures with type hints are clear and helpful. However, there's a discrepancy in the error handling section.
The error handling example uses
requests.exceptions.HTTPError
but the client useshttpx
. Update the error handling section to use the correct exception: