Coverage for amazonorders/cli.py: 91.30%
46 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-14 16:09 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-14 16:09 +0000
1import datetime
2import os
4import click
6from amazonorders.exception import AmazonOrdersError
7from amazonorders.orders import AmazonOrders
9from amazonorders.session import AmazonSession
11__author__ = "Alex Laird"
12__copyright__ = "Copyright 2024, Alex Laird"
13__version__ = "0.0.4"
16@click.group(invoke_without_command=True)
17@click.option('--username', default=os.environ.get("AMAZON_USERNAME"), help="An Amazon username.")
18@click.option('--password', default=os.environ.get("AMAZON_PASSWORD"), help="An Amazon password.")
19@click.pass_context
20def amazon_orders(ctx, **kwargs):
21 ctx.ensure_object(dict)
22 for key, value in kwargs.items():
23 if value:
24 ctx.obj[key] = value
26 if not kwargs["username"] or not kwargs["password"]:
27 ctx.fail("Must provide --username and --password for Amazon.")
29 ctx.obj["amazon_session"] = AmazonSession(kwargs["username"], kwargs["password"])
32@amazon_orders.command(help="Retrieve Amazon order history for a given year.")
33@click.pass_context
34@click.option('--year', default=datetime.date.today().year,
35 help="The year for which to get order history, defaults to the current year.")
36@click.option('--full-details', is_flag=True, default=False,
37 help="Retrieve the full details for each order in the history.")
38def history(ctx, **kwargs):
39 amazon_session = ctx.obj["amazon_session"]
40 amazon_session.login()
42 amazon_orders = AmazonOrders(amazon_session,
43 print_output=True)
45 try:
46 amazon_orders.get_order_history(year=kwargs["year"],
47 full_details=kwargs["full_details"])
48 except AmazonOrdersError as e:
49 ctx.fail(str(e))
51 amazon_session.close()
54@amazon_orders.command(help="Retrieve the full details for the given Amazon order ID.")
55@click.pass_context
56@click.argument("order_id")
57def order(ctx, order_id):
58 amazon_session = ctx.obj["amazon_session"]
59 amazon_session.login()
61 amazon_orders = AmazonOrders(amazon_session,
62 print_output=True)
64 try:
65 amazon_orders.get_order(order_id)
66 except AmazonOrdersError as e:
67 ctx.fail(str(e))
69 amazon_session.close()
72if __name__ == "__main__":
73 amazon_orders(obj={})