-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from kscalelabs/api-improvements
api improvements
- Loading branch information
Showing
12 changed files
with
228 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
"""Defines the common interface for the K-Scale Python API.""" | ||
|
||
__version__ = "0.0.4" | ||
|
||
from kscale.api import KScale |
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,14 @@ | ||
"""Defines common functionality for the K-Scale API.""" | ||
|
||
from kscale.store.api import StoreAPI | ||
from kscale.utils.api_base import APIBase | ||
|
||
|
||
class KScale( | ||
StoreAPI, | ||
APIBase, | ||
): | ||
"""Defines a common interface for the K-Scale API.""" | ||
|
||
def __init__(self, api_key: str | None = None) -> None: | ||
self.api_key = api_key |
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 |
---|---|---|
@@ -1 +0,0 @@ | ||
__version__ = "0.0.1" | ||
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,20 @@ | ||
"""Defines a common interface for the K-Scale Store API.""" | ||
|
||
from pathlib import Path | ||
|
||
from kscale.store.urdf import download_urdf | ||
from kscale.utils.api_base import APIBase | ||
|
||
|
||
class StoreAPI(APIBase): | ||
def __init__( | ||
self, | ||
*, | ||
api_key: str | None = None, | ||
) -> None: | ||
super().__init__() | ||
|
||
self.api_key = api_key | ||
|
||
async def urdf(self, artifact_id: str) -> Path: | ||
return await download_urdf(artifact_id) |
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,79 @@ | ||
"""Defines a typed client for the K-Scale Store API.""" | ||
|
||
import logging | ||
from types import TracebackType | ||
from typing import Any, Dict, Type | ||
from urllib.parse import urljoin | ||
|
||
import httpx | ||
from pydantic import BaseModel | ||
|
||
from kscale.store.gen.api import ( | ||
NewListingRequest, | ||
NewListingResponse, | ||
SingleArtifactResponse, | ||
UploadArtifactResponse, | ||
) | ||
from kscale.store.utils import API_ROOT, get_api_key | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class KScaleStoreClient: | ||
def __init__(self, base_url: str = API_ROOT) -> None: | ||
self.base_url = base_url | ||
self.client = httpx.AsyncClient( | ||
base_url=self.base_url, | ||
headers={"Authorization": f"Bearer {get_api_key()}"}, | ||
) | ||
|
||
async def _request( | ||
self, | ||
method: str, | ||
endpoint: str, | ||
*, | ||
params: Dict[str, Any] | None = None, | ||
data: BaseModel | None = None, | ||
files: Dict[str, Any] | None = None, | ||
) -> Dict[str, Any]: | ||
url = urljoin(self.base_url, endpoint) | ||
kwargs: Dict[str, Any] = {"params": params} | ||
|
||
if data: | ||
kwargs["json"] = data.dict(exclude_unset=True) | ||
if files: | ||
kwargs["files"] = files | ||
|
||
response = await self.client.request(method, url, **kwargs) | ||
if response.is_error: | ||
logger.error(f"Error response from K-Scale Store: {response.text}") | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
async def get_artifact_info(self, artifact_id: str) -> SingleArtifactResponse: | ||
data = await self._request("GET", f"/artifacts/info/{artifact_id}") | ||
return SingleArtifactResponse(**data) | ||
|
||
async def upload_artifact(self, listing_id: str, file_path: str) -> UploadArtifactResponse: | ||
with open(file_path, "rb") as f: | ||
files = {"files": (f.name, f, "application/gzip")} | ||
data = await self._request("POST", f"/artifacts/upload/{listing_id}", files=files) | ||
return UploadArtifactResponse(**data) | ||
|
||
async def create_listing(self, request: NewListingRequest) -> NewListingResponse: | ||
data = await self._request("POST", "/listings", data=request) | ||
return NewListingResponse(**data) | ||
|
||
async def close(self) -> None: | ||
await self.client.aclose() | ||
|
||
async def __aenter__(self) -> "KScaleStoreClient": | ||
return self | ||
|
||
async def __aexit__( | ||
self, | ||
exc_type: Type[BaseException] | None, | ||
exc_val: BaseException | None, | ||
exc_tb: TracebackType | None, | ||
) -> None: | ||
await self.close() |
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.