|
| 1 | +import argparse |
| 2 | +import logging |
1 | 3 | import sys
|
2 | 4 | from os import getenv
|
3 | 5 | from pprint import pprint
|
4 | 6 |
|
5 | 7 | from azure.cosmos import CosmosClient, PartitionKey
|
6 | 8 | from dotenv import load_dotenv
|
7 | 9 |
|
| 10 | + |
| 11 | +def init_args() -> argparse.Namespace: |
| 12 | + parser = argparse.ArgumentParser( |
| 13 | + prog="cosmosdb", |
| 14 | + description="", |
| 15 | + ) |
| 16 | + parser.add_argument("-i", "--item-id", default="test", help="Item ID") |
| 17 | + parser.add_argument("-p", "--partition-key-path", default="/id", help="Partition key") |
| 18 | + parser.add_argument("-v", "--verbose", action="store_true") |
| 19 | + parser.add_argument("-c", "--command", default="create", help="Command to execute: create, read, delete") |
| 20 | + return parser.parse_args() |
| 21 | + |
| 22 | + |
8 | 23 | if __name__ == "__main__":
|
| 24 | + args = init_args() |
| 25 | + |
| 26 | + # Set verbose mode |
| 27 | + if args.verbose: |
| 28 | + logging.basicConfig(level=logging.DEBUG) |
| 29 | + |
9 | 30 | # Parse .env file and get environment variables
|
10 | 31 | load_dotenv()
|
11 | 32 |
|
12 |
| - # Get item_id from command line arguments |
13 |
| - if len(sys.argv) < 2: |
14 |
| - print("Usage: python main.py <item_id>") |
15 |
| - sys.exit(1) |
16 |
| - item_id = sys.argv[1] |
| 33 | + item_id = args.item_id |
17 | 34 |
|
18 | 35 | # Connect to Azure Cosmos DB
|
19 | 36 | try:
|
20 | 37 | client = CosmosClient.from_connection_string(getenv("AZURE_COSMOS_DB_CONNECTION_STRING"))
|
21 | 38 | database = client.create_database_if_not_exists(id=getenv("AZURE_COSMOS_DB_DATABASE_NAME"))
|
22 | 39 | partition_key_path = PartitionKey(
|
23 |
| - path="/id", |
| 40 | + path=args.partition_key_path, |
24 | 41 | kind="Hash",
|
25 | 42 | )
|
26 | 43 | container = database.create_container_if_not_exists(
|
|
31 | 48 | print(f"Failed to connect to Azure Cosmos DB: {e}")
|
32 | 49 | sys.exit(1)
|
33 | 50 |
|
34 |
| - # Create and read an item |
35 |
| - try: |
36 |
| - response = container.create_item( |
37 |
| - body={ |
38 |
| - "id": item_id, |
39 |
| - "role": "assistant", |
40 |
| - "content": "Hello, world!", |
41 |
| - } |
42 |
| - ) |
43 |
| - print("Created item:") |
44 |
| - pprint(response) |
45 |
| - response = container.read_item( |
46 |
| - item=item_id, |
47 |
| - partition_key=item_id, |
48 |
| - ) |
| 51 | + if args.command == "create": |
| 52 | + # Create and read an item |
| 53 | + print("Create item") |
| 54 | + try: |
| 55 | + response = container.create_item( |
| 56 | + body={ |
| 57 | + "id": item_id, |
| 58 | + "role": "assistant", |
| 59 | + "content": "Hello, world!", |
| 60 | + } |
| 61 | + ) |
| 62 | + pprint(response) |
| 63 | + except Exception as e: |
| 64 | + print(f"Failed to create item: {e}") |
| 65 | + sys.exit(1) |
| 66 | + elif args.command == "read": |
| 67 | + # Read an item |
49 | 68 | print("Read item:")
|
50 |
| - pprint(response) |
| 69 | + try: |
| 70 | + response = container.read_item( |
| 71 | + item=item_id, |
| 72 | + partition_key=item_id, |
| 73 | + ) |
| 74 | + pprint(response) |
51 | 75 |
|
52 |
| - except Exception as e: |
53 |
| - print(f"Failed to create item: {e}") |
| 76 | + except Exception as e: |
| 77 | + print(f"Failed to read item: {e}") |
| 78 | + sys.exit(1) |
| 79 | + elif args.command == "delete": |
| 80 | + # Delete an item |
| 81 | + print("Delete item:") |
| 82 | + try: |
| 83 | + container.delete_item( |
| 84 | + item=item_id, |
| 85 | + partition_key=item_id, |
| 86 | + ) |
| 87 | + except Exception as e: |
| 88 | + print(f"Failed to delete item: {e}") |
| 89 | + sys.exit(1) |
| 90 | + else: |
| 91 | + print(f"Invalid command {args.command}") |
54 | 92 | sys.exit(1)
|
0 commit comments