Skip to content

Commit

Permalink
[Ops] Print responses and errors in a in the invoker to reduce code d…
Browse files Browse the repository at this point in the history
…uplication.
  • Loading branch information
gmarciani committed Nov 30, 2024
1 parent 7a33904 commit 814030d
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 43 deletions.
3 changes: 1 addition & 2 deletions ops/src/yawa_ops/commands/admin/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@
def send_mail(ctx, endpoint, profile, verify_ssl, ca_file, debug, mail_type, attributes):
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
request = SendMailRequest(mailType=mail_type, attributes=string_to_dict(attributes))
response = SendMail(api_client).send_mail(body=request)
print_response(response)
return SendMail(api_client).send_mail(body=request)
9 changes: 3 additions & 6 deletions ops/src/yawa_ops/commands/admin/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@
@click.pass_context
def health(ctx, endpoint, profile, verify_ssl, ca_file, debug):
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
response = Health(api_client).health()
print_response(response)
return Health(api_client).health()


@click.command(help="Describe the server info.", cls=BaseCommand)
@click.pass_context
def info(ctx, endpoint, profile, verify_ssl, ca_file, debug):
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
response = Info(api_client).info()
print_response(response)
return Info(api_client).info()


@click.command(help="Shuts the server down.", cls=BaseCommand)
@click.pass_context
def shutdown(ctx, endpoint, profile, verify_ssl, ca_file, debug):
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
response = Shutdown(api_client).shutdown()
print_response(response)
return Shutdown(api_client).shutdown()
9 changes: 2 additions & 7 deletions ops/src/yawa_ops/commands/auth/login.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import click
import yawac
from yawac.paths.auth_login.post import Login

from yawa_ops.commands.base_command import BaseCommand
Expand Down Expand Up @@ -32,9 +31,5 @@
)
def login(ctx, endpoint, profile, verify_ssl, ca_file, debug, username, password, never_expire):
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
try:
request = LoginRequest(username=username, password=password, neverExpire=never_expire)
response = Login(api_client).login(body=request)
print_response(response)
except yawac.ApiException as e:
log.error("Request failed:\n%s" % e)
request = LoginRequest(username=username, password=password, neverExpire=never_expire)
return Login(api_client).login(body=request)
8 changes: 2 additions & 6 deletions ops/src/yawa_ops/commands/auth/logout.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from yawa_ops.commands.base_command import BaseCommand
from yawa_ops.utils import logutils

from yawa_ops.utils.api import print_response, build_client
from yawa_ops.utils.api import print_response, build_client, print_error

log = logutils.get_logger(__name__)

Expand All @@ -14,8 +14,4 @@
@click.pass_context
def logout(ctx, endpoint, profile, verify_ssl, ca_file, debug):
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
try:
response = Logout(api_client).logout()
print_response(response)
except yawac.ApiException as e:
log.error("Request failed:\n%s" % e)
return Logout(api_client).logout()
7 changes: 4 additions & 3 deletions ops/src/yawa_ops/commands/base_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from yawa_ops.config.profiles import load_profile
from yawa_ops.exceptions.cli_exceptions import ClientError
from yawa_ops.utils import logutils
from yawa_ops.utils.api import print_error, print_response

