Skip to content

Commit

Permalink
Merge pull request #9 from kscalelabs/fix-urdf-upload-flow
Browse files Browse the repository at this point in the history
fix urdf upload flow
  • Loading branch information
codekansas authored Sep 4, 2024
2 parents a909297 + b5ff13f commit e2d1b0b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 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, root_dir: Path) -> UploadArtifactResponse:
return await upload_urdf(listing_id, root_dir)
8 changes: 5 additions & 3 deletions 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 All @@ -14,13 +15,13 @@
SingleArtifactResponse,
UploadArtifactResponse,
)
from kscale.store.utils import API_ROOT, get_api_key
from kscale.store.utils import get_api_key, get_api_root

logger = logging.getLogger(__name__)


class KScaleStoreClient:
def __init__(self, base_url: str = API_ROOT) -> None:
def __init__(self, base_url: str = get_api_root()) -> None:
self.base_url = base_url
self.client = httpx.AsyncClient(
base_url=self.base_url,
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
16 changes: 15 additions & 1 deletion kscale/store/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@

from kscale.conf import Settings

API_ROOT = "https://api.kscale.store"

def get_api_root() -> str:
"""Returns the base URL for the K-Scale Store API.
This can be overridden when targetting a different server.
Returns:
The base URL for the K-Scale Store API.
"""
return os.getenv("KSCALE_API_ROOT", "https://api.kscale.store")


def get_api_key() -> str:
"""Returns the API key for the K-Scale Store API.
Returns:
The API key for the K-Scale Store API.
"""
api_key = Settings.load().store.api_key
if api_key is None:
api_key = os.getenv("KSCALE_API_KEY")
Expand Down

0 comments on commit e2d1b0b

Please sign in to comment.