Coverage for amazonorders/cli.py: 89.09%
55 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-16 23:55 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-16 23:55 +0000
1import datetime
2import logging
3import os
4from typing import Any
6import click
7from click.core import Context
9from amazonorders.exception import AmazonOrdersError
10from amazonorders.orders import AmazonOrders
11from amazonorders.session import AmazonSession
13__author__ = "Alex Laird"
14__copyright__ = "Copyright 2024, Alex Laird"
15__version__ = "0.0.7"
17logger = logging.getLogger("amazonorders")
20@click.group(invoke_without_command=True)
21@click.option('--username', default=os.environ.get("AMAZON_USERNAME"), help="An Amazon username.")
22@click.option('--password', default=os.environ.get("AMAZON_PASSWORD"), help="An Amazon password.")
23@click.option('--debug', is_flag=True, default=False, help="Enable debugging and send output to command line.")
24@click.pass_context
25def amazon_orders_cli(ctx,
26 **kwargs: Any):
27 ctx.ensure_object(dict)
28 for key, value in kwargs.items():
29 if value:
30 ctx.obj[key] = value
32 if not kwargs["username"] or not kwargs["password"]:
33 ctx.fail("Must provide --username and --password for Amazon.")
35 if kwargs["debug"]:
36 logger.setLevel(logging.DEBUG)
37 logger.addHandler(logging.StreamHandler())
39 ctx.obj["amazon_session"] = AmazonSession(kwargs["username"],
40 kwargs["password"],
41 debug=kwargs["debug"])
44@amazon_orders_cli.command(help="Retrieve Amazon order history for a given year.")
45@click.pass_context
46@click.option('--year', default=datetime.date.today().year,
47 help="The year for which to get order history, defaults to the current year.")
48@click.option('--start-index', help="Retrieve the single page of history at the given index.")
49@click.option('--full-details', is_flag=True, default=False,
50 help="Retrieve the full details for each order in the history.")
51def history(ctx: Context,
52 **kwargs: Any):
53 amazon_session = ctx.obj["amazon_session"]
54 amazon_session.login()
56 amazon_orders = AmazonOrders(amazon_session,
57 debug=amazon_session.debug,
58 print_output=True)
60 try:
61 amazon_orders.get_order_history(year=kwargs["year"],
62 start_index=kwargs["start_index"],
63 full_details=kwargs["full_details"])
64 except AmazonOrdersError as e:
65 ctx.fail(str(e))
67 amazon_session.close()
70@amazon_orders_cli.command(help="Retrieve the full details for the given Amazon order ID.")
71@click.pass_context
72@click.argument("order_id")
73def order(ctx: Context,
74 order_id: str):
75 amazon_session = ctx.obj["amazon_session"]
76 amazon_session.login()
78 amazon_orders = AmazonOrders(amazon_session,
79 debug=amazon_session.debug,
80 print_output=True)
82 try:
83 amazon_orders.get_order(order_id)
84 except AmazonOrdersError as e:
85 ctx.fail(str(e))
87 amazon_session.close()
90if __name__ == "__main__":
91 amazon_orders_cli(obj={})