ENDPOINT_OPTION = click.Option(
("--endpoint",), default=DEFAULT_ENDPOINT, show_default=True, type=str, help="Service endpoint."
Expand Down Expand Up @@ -56,9 +57,9 @@ def invoke(self, ctx: click.Context):
debug=ctx.params.get(DEBUG_OPTION.name)
)
try:
super().invoke(ctx)
response = super().invoke(ctx)
print_response(response)
except yawac.ApiException as e:
log.error("API error: %s" % e)
print(json.dumps(ClientError(e).__dict__, indent=4))
print_error(e)
except RuntimeError as e:
log.error("Unknown error: %s" % e)
3 changes: 1 addition & 2 deletions ops/src/yawa_ops/commands/docs/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@
@click.pass_context
def openapi(ctx, endpoint, profile, verify_ssl, ca_file, debug):
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
response = DocsOpenapi(api_client).get()
print_response(response)
return DocsOpenapi(api_client).get()
3 changes: 1 addition & 2 deletions ops/src/yawa_ops/commands/simple/get_greetings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@
@click.pass_context
def get_greetings(ctx, endpoint, profile, verify_ssl, ca_file, debug):
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
response = GetGreetings(api_client).get_greetings()
print_response(response)
return GetGreetings(api_client).get_greetings()
3 changes: 1 addition & 2 deletions ops/src/yawa_ops/commands/simple/get_outcome.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
)
def get_outcome(ctx, endpoint, profile, verify_ssl, ca_file, debug, outcome):
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
response = GetOutcomeApi(api_client).get_outcome(
return GetOutcomeApi(api_client).get_outcome(
query_params={
"outcome": outcome,
}
)
print_response(response)
6 changes: 2 additions & 4 deletions ops/src/yawa_ops/commands/users/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def activate_user(ctx, endpoint, profile, verify_ssl, ca_file, debug, username,
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
path_params = RequestPathParams(username=username)
body = ActivateUserRequest(token=token)
response = ActivateUser(api_client).activate_user(body=body, path_params=path_params)
print_response(response)
return ActivateUser(api_client).activate_user(body=body, path_params=path_params)


@click.command(help="Send user activation token.", cls=BaseCommand)
Expand All @@ -72,5 +71,4 @@ def activate_user(ctx, endpoint, profile, verify_ssl, ca_file, debug, username,
def send_user_activation_token(ctx, endpoint, profile, verify_ssl, ca_file, debug, username):
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
path_params = RequestPathParams(username=username)
response = SendUserActivationToken(api_client).send_user_activation_token(path_params=path_params)
print_response(response)
return SendUserActivationToken(api_client).send_user_activation_token(path_params=path_params)
6 changes: 2 additions & 4 deletions ops/src/yawa_ops/commands/users/deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def delete_user(ctx, endpoint, profile, verify_ssl, ca_file, debug, username, to
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
path_params = RequestPathParams(username=username)
body = DeleteUserRequest(token=token)
response = DeleteUser(api_client).delete_user(body=body, path_params=path_params)
print_response(response)
return DeleteUser(api_client).delete_user(body=body, path_params=path_params)


@click.command(help="Send user deletion token.", cls=BaseCommand)
Expand All @@ -49,5 +48,4 @@ def delete_user(ctx, endpoint, profile, verify_ssl, ca_file, debug, username, to
def send_user_deletion_token(ctx, endpoint, profile, verify_ssl, ca_file, debug, username):
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
path_params = RequestPathParams(username=username)
response = SendUserDeletionToken(api_client).send_user_deletion_token(path_params=path_params)
print_response(response)
return SendUserDeletionToken(api_client).send_user_deletion_token(path_params=path_params)
6 changes: 2 additions & 4 deletions ops/src/yawa_ops/commands/users/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def reset_password(ctx, endpoint, profile, verify_ssl, ca_file, debug, username,
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
path_params = RequestPathParams(username=username)
body = ResetPasswordRequest(password=password, token=token)
response = ResetPassword(api_client).reset_password(body=body, path_params=path_params)
print_response(response)
return ResetPassword(api_client).reset_password(body=body, path_params=path_params)


@click.command(help="Send password reset token.", cls=BaseCommand)
Expand All @@ -57,5 +56,4 @@ def reset_password(ctx, endpoint, profile, verify_ssl, ca_file, debug, username,
def send_password_reset_token(ctx, endpoint, profile, verify_ssl, ca_file, debug, username):
with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client:
path_params = RequestPathParams(username=username)
response = SendPasswordResetToken(api_client).send_password_reset_token(path_params=path_params)
print_response(response)
return SendPasswordResetToken(api_client).send_password_reset_token(path_params=path_params)
8 changes: 7 additions & 1 deletion ops/src/yawa_ops/utils/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

import yawac
from yawac import api_client

from yawa_ops.config.profiles import Profile
from yawa_ops.utils import logutils
Expand Down Expand Up @@ -45,6 +46,11 @@ def build_client_config(
return configuration


def print_response(api_response):
def print_response(api_response: api_client.ApiResponse):
j = json.loads(api_response.response.data.decode("utf-8"))
print(json.dumps(j, indent=2))


def print_error(error: yawac.ApiException):
j = json.loads(error.body.decode("utf-8"))
print(json.dumps(j, indent=2))

0 comments on commit 814030d

Please sign in to comment.