@@ -59,66 +59,6 @@ To run the application locally:
59
59
pip install -r requirements.txt
60
60
```
61
61
62
- 1. Integrate a database:
63
-
64
- ```Python
65
-
66
- from azure.cosmos.aio import CosmosClient
67
- from azure.cosmos import exceptions
68
- from azure.cosmos.partition_key import PartitionKey
69
-
70
- from configs.credential import HOST, MASTER_KEY, DATABASE_ID
71
-
72
-
73
- def get_database_client():
74
- # Initialize the Cosmos client
75
- client = CosmosClient(HOST, MASTER_KEY)
76
-
77
- # Create or get a reference to a database
78
- try:
79
- database = client.create_database_if_not_exists(id=DATABASE_ID)
80
- print(f'Database "{DATABASE_ID}" created or retrieved successfully.')
81
-
82
- except exceptions.CosmosResourceExistsError:
83
- database = client.get_database_client(DATABASE_ID)
84
- print('Database with id \'{0}\' was found'.format(DATABASE_ID))
85
-
86
- return database
87
-
88
-
89
- def get_container_client(container_id):
90
- database = get_database_client()
91
- # Create or get a reference to a container
92
- try:
93
- container = database.create_container(id=container_id, partition_key=PartitionKey(path='/partitionKey'))
94
- print('Container with id \'{0}\' created'.format(container_id))
95
-
96
- except exceptions.CosmosResourceExistsError:
97
- container = database.get_container_client(container_id)
98
- print('Container with id \'{0}\' was found'.format(container_id))
99
-
100
- return container
101
-
102
- async def create_item(container_id, item):
103
- async with CosmosClient(HOST, credential=MASTER_KEY) as client:
104
- database = client.get_database_client(DATABASE_ID)
105
- container = database.get_container_client(container_id)
106
- await container.upsert_item(body=item)
107
-
108
- async def get_items(container_id):
109
- items = []
110
- try:
111
- async with CosmosClient(HOST, credential=MASTER_KEY) as client:
112
- database = client.get_database_client(DATABASE_ID)
113
- container = database.get_container_client(container_id)
114
- async for item in container.read_all_items():
115
- items.append(item)
116
- except Exception as e:
117
- print(f"An error occurred: {e}")
118
-
119
- return items
120
- ```
121
-
122
62
1. Run the app:
123
63
124
64
```Console
0 commit comments