Skip to content
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

Add download by filename endpoint to Python #1017

Merged
merged 6 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/python-beta/src/bailo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

__version__ = "0.4.1"
__version__ = "0.4.3"

from bailo.core.agent import Agent, PkiAgent, TokenAgent
from bailo.core.client import Client
Expand Down
31 changes: 30 additions & 1 deletion lib/python-beta/src/bailo/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def get_download_file(
file_id: str,
buffer: BytesIO,
):
"""Download a specific file.
"""Download a specific file by it's id.

:param model_id: Unique model ID
:param file_id: Unique file ID
Expand All @@ -314,6 +314,35 @@ def get_download_file(
shutil.copyfileobj(req.raw, buffer)
return file_id

def get_download_by_filename(
self,
model_id: str,
semver: str,
filename: str,
buffer: BytesIO,
):
"""Download a specific file.

:param model_id: Unique model ID
:param semver: Semver of the release
:param filename: The filename trying to download from
:param buffer: BytesIO object for bailo to write to
:return: The filename
"""
if isinstance(self.agent, TokenAgent):
req = self.agent.get(
f"{self.url}/v2/token/model/{model_id}/release/{semver}/file/{filename}/download",
stream=True,
timeout=10_000,
)
else:
req = self.agent.get(
f"{self.url}/v2/model/{model_id}/release/{semver}/file/{filename}/download", stream=True, timeout=10_000
)

shutil.copyfileobj(req.raw, buffer)
return filename

def simple_upload(self, model_id: str, name: str, buffer: BytesIO):
"""Create a simple file upload.

Expand Down
8 changes: 4 additions & 4 deletions lib/python-beta/src/bailo/helper/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,16 @@ def from_version(cls, client: Client, model_id: str, version: Version | str) ->
draft,
)

def download(self, file_id: str, buffer: BytesIO) -> str:
def download(self, filename: str, buffer: BytesIO) -> str:
"""Give returns a Reading object given the file id.

:param file_name: The name of the file to retrieve
:param filename: The name of the file to retrieve
:param buffer: A BytesIO object
:return: A JSON response object
"""
return self.client.get_download_file(self.model_id, file_id, buffer)
return self.client.get_download_by_filename(self.model_id, str(self.version), filename, buffer)

def upload(self, name: str, file: BytesIO) -> Any:
def upload(self, name: str, file: BytesIO) -> str:
"""Upload files in a given directory to the release.

:param name: The name of the file to upload to bailo
Expand Down
8 changes: 4 additions & 4 deletions lib/python-beta/tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from io import BytesIO

import pytest
from bailo.core.exceptions import ResponseException
from bailo.core.exceptions import BailoException


@pytest.mark.integration
Expand All @@ -14,10 +14,10 @@ def test_file_upload(example_model):
example_release = example_model.create_release("0.1.0", "test")

with file as file:
file_id = example_release.upload("test", file)
example_release.upload("test", file)

download_file = BytesIO()
example_release.download(file_id, download_file)
example_release.download("test", download_file)

# Check that file uploaded has the same contents as the one downloaded
download_file.seek(0)
Expand All @@ -28,5 +28,5 @@ def test_file_upload(example_model):
def test_source_target_doesnt_exist(example_model):
download_file = BytesIO()
example_release = example_model.create_release("0.1.0", "test")
with pytest.raises(ResponseException):
with pytest.raises(BailoException):
example_release.download("non_existant_model", download_file)