Coverage for amazonorders/cli.py: 88.68%

53 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-16 15:31 +0000

1import datetime 

2import logging 

3import os 

4 

5import click 

6 

7from amazonorders.exception import AmazonOrdersError 

8from amazonorders.orders import AmazonOrders 

9 

10from amazonorders.session import AmazonSession 

11 

12__author__ = "Alex Laird" 

13__copyright__ = "Copyright 2024, Alex Laird" 

14__version__ = "0.0.5" 

15 

16logger = logging.getLogger("amazonorders") 

17 

18 

19@click.group(invoke_without_command=True) 

20@click.option('--username', default=os.environ.get("AMAZON_USERNAME"), help="An Amazon username.") 

21@click.option('--password', default=os.environ.get("AMAZON_PASSWORD"), help="An Amazon password.") 

22@click.option('--debug', is_flag=True, default=False, help="Enable debugging and send output to command line.") 

23@click.pass_context 

24def amazon_orders_cli(ctx, **kwargs): 

25 ctx.ensure_object(dict) 

26 for key, value in kwargs.items(): 

27 if value: 

28 ctx.obj[key] = value 

29 

30 if not kwargs["username"] or not kwargs["password"]: 

31 ctx.fail("Must provide --username and --password for Amazon.") 

32 

33 if kwargs["debug"]: 

34 logger.setLevel(logging.DEBUG) 

35 logger.addHandler(logging.StreamHandler()) 

36 

37 ctx.obj["amazon_session"] = AmazonSession(kwargs["username"], 

38 kwargs["password"], 

39 debug=kwargs["debug"]) 

40 

41 

42@amazon_orders_cli.command(help="Retrieve Amazon order history for a given year.") 

43@click.pass_context 

44@click.option('--year', default=datetime.date.today().year, 

45 help="The year for which to get order history, defaults to the current year.") 

46@click.option('--start-index', help="Retrieve the single page of history at the given index.") 

47@click.option('--full-details', is_flag=True, default=False, 

48 help="Retrieve the full details for each order in the history.") 

49def history(ctx, **kwargs): 

50 amazon_session = ctx.obj["amazon_session"] 

51 amazon_session.login() 

52 

53 amazon_orders = AmazonOrders(amazon_session, 

54 debug=amazon_session.debug, 

55 print_output=True) 

56 

57 try: 

58 amazon_orders.get_order_history(year=kwargs["year"], 

59 start_index=kwargs["start_index"], 

60 full_details=kwargs["full_details"]) 

61 except AmazonOrdersError as e: 

62 ctx.fail(str(e)) 

63 

64 amazon_session.close() 

65 

66 

67@amazon_orders_cli.command(help="Retrieve the full details for the given Amazon order ID.") 

68@click.pass_context 

69@click.argument("order_id") 

70def order(ctx, order_id): 

71 amazon_session = ctx.obj["amazon_session"] 

72 amazon_session.login() 

73 

74 amazon_orders = AmazonOrders(amazon_session, 

75 debug=amazon_session.debug, 

76 print_output=True) 

77 

78 try: 

79 amazon_orders.get_order(order_id) 

80 except AmazonOrdersError as e: 

81 ctx.fail(str(e)) 

82 

83 amazon_session.close() 

84 

85 

86if __name__ == "__main__": 

87 amazon_orders_cli(obj={})