Skip to content

Commit

Permalink
(#27) pokedex#26: "switch between results and metadata"
Browse files Browse the repository at this point in the history
  • Loading branch information
withtwoemms committed Feb 27, 2024
1 parent 0820412 commit bfd4abf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
16 changes: 14 additions & 2 deletions README.md
Expand Up @@ -47,9 +47,21 @@ poetry run check

### Usage

There exists a `poetry` buildscript called `get-pokemon`.
It can be invoked like so:
There exists a `poetry` console script called `get-pokemon`.
It can be invoked like so (and optionally piped to `less`):

```
poetry run get-pokemon by --type ghost
```

Selection of `ApiRequest` implementation can be done using the `API_REQUEST_IMPL` environment variable:

```
API_REQUEST_IMPL=CACHED poetry run get-pokemon by --move razor-leaf
```

If you'd like to see the _actual_ API records instead of persistence metadata, use the `--view-records` flag:

```
API_REQUEST_IMPL=CACHED poetry run get-pokemon by --move razor-leaf --view-records
```
19 changes: 10 additions & 9 deletions pokedex/api/__main__.py
Expand Up @@ -11,21 +11,22 @@ def cli():


@cli.command(no_args_is_help=True)
@click.option("--type", type=click.STRING, default=False, help="e.g. water, grass, fire")
@click.option("--move", type=click.STRING, default=False, help="e.g. water-gun, razor-leaf, ember")
def by(type: str, move: str):
@click.option("--type", type=click.STRING, help="e.g. water, grass, fire")
@click.option("--move", type=click.STRING, help="e.g. water-gun, razor-leaf, ember")
@click.option("--view-records", is_flag=True, default=False, help="get actual API records if flag present")
def by(type: str, move: str, view_records: bool):
"""
Accepts values by which to fetch Pokemon
TODO: dismiss mutual exclusivity for given options--
i.e. `--type` and `--move` should be usable at the same time
TODO: dismiss mutual exclusivity for given selection options
as they, ultimately, should be usable simultaneously for targeted queries
"""
if type != "False":
results = dict(persist_requests(get_pokemon_by_type(type)))
if type:
results = dict(persist_requests(get_pokemon_by_type(type), view_records=view_records))
print(Report(persisted=results))
return

if move != "False":
results = dict(persist_requests(get_pokemon_by_move(move)))
if move:
results = dict(persist_requests(get_pokemon_by_move(move), view_records=view_records))
print(Report(persisted=results))
return
6 changes: 4 additions & 2 deletions pokedex/db/actions.py
Expand Up @@ -14,6 +14,7 @@ class DbInsertRequestResult(Action):
key: AnyStr
value: DeferredRequest
db: Optional[str] = None
view_records: bool = False

def __post_init__(self):
self.set(name=self.key)
Expand All @@ -22,8 +23,9 @@ def instruction(self) -> bool:
db = self.db or str(CACHEPATH)
response = self.value() # external call
with dbm.open(db, "c") as cache:
cache[self.key] = json.dumps(response.json())
return True
record = response.json()
cache[self.key] = json.dumps(record)
return record if self.view_records else True


@dataclass
Expand Down
4 changes: 2 additions & 2 deletions pokedex/db/client.py
Expand Up @@ -8,8 +8,8 @@
from pokedex.db.actions import DbInsert, DbInsertRequestResult, DbRead


def persist_requests(requests: Iterable[DeferredRequest]):
db_inserts = (DbInsertRequestResult(key=rq.url, value=rq) for rq in requests)
def persist_requests(requests: Iterable[DeferredRequest], view_records=False):
db_inserts = (DbInsertRequestResult(key=rq.url, value=rq, view_records=view_records) for rq in requests)
procedure = KeyedProcedure[str, dict](db_inserts).execute(should_raise=True)
for key, result in procedure:
yield key, result.value
Expand Down
5 changes: 1 addition & 4 deletions pokedex/db/models.py
@@ -1,14 +1,11 @@
import json
from dataclasses import asdict, dataclass
from typing import Any, Dict, List, Type, Union

JSON = Union[Dict[str, Any], List[Any], int, str, float, bool, Type[None]]


@dataclass
class Report:
persisted: dict

def __str__(self) -> JSON:
def __str__(self) -> str:
summary = dict(asdict(self), count=len(self.persisted))
return json.dumps(summary, indent=4)

0 comments on commit bfd4abf

Please sign in to comment.