|
12 | 12 | # Load environment variables from .env file |
13 | 13 | load_dotenv() |
14 | 14 |
|
15 | | -# Get the MongoDB URL from the environment variables |
16 | | -mongo_url = os.getenv("MONGODB_URL") |
| 15 | +token_path = os.path.join(os.path.dirname(__file__), '.dev-token.json') |
17 | 16 | collections = [ |
18 | 17 | { |
19 | 18 | "name": "identities", |
|
72 | 71 | }, |
73 | 72 | ] |
74 | 73 |
|
75 | | -# async def populate_collection(collection_name, db): |
76 | | -# print(f"Populating {collection_name}...") |
77 | | -# file_path = os.path.join(os.path.dirname(__file__), 'seeds', f"{collection_name}.json") |
78 | | -# with open(file_path) as f: |
79 | | -# data = json.load(f) |
80 | | -# for d in data: |
81 | | -# d["_id"] = ObjectId(d["_id"]) |
82 | | -# try: |
83 | | -# result = await db[collection_name].insert_many(data) |
84 | | -# logger.info(f"{len(result.inserted_ids)} {collection_name} inserted") |
85 | | -# except Exception as e: |
86 | | -# logger.warning(f"Duplicate key error: {e}") |
87 | | -# logger.warning(f"Skipping {collection_name}...") |
88 | | - |
89 | 74 | api_endpoint = os.getenv("API_BASE_URL") |
90 | 75 |
|
91 | | -async def populate_collection(collection): |
| 76 | +async def populate_collection(collection, token): |
92 | 77 | logger.info(f"Populating {collection.get('name')}...") |
93 | 78 | file_path = os.path.join(os.path.dirname(__file__), 'seeds', collection.get('file')) |
| 79 | + headers = { |
| 80 | + "Authorization": f"Bearer {token}", "Content-Type": "application/json; charset=utf-8" |
| 81 | + } |
94 | 82 | with open(file_path) as f: |
95 | 83 | datas = json.load(f) |
96 | 84 | for data in datas: |
97 | 85 | try: |
98 | | - response = requests.post(f"{api_endpoint}/{collection.get('endpoint')}", json=data) |
| 86 | + |
| 87 | + response = requests.post(f"{api_endpoint}/{collection.get('endpoint')}", headers=headers, json=data ) |
99 | 88 | response.raise_for_status() |
100 | 89 | logger.info(f"{collection.get('name')} inserted") |
101 | 90 | except Exception as e: |
102 | | - logger.warning(f"Failed to insert {collection.get('name')}: {e}") |
| 91 | + error_message = response.json().get('message') |
| 92 | + logger.warning(f"Failed to insert {collection.get('name')}: {error_message}") |
103 | 93 |
|
104 | 94 | async def main(): |
105 | | - collection_tasks = [populate_collection(col) for col in collections] |
| 95 | + with open(token_path) as f: |
| 96 | + token = json.load(f).get("access_token") |
| 97 | + collection_tasks = [populate_collection(col, token) for col in collections] |
106 | 98 | await asyncio.gather(*collection_tasks) |
107 | 99 |
|
108 | 100 | print("DB populated") |
|
0 commit comments