This commit is contained in:
Waylon Walker 2023-06-09 16:04:58 -05:00
parent 1a0bf1adb9
commit c3db85a209
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
21 changed files with 647 additions and 658 deletions

View file

@ -2,13 +2,11 @@ import sys
from typing import List, Optional, Union
from engorgio import engorgio
import httpx
from rich.console import Console
import typer
from learn_sql_model.config import Config, get_config
from learn_sql_model.config import get_config
from learn_sql_model.factories.hero import HeroFactory
from learn_sql_model.factories.pet import PetFactory
from learn_sql_model.models.hero import (
Hero,
HeroCreate,
@ -19,6 +17,8 @@ from learn_sql_model.models.hero import (
hero_app = typer.Typer()
config = get_config()
@hero_app.callback()
def hero():
@ -28,12 +28,10 @@ def hero():
@hero_app.command()
@engorgio(typer=True)
def get(
id: Optional[int] = typer.Argument(default=None),
config: Config = None,
hero_id: Optional[int] = typer.Argument(default=None),
) -> Union[Hero, List[Hero]]:
"get one hero"
config.init()
hero = HeroRead.get(id=id, config=config)
hero = HeroRead.get(id=hero_id)
Console().print(hero)
return hero
@ -42,12 +40,11 @@ def get(
@engorgio(typer=True)
def list(
where: Optional[str] = None,
config: Config = None,
offset: int = 0,
limit: Optional[int] = None,
) -> Union[Hero, List[Hero]]:
"get one hero"
hero = HeroRead.list(config=config, where=where, offset=offset, limit=limit)
"list many heros"
hero = HeroRead.list(where=where, offset=offset, limit=limit)
Console().print(hero)
return hero
@ -56,65 +53,39 @@ def list(
@engorgio(typer=True)
def create(
hero: HeroCreate,
config: Config = None,
) -> Hero:
"read all the heros"
r = httpx.post(
f"{config.api_client.url}/hero/",
json=hero.dict(),
)
if r.status_code != 200:
raise RuntimeError(f"{r.status_code}:\n {r.text}")
# hero = hero.post(config=config)
# Console().print(hero)
# return hero
"create one hero"
hero.post()
@hero_app.command()
@engorgio(typer=True)
def update(
hero: HeroUpdate,
config: Config = None,
) -> Hero:
"read all the heros"
r = httpx.patch(
f"{config.api_client.url}/hero/",
json=hero.dict(),
)
if r.status_code != 200:
raise RuntimeError(f"{r.status_code}:\n {r.text}")
"update one hero"
hero.update()
@hero_app.command()
@engorgio(typer=True)
def delete(
hero: HeroDelete,
config: Config = None,
) -> Hero:
"read all the heros"
r = httpx.delete(
f"{config.api_client.url}/hero/{hero.id}",
)
if r.status_code != 200:
raise RuntimeError(f"{r.status_code}:\n {r.text}")
"delete a hero by id"
hero.delete()
@hero_app.command()
@engorgio(typer=True)
def populate(
hero: Hero,
n: int = 10,
) -> Hero:
"read all the heros"
config = get_config()
"Create n number of heros"
if config.env == "prod":
Console().print("populate is not supported in production")
sys.exit(1)
for hero in HeroFactory().batch(n):
pet = PetFactory().build()
hero.pet = pet
Console().print(hero)
hero.post(config=config)
hero = HeroCreate(**hero.dict())
hero.post()