Skip to content

Commit

Permalink
expand api
Browse files Browse the repository at this point in the history
  • Loading branch information
codekansas committed Sep 4, 2024
1 parent d8d08e6 commit 9f8ad22
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
6 changes: 5 additions & 1 deletion kscale/store/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from pathlib import Path

from kscale.store.urdf import download_urdf
from kscale.store.gen.api import UploadArtifactResponse
from kscale.store.urdf import download_urdf, upload_urdf
from kscale.utils.api_base import APIBase


Expand All @@ -18,3 +19,6 @@ def __init__(

async def urdf(self, artifact_id: str) -> Path:
return await download_urdf(artifact_id)

async def upload_urdf(self, listing_id: str, file_path: str) -> UploadArtifactResponse:
return await upload_urdf(listing_id, file_path)
4 changes: 3 additions & 1 deletion kscale/store/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Defines a typed client for the K-Scale Store API."""

import logging
from pathlib import Path
from types import TracebackType
from typing import Any, Dict, Type
from urllib.parse import urljoin
Expand Down Expand Up @@ -55,8 +56,9 @@ async def get_artifact_info(self, artifact_id: str) -> SingleArtifactResponse:
return SingleArtifactResponse(**data)

async def upload_artifact(self, listing_id: str, file_path: str) -> UploadArtifactResponse:
file_name = Path(file_path).name
with open(file_path, "rb") as f:
files = {"files": (f.name, f, "application/gzip")}
files = {"files": (file_name, f, "application/gzip")}
data = await self._request("POST", f"/artifacts/upload/{listing_id}", files=files)
return UploadArtifactResponse(**data)

Expand Down
22 changes: 14 additions & 8 deletions kscale/store/urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from kscale.conf import Settings
from kscale.store.client import KScaleStoreClient
from kscale.store.gen.api import SingleArtifactResponse
from kscale.store.gen.api import SingleArtifactResponse, UploadArtifactResponse
from kscale.store.utils import get_api_key

# Set up logging
Expand Down Expand Up @@ -128,18 +128,24 @@ async def remove_local_urdf(artifact_id: str) -> None:
raise


async def upload_urdf(listing_id: str, args: Sequence[str]) -> None:
parser = argparse.ArgumentParser(description="K-Scale URDF Store", add_help=False)
parser.add_argument("root_dir", type=Path, help="The path to the root directory to upload")
parsed_args = parser.parse_args(args)

root_dir = parsed_args.root_dir
async def upload_urdf(listing_id: str, root_dir: Path) -> UploadArtifactResponse:
tarball_path = create_tarball(root_dir, "robot.tgz", get_artifact_dir(listing_id))

async with KScaleStoreClient() as client:
response = await client.upload_artifact(listing_id, str(tarball_path))

logger.info("Uploaded artifacts: %s", [artifact.artifact_id for artifact in response.artifacts])
return response


async def upload_urdf_cli(listing_id: str, args: Sequence[str]) -> UploadArtifactResponse:
parser = argparse.ArgumentParser(description="K-Scale URDF Store", add_help=False)
parser.add_argument("root_dir", type=Path, help="The path to the root directory to upload")
parsed_args = parser.parse_args(args)

root_dir = parsed_args.root_dir
response = await upload_urdf(listing_id, root_dir)
return response


Command = Literal["download", "info", "upload", "remove-local"]
Expand All @@ -165,7 +171,7 @@ async def main(args: Sequence[str] | None = None) -> None:
await remove_local_urdf(id)

case "upload":
await upload_urdf(id, remaining_args)
await upload_urdf_cli(id, remaining_args)

case _:
logger.error("Invalid command")
Expand Down

0 comments on commit 9f8ad22

Please sign in to comment.