Skip to content

Commit

Permalink
feat(apps): adapt OpenAPI listBenchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
zhen.chen committed Nov 26, 2021
1 parent 0017cab commit 86467b7
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions tensorbay/apps/sextant.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

"""Interact with sextant app at graviti marketplace."""

from typing import Any, Dict, List, Optional
from typing import Any, Dict, Generator, List, Optional
from urllib.parse import urljoin

from tensorbay.client.lazy import PagingList
from tensorbay.client.requests import Client
from tensorbay.exception import ResourceNotExistError


class Evaluation:
Expand Down Expand Up @@ -109,7 +111,19 @@ class Sextant(Client):

def __init__(self, access_key: str, url: str = "") -> None:
super().__init__(access_key, url)
self._open_api = urljoin(self.gateway_url, "apps-sextant/v1")
self._open_api = urljoin(self.gateway_url, "apps-sextant/v1/")

def _generate_benmarks(
self, offset: int = 0, limit: int = 128
) -> Generator[Benchmark, None, int]:

params: Dict[str, Any] = {"offset": offset, "limit": limit}
response = self.open_api_do("GET", "benchmarks", "", params=params).json()

for benchmark in response["benchmarks"]:
yield Benchmark(benchmark["name"], benchmark["benchmarkId"], self)

return response["totalCount"] # type: ignore[no-any-return]

def create_benchmark(
self,
Expand All @@ -136,21 +150,30 @@ def create_benchmark(
"""

def list_benchmarks(self) -> List[Benchmark]:
def list_benchmarks(self) -> PagingList[Benchmark]:
"""List all benchmarks.
Return:
Returns:
The list of Benchmark instances.
"""
return PagingList(self._generate_benmarks, 128)

def get_benchmark(self, name: str) -> Benchmark:
"""Get a benchmark instance by name.
Arguments:
name: Name of the Benchmark.
Return:
Returns:
The Benchmark instance with the given name.
Raises:
ResourceNotExistError: When the required benchmark does not exist.
"""
for benchmark in self.list_benchmarks():
if benchmark.name == name:
return benchmark

raise ResourceNotExistError(resource="benchmark", identification=name)

0 comments on commit 86467b7

Please sign in to comment.