Skip to content

Commit

Permalink
Merge pull request #61 from gizatechxyz/feature/add-json-output
Browse files Browse the repository at this point in the history
Add Tables and Json output
  • Loading branch information
Gonmeso authored Jun 24, 2024
2 parents 90d98d7 + ec42795 commit a145ecc
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 44 deletions.
25 changes: 20 additions & 5 deletions giza/cli/commands/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import List, Optional

import typer
from rich import print_json
from rich.console import Console
from rich.table import Table

Expand All @@ -14,6 +13,7 @@
DEBUG_OPTION,
DESCRIPTION_OPTION,
ENDPOINT_OPTION,
JSON_OPTION,
MODEL_OPTION,
NAME_OPTION,
VERSION_OPTION,
Expand Down Expand Up @@ -44,8 +44,13 @@ def create(
endpoint_id: int = ENDPOINT_OPTION,
name: Optional[str] = NAME_OPTION,
description: Optional[str] = DESCRIPTION_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:

if json:
echo.set_log_file()

echo("Creating agent ✅ ")

if not model_id and not version_id and not endpoint_id:
Expand Down Expand Up @@ -104,7 +109,7 @@ def create(
},
)
agent = client.create(agent_create)
print_json(agent.model_dump_json())
echo.print_model(agent)


@app.command(
Expand All @@ -123,8 +128,12 @@ def list(
parameters: Optional[List[str]] = typer.Option(
None, "--parameters", "-p", help="The parameters of the agent"
),
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()

echo("Listing agents ✅ ")
with ExceptionHandler(debug=debug):
client = AgentsClient(API_HOST)
Expand All @@ -135,7 +144,7 @@ def list(
else:
query_params = None
agents: AgentList = client.list(params=query_params)
print_json(agents.model_dump_json())
echo.print_model(agents)


# giza/commands/deployments.py
Expand All @@ -149,13 +158,16 @@ def list(
)
def get(
agent_id: int = AGENT_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo(f"Getting agent {agent_id} ✅ ")
with ExceptionHandler(debug=debug):
client = AgentsClient(API_HOST)
deployment = client.get(agent_id)
print_json(deployment.model_dump_json())
echo.print_model(deployment)


@app.command(
Expand Down Expand Up @@ -196,8 +208,11 @@ def update(
parameters: Optional[List[str]] = typer.Option(
None, "--parameters", "-p", help="The parameters of the agent"
),
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo(f"Updating agent {agent_id} ✅ ")
with ExceptionHandler(debug=debug):
client = AgentsClient(API_HOST)
Expand All @@ -206,4 +221,4 @@ def update(
name=name, description=description, parameters=update_params
)
agent = client.patch(agent_id, agent_update)
print_json(agent.model_dump_json())
echo.print_model(agent)
32 changes: 25 additions & 7 deletions giza/cli/commands/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import typer
from pydantic import ValidationError
from requests import HTTPError
from rich import print_json

from giza.cli import API_HOST
from giza.cli.client import EndpointsClient
Expand All @@ -13,6 +12,7 @@
DEBUG_OPTION,
ENDPOINT_OPTION,
FRAMEWORK_OPTION,
JSON_OPTION,
MODEL_OPTION,
VERSION_OPTION,
)
Expand Down Expand Up @@ -77,8 +77,11 @@ def list(
only_active: bool = typer.Option(
False, "--only-active", "-a", help="Only list active endpoints"
),
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo("Listing endpoints ✅ ")
params = {}
try:
Expand Down Expand Up @@ -113,7 +116,7 @@ def list(
if debug:
raise e
sys.exit(1)
print_json(deployments.model_dump_json())
echo.print_model(deployments)


# giza/commands/deployments.py
Expand All @@ -127,8 +130,11 @@ def list(
)
def get(
endpoint_id: int = ENDPOINT_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo(f"Getting endpoint {endpoint_id} ✅ ")
try:
client = EndpointsClient(API_HOST)
Expand Down Expand Up @@ -156,7 +162,7 @@ def get(
if debug:
raise e
sys.exit(1)
print_json(deployment.model_dump_json())
echo.print_model(deployment)


@app.command(
Expand Down Expand Up @@ -191,8 +197,11 @@ def delete_endpoint(
)
def list_proofs(
endpoint_id: int = ENDPOINT_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo(f"Getting proofs from endpoint {endpoint_id} ✅ ")
try:
client = EndpointsClient(API_HOST)
Expand Down Expand Up @@ -220,7 +229,7 @@ def list_proofs(
if debug:
raise e
sys.exit(1)
print_json(proofs.model_dump_json(exclude_unset=True))
echo.print_model(proofs)


@app.command(
Expand All @@ -237,8 +246,11 @@ def get_proof(
proof_id: str = typer.Option(
None, "--proof-id", "-p", help="The ID or request id of the proof"
),
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo(f"Getting proof from endpoint {endpoint_id} ✅ ")
try:
client = EndpointsClient(API_HOST)
Expand Down Expand Up @@ -266,7 +278,7 @@ def get_proof(
if debug:
raise e
sys.exit(1)
print_json(proof.model_dump_json(exclude_unset=True))
echo.print_model(proof)


@app.command(
Expand Down Expand Up @@ -334,13 +346,16 @@ def download_proof(
)
def list_jobs(
endpoint_id: int = ENDPOINT_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo(f"Getting jobs from endpoint {endpoint_id} ✅ ")
with ExceptionHandler(debug=debug):
client = EndpointsClient(API_HOST)
jobs = client.list_jobs(endpoint_id)
print_json(jobs.json(exclude_unset=True))
echo.print_model(jobs)


@app.command(
Expand All @@ -357,13 +372,16 @@ def verify(
proof_id: str = typer.Option(
None, "--proof-id", "-p", help="The ID or request id of the proof"
),
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
if json:
echo.set_log_file()
echo(f"Verifying proof from endpoint {endpoint_id} ✅ ")
with ExceptionHandler(debug=debug):
client = EndpointsClient(API_HOST)
verification = client.verify_proof(endpoint_id, proof_id)
print_json(verification.model_dump_json(exclude_unset=True))
echo.print_model(verification)


@app.command(
Expand Down
19 changes: 13 additions & 6 deletions giza/cli/commands/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import typer
from pydantic import ValidationError
from requests import HTTPError
from rich import print_json

from giza.cli import API_HOST
from giza.cli.client import ModelsClient
from giza.cli.options import DEBUG_OPTION, DESCRIPTION_OPTION, MODEL_OPTION
from giza.cli.options import DEBUG_OPTION, DESCRIPTION_OPTION, JSON_OPTION, MODEL_OPTION
from giza.cli.schemas.models import ModelCreate
from giza.cli.utils import echo, get_response_info

Expand All @@ -28,6 +27,7 @@
)
def get(
model_id: int = MODEL_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
"""
Expand All @@ -41,6 +41,8 @@ def get(
ValidationError: input fields are validated, if these are not suitable the exception is raised
HTTPError: request error to the API, 4XX or 5XX
"""
if json:
echo.set_log_file()
echo("Retrieving model information ✅ ")
try:
client = ModelsClient(API_HOST)
Expand Down Expand Up @@ -68,7 +70,7 @@ def get(
if debug:
raise e
sys.exit(1)
print_json(model.model_dump_json())
echo.print_model(model)


@app.command(
Expand All @@ -82,6 +84,7 @@ def get(
""",
)
def list(
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
"""
Expand All @@ -94,7 +97,8 @@ def list(
ValidationError: input fields are validated, if these are not suitable the exception is raised
HTTPError: request error to the API, 4XX or 5XX
"""

if json:
echo.set_log_file()
echo("Listing models ✅ ")
try:
client = ModelsClient(API_HOST)
Expand Down Expand Up @@ -122,7 +126,7 @@ def list(
if debug:
raise e
sys.exit(1)
print_json(models.model_dump_json())
echo.print_model(models)


@app.command(
Expand All @@ -139,6 +143,7 @@ def create(
..., "--name", "-n", help="Name of the model to be created"
),
description: str = DESCRIPTION_OPTION,
json: Optional[bool] = JSON_OPTION,
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
"""
Expand All @@ -152,6 +157,8 @@ def create(
ValidationError: input fields are validated, if these are not suitable the exception is raised
HTTPError: request error to the API, 4XX or 5XX
"""
if json:
echo.set_log_file()
if name is None or name == "":
echo.error("Name is required")
sys.exit(1)
Expand Down Expand Up @@ -182,4 +189,4 @@ def create(
if debug:
raise e
sys.exit(1)
print_json(model.model_dump_json())
echo.print_model(model)
13 changes: 9 additions & 4 deletions giza/cli/commands/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
from email_validator import EmailNotValidError, validate_email
from pydantic import SecretStr, ValidationError
from requests import HTTPError
from rich import print_json

from giza.cli import API_HOST
from giza.cli.client import UsersClient
from giza.cli.exceptions import PasswordError
from giza.cli.options import DEBUG_OPTION
from giza.cli.options import DEBUG_OPTION, JSON_OPTION
from giza.cli.schemas import users
from giza.cli.utils import echo, get_response_info
from giza.cli.utils.misc import _check_password_strength
Expand Down Expand Up @@ -200,18 +199,24 @@ def create_api_key(
Verification and an active token is needed.
""",
)
def me(debug: Optional[bool] = DEBUG_OPTION) -> None:
def me(
debug: Optional[bool] = DEBUG_OPTION, json: Optional[bool] = JSON_OPTION
) -> None:
"""
Retrieve information about the current user and print it as json to stdout.
Args:
debug (Optional[bool], optional): Whether to add debug information, will show requests, extra logs and traceback if there is an Exception. Defaults to DEBUG_OPTION (False)
"""

if json:
echo.set_log_file()

echo("Retrieving information about me!")
client = UsersClient(API_HOST, debug=debug)
user = client.me()

print_json(user.model_dump_json())
echo.print_model(user)


@app.command(
Expand Down
Loading

0 comments on commit a145ecc

Please sign in to comment.