Skip to content

Commit 692b22b

Browse files
add token + fix entities
1 parent f8bcd6e commit 692b22b

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

populate/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.dev-token.json
2+
.env
3+
!.env.example
4+
```

populate/populate_data.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
# Load environment variables from .env file
1313
load_dotenv()
1414

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')
1716
collections = [
1817
{
1918
"name": "identities",
@@ -72,37 +71,30 @@
7271
},
7372
]
7473

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-
8974
api_endpoint = os.getenv("API_BASE_URL")
9075

91-
async def populate_collection(collection):
76+
async def populate_collection(collection, token):
9277
logger.info(f"Populating {collection.get('name')}...")
9378
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+
}
9482
with open(file_path) as f:
9583
datas = json.load(f)
9684
for data in datas:
9785
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 )
9988
response.raise_for_status()
10089
logger.info(f"{collection.get('name')} inserted")
10190
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}")
10393

10494
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]
10698
await asyncio.gather(*collection_tasks)
10799

108100
print("DB populated")

populate/seeds/entities.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
{
1414
"id": "5f84f96c8f3ac2082347f251",
1515
"type": 3,
16-
"phoneNumber": "0329292929",
16+
"phoneNumber": "+33329292929",
1717
"description": "Work Phone"
1818
},
1919
{
2020
"id": "5f84f96c8f3ac2082347f252",
2121
"type": 4,
22-
"phoneNumber": "0329292928",
22+
"phoneNumber": "+33329292928",
2323
"description": "Personal Phone"
2424
}
2525
]

0 commit comments

Comments
 (0)