-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Julamada-GAIA-Project/rdvl/cosmo-to-mongo
Rdvl/cosmo-to-mongo
- Loading branch information
Showing
6 changed files
with
45 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
fastapi | ||
azure-cosmos | ||
pymongo | ||
pydantic | ||
uvicorn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,46 @@ | ||
from fastapi import FastAPI | ||
from azure.cosmos import CosmosClient, exceptions | ||
import json | ||
import random | ||
import os | ||
|
||
## COSMODB CONNECTION ## | ||
# CosmoDB Params | ||
endpoint = os.environ.get("COSMODB_URI") | ||
key = os.environ.get("COSMODB_KEY") | ||
database_name = os.environ.get("COSMODB_NAME") | ||
container_name = os.environ.get("COSMODB_CONTAINER") | ||
|
||
# Cosmo Client | ||
client = CosmosClient(endpoint, key) | ||
from typing import List | ||
from fastapi import FastAPI, HTTPException | ||
from pymongo import MongoClient | ||
|
||
# Obtains BDD or creates it | ||
try: | ||
database = client.get_database_client(database_name) | ||
except exceptions.CosmosResourceNotFoundError: | ||
database = client.create_database_if_not_exists(database_name) | ||
from src.models.Human import Human | ||
|
||
try: | ||
container = database.get_container_client(container_name) | ||
except exceptions.CosmosResourceNotFoundError: | ||
container = database.create_container_if_not_exists(id=container_name) | ||
## MONGODB CONNECTION ## | ||
# MongoDB Params | ||
mongodb_uri = os.getenv('MONGODB_URI') | ||
|
||
# Mongo Client | ||
client = MongoClient(mongodb_uri) | ||
db = client.get_database('gaia-dev') | ||
|
||
# Init | ||
app = FastAPI() | ||
|
||
## ENDPOINTS ## | ||
# Post Human | ||
@app.post("/humans") | ||
async def post_human(data: dict): | ||
response = container.create_item(body=data) | ||
return {"message": "Human Post success", "item_id": response["id"]} | ||
async def post_human(human: Human): | ||
result = db.humans.insert_one(human.dict()) | ||
return {"message": "Human created successfully", "human_id": str(result.inserted_id)} | ||
|
||
# Get Human by id | ||
@app.get("/humans/{human_id}", response_model=Human) | ||
async def get_human_by_id(human_id: str): | ||
human_data = db['humans'].find_one({"_id": human_id}) | ||
if human_data: | ||
return Human(**human_data) | ||
else: | ||
raise HTTPException(status_code=404, detail="Human not found") | ||
|
||
@app.post("/hello") | ||
async def post_human(data: dict): | ||
return {"message": "Hello world!"} | ||
# Get All humans | ||
@app.get("/humans", response_model=List[Human]) | ||
async def get_all_humans(): | ||
humans = [] | ||
cursor = db['humans'].find() | ||
for human_data in cursor: | ||
human = Human(**human_data) | ||
humans.append(human) | ||
return humans |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from pydantic import BaseModel | ||
|
||
class Human(BaseModel): | ||
name: str | ||
lastName: str | ||
physical: int | ||
studies: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .Human import Human |