-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
feat: update file upload and download mechanism on toolset #1124
Open
angrybayblade
wants to merge
12
commits into
master
Choose a base branch
from
feat/file-handling
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+908
−179
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
81c4a34
feat: update file upload and download mechanism on toolset
angrybayblade 19e16a9
Merge branch 'master' into feat/file-handling
angrybayblade 944e2ef
feat: use `accept` field to validate mimetype before uploading
angrybayblade 671e393
fix: update response wrapper
angrybayblade 72314fa
fix: handle backwards incompatibility errors
angrybayblade 2cda69a
Merge branch 'master' into feat/file-handling
angrybayblade 7af70d2
feat: file uploadable/downloadable premitives
angrybayblade 5dff124
feat: file uploadable/downloadable subtitution at runtime
angrybayblade 4dd1703
fix: tests
angrybayblade 565d03f
fix: URL reuse exception
angrybayblade 905beef
Merge branch 'master' into feat/file-handling
angrybayblade f8d0db0
fix: get_single_action schema
angrybayblade File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,94 @@ | ||
from __future__ import annotations | ||
|
||
import hashlib | ||
import typing as t | ||
from pathlib import Path | ||
|
||
import requests | ||
import typing_extensions as te | ||
from pydantic import BaseModel, ConfigDict, Field | ||
|
||
from composio.exceptions import ComposioSDKError | ||
from composio.utils import mimetypes | ||
|
||
|
||
# pylint: disable=missing-timeout | ||
|
||
if te.TYPE_CHECKING: | ||
from composio.client import Composio | ||
|
||
|
||
_DEFAULT_CHUNK_SIZE = 1024 * 1024 | ||
|
||
|
||
def get_md5(file: Path): | ||
obj = hashlib.md5() | ||
with file.open("rb") as fp: | ||
while True: | ||
line = fp.read(_DEFAULT_CHUNK_SIZE) | ||
if not line: | ||
break | ||
obj.update(line) | ||
return obj.hexdigest() | ||
|
||
|
||
def upload(url: str, file: Path) -> bool: | ||
with file.open("rb") as data: | ||
return requests.put(url=url, data=data).status_code in (200, 403) | ||
|
||
|
||
class FileUploadable(BaseModel): | ||
model_config = ConfigDict(json_schema_extra={"file_uploadable": True}) | ||
|
||
name: str | ||
mimetype: str | ||
s3key: str | ||
|
||
@classmethod | ||
def from_path( | ||
cls, | ||
file: t.Union[str, Path], | ||
client: Composio, | ||
action: str, | ||
app: str, | ||
) -> te.Self: | ||
file = Path(file) | ||
if not file.exists(): | ||
raise ComposioSDKError(f"File not found: {file}") | ||
|
||
mimetype = mimetypes.guess(file=file) | ||
s3meta = client.actions.create_file_upload( | ||
app=app, | ||
action=action, | ||
filename=file.name, | ||
mimetype=mimetype, | ||
md5=get_md5(file=file), | ||
) | ||
if not upload(url=s3meta.url, file=file): | ||
raise ComposioSDKError(f"Error uploading file: {file}") | ||
|
||
return cls( | ||
name=file.name, | ||
mimetype=mimetype, | ||
s3key=s3meta.key, | ||
) | ||
|
||
|
||
class FileDownloadable(BaseModel): | ||
model_config = ConfigDict(json_schema_extra={"file_downloadable": True}) | ||
|
||
name: str = Field(..., description="Name of the file") | ||
mimetype: str = Field(..., description="Mime type of the file.") | ||
s3url: str = Field(..., description="URL of the file.") | ||
|
||
def download(self, outdir: Path, chunk_size: int = _DEFAULT_CHUNK_SIZE) -> Path: | ||
outfile = outdir / self.name | ||
outdir.mkdir(exist_ok=True, parents=True) | ||
response = requests.get(url=self.s3url, stream=True) | ||
if response.status_code != 200: | ||
raise ComposioSDKError(f"Error downloading file: {self.s3url}") | ||
|
||
with outfile.open("wb") as fd: | ||
for chunk in response.iter_content(chunk_size=chunk_size): | ||
fd.write(chunk) | ||
return outfile |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Treating HTTP 403 (Forbidden) as a successful upload is incorrect and masks authorization failures. The upload should only return
True
for HTTP 200.📝 Committable Code Suggestion