-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose ds uploader and upload tasks (#142)
Signed-off-by: Michele Dolfi <[email protected]> Signed-off-by: Panos Vagenas <[email protected]> Co-authored-by: Panos Vagenas <[email protected]>
- Loading branch information
1 parent
b47fbe1
commit a00fc03
Showing
5 changed files
with
119 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
from typing import TYPE_CHECKING, Union | ||
|
||
import requests | ||
from pydantic import BaseModel | ||
|
||
from deepsearch.cps.apis import public as sw_client | ||
from deepsearch.cps.apis.public.models.temporary_upload_file_result import ( | ||
TemporaryUploadFileResult, | ||
) | ||
from deepsearch.cps.client.components.projects import Project | ||
|
||
if TYPE_CHECKING: | ||
from deepsearch.cps.client import CpsApi | ||
|
||
|
||
class UploadedFile(BaseModel): | ||
download_url: str | ||
internal_url: str | ||
|
||
|
||
class DSApiUploader: | ||
def __init__(self, api: CpsApi) -> None: | ||
self.api = api | ||
self.upload_api = sw_client.UploadsApi(self.api.client.swagger_client) | ||
|
||
def upload_file( | ||
self, | ||
project: Union[Project, str], | ||
source_path: Union[Path, str], | ||
tls_verify: bool = True, | ||
) -> UploadedFile: | ||
""" | ||
Upload a file to the scratch storage of Deep Search. | ||
The returned object provides the `download_url` and `internal_url` which can be | ||
use for retrieving the file or submitting to other Deep Search APIs, respectively. | ||
""" | ||
|
||
proj_key = project.key if isinstance(project, Project) else project | ||
source_path = Path(source_path) | ||
|
||
# Register file | ||
source_basename = source_path.name | ||
scratch_specs: TemporaryUploadFileResult = ( | ||
self.upload_api.create_project_scratch_file( | ||
proj_key=proj_key, filename=source_basename | ||
) | ||
) | ||
|
||
# Upload file | ||
upload_specs = scratch_specs.upload | ||
with source_path.open("rb") as f: | ||
files = {"file": (source_basename, f)} | ||
request_upload = requests.post( | ||
url=upload_specs.url, | ||
data=upload_specs.fields, | ||
files=files, | ||
verify=tls_verify, | ||
) | ||
request_upload.raise_for_status() | ||
|
||
return UploadedFile( | ||
download_url=scratch_specs.download.url, | ||
internal_url=scratch_specs.download_private.url, | ||
) |
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