From a9e82b996e04eae4420a4b0ecef0ab68f48872e4 Mon Sep 17 00:00:00 2001 From: Dev Mukherjee Date: Thu, 7 Dec 2023 09:57:25 +1100 Subject: [PATCH] feat: basic prototype of cli working 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 --- gallagher/cli/__init__.py | 8 -------- gallagher/cli/cardholders.py | 36 +++++++++++++++++++++++++++++++++++- gallagher/dto/cardholder.py | 25 +++++++++++++++++++++++++ tests/__init__.py | 3 ++- 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/gallagher/cli/__init__.py b/gallagher/cli/__init__.py index 269bb551..ddcd0b2e 100644 --- a/gallagher/cli/__init__.py +++ b/gallagher/cli/__init__.py @@ -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 diff --git a/gallagher/cli/cardholders.py b/gallagher/cli/cardholders.py index 828d6073..d12e28e2 100644 --- a/gallagher/cli/cardholders.py +++ b/gallagher/cli/cardholders.py @@ -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}") diff --git a/gallagher/dto/cardholder.py b/gallagher/dto/cardholder.py index b15763e5..2b99a645 100644 --- a/gallagher/dto/cardholder.py +++ b/gallagher/dto/cardholder.py @@ -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 @@ -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" diff --git a/tests/__init__.py b/tests/__init__.py index 62bb40b8..dc5d6ae6 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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. @@ -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