Skip to content

Commit

Permalink
Merge pull request #7 from Julamada-GAIA-Project/rdvl/cosmo-to-mongo
Browse files Browse the repository at this point in the history
Rdvl/cosmo-to-mongo
  • Loading branch information
r-dvl authored Sep 6, 2023
2 parents a533991 + 17f2bce commit c4d0020
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
GAIA Project API

## Docker Image
Environment vars required in run command to protect CosmoDB secrets
Environment vars required in run command to protect MongoDB secrets

### Run cmd
~~~shell
docker run -e COSMODB_URI=[uri] -e COSMODB_KEY=[key] -e COSMODB_NAME=[name] -e COSMODB_CONTAINER=[container] ghcr.io/julamada-gaia-project/gaia-project-backend:latest
docker run -e MONGODB_URI=[uri] ghcr.io/julamada-gaia-project/gaia-project-backend:latest
~~~
5 changes: 1 addition & 4 deletions dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ FROM python:3.8-slim

WORKDIR /app

ENV COSMODB_URI = ""
ENV COSMODB_KEY = ""
ENV COSMODB_NAME = ""
ENV COSMODB_CONTAINER = ""
ENV MONGODB_URI = ""

COPY requirements.txt .

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fastapi
azure-cosmos
pymongo
pydantic
uvicorn
58 changes: 32 additions & 26 deletions src/main.py
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
7 changes: 7 additions & 0 deletions src/models/Human.py
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
1 change: 1 addition & 0 deletions src/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .Human import Human

0 comments on commit c4d0020

Please sign in to comment.