Skip to content

Commit

Permalink
feat: basic prototype of cli working
Browse files Browse the repository at this point in the history
subcommands have been setup in accordance to the documentation with typer
routing the cardholder commands and then fetching the data via the client
rich is rendering tables to the terminal

REFS #13
  • Loading branch information
devraj committed Dec 6, 2023
1 parent fa32907 commit a9e82b9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
8 changes: 0 additions & 8 deletions gallagher/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@
app.add_typer(cardholders_app, name="ch")


@app.command()
def echo(name: str):
"""
"""
typer.echo("Hello World")


if __name__ == "__main__":
""" In case you are invoking this via Python directly
Expand Down
36 changes: 35 additions & 1 deletion gallagher/cli/cardholders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,39 @@
"""
import typer
from rich.console import Console
from rich.table import Table

app = typer.Typer()
from gallagher.cc.cardholders.cardholders import Cardholder

app = typer.Typer(help="query or manage cardholders")


@app.command("summary")
def summary():
""" list all cardholders
"""
import os
api_key = os.environ.get("GACC_API_KEY")

from gallagher import cc
cc.api_key = api_key

cardholders = Cardholder.list()

table = Table(title="Cardholders")
for header in cardholders.cli_header:
table.add_column(header)

for row in cardholders.cli_repr:
table.add_row(*row)

console = Console()
console.print(table)


@app.command("get")
def get(id: int):
""" get a cardholder by id
"""
typer.echo(f"Getting cardholder {id}")
25 changes: 25 additions & 0 deletions gallagher/dto/cardholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ class CardholderSummary(
description: Optional[str] = None
authorised: bool

def cli_repr(self):
return [
self.first_name,
self.last_name,
"yes" if self.authorised else "no"
]

def __str__(self):
return f"{self.id} {self.first_name} {self.last_name}"


class CardholderDetail(
CardholderSummary
Expand Down Expand Up @@ -63,3 +73,18 @@ class CardholderSummaryResponse(
"""
results: list[CardholderSummary]

@property
def cli_header(self):
return [
"First name",
"Last name",
"Authorised"
]

@property
def cli_repr(self):
return [x.cli_repr() for x in self.results]

def __str__(self):
return f"{len(self.results)} cardholders"
3 changes: 2 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""


def setup_module(module):
""" The Gallagher API client requires a test key, this is
set in the environment variable GACC_API_KEY.
Expand All @@ -14,7 +15,7 @@ def setup_module(module):
"""
import os
api_key = os.environ.get("GACC_API_KEY")

from gallagher import cc
cc.api_key = api_key

Expand Down

0 comments on commit a9e82b9

Please sign in to comment.