Skip to content

Commit

Permalink
Added code for basic API get and edit operations
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeLynxy committed May 14, 2021
1 parent f307b22 commit 5c4aa8b
Show file tree
Hide file tree
Showing 15 changed files with 226 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PYTHONPATH=./api
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__/
*.pyc
.vscode
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM ubuntu:20.04
RUN apt update
RUN apt install -y software-properties-common && \
rm -rf /var/lib/apt/lists/*
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt install -y python3.8 python3-pip

WORKDIR /api

COPY requirements.txt .
RUN pip3 install --upgrade pip
RUN pip3 install --no-cache-dir -r requirements.txt

VOLUME /api
VOLUME /smart-contracts

ADD /api /api
ADD /smart-contracts /smart-contracts

RUN add-apt-repository ppa:ethereum/ethereum
RUN apt-get update
RUN apt-get install -y solc
RUN pip3 install py-solc-x
WORKDIR /smart-contracts
#RUN python3.8 deploy.py
WORKDIR /api
Empty file added api/config/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions api/config/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from motor.motor_asyncio import AsyncIOMotorClient
from .utils import load_config


CONF = load_config()

############### MONGO DATABASE ####################

DB_CLIENT = AsyncIOMotorClient(
host=CONF.get("databases")["default"]["HOST"],
port=int(CONF.get("databases")["default"]["PORT"]),
username=CONF.get("databases")["default"]["USER"],
password=CONF.get("databases")["default"]["PASSWORD"],
)

DB = DB_CLIENT[CONF.get("databases")["default"]["NAME"]]

def close_db_client():
DB_CLIENT.close()

12 changes: 12 additions & 0 deletions api/config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fastapi:
debug: true

databases:
default:
ENGINE: 'mongodb'
DRIVER: 'motor'
NAME: 'blockchain_med_integrity_db'
USER: 'lupin'
PASSWORD: 'BNPH.9z5g^8z6Pr4'
HOST: 'blockchain-med-integrity-db'
PORT: '27019'
1 change: 1 addition & 0 deletions api/config/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_FILE = 'config/config.yml'
7 changes: 7 additions & 0 deletions api/config/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .constants import CONFIG_FILE
import yaml

def load_config() -> dict:
with open(CONFIG_FILE) as yaml_file:
conf = yaml.load(yaml_file.read(), Loader=yaml.SafeLoader)
return conf
50 changes: 50 additions & 0 deletions api/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from config import config
from patients.routes import patients_router
from web3 import Web3
#from utils.contract import MedDataIntegrityContractBridge


#w3 = Web3(Web3.HTTPProvider("http://172.17.0.1:7545"))
#w3.eth.defaultAccount = w3.eth.accounts[0]

#med_data_integrity_contract_bridge = ChainVoteContractBridge(w3)

app = FastAPI()

origins = [
"http://0.0.0.0"
]

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

app.include_router(
patients_router,
prefix="/med-data-integrity-api/patients",
tags=["patients"],
responses={404: {"description": "Not found"}},
)


@app.on_event("startup")
async def app_startup():
"""
App initialization.
"""
print("App has started ...")


@app.on_event("shutdown")
async def app_shutdown():
"""
App termination.
"""
print( "App termination ...")

67 changes: 67 additions & 0 deletions api/patients/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from fastapi import APIRouter
from bson.objectid import ObjectId
import bcrypt
from config.config import DB
from utils import *
import main

patients_router = APIRouter()
patients = DB.patients

@patients_router.put("/edit")
async def edit_patient(data: dict):
patient_id = ObjectId(data.get("id"))
patient = await patients.find_one({"_id": patient_id})
if patient:
result = await patients.update_one(
{
"_id": patient_id
},
{
"$set": {
"patient_details": data.get("core_data")
}
}
)
if result.modified_count > 0:
return {
"status_code": 2000,
"detail": "Patient information is successfully modified"
}
else:
return {
"status_code": 1001,
"detail": "An error occurred while modifying patient information"
}
else:
return {
"status_code": 1000,
"detail": "Failed to retrieve this patient"
}


@patients_router.get("/{patient_id}")
async def get_patient(patient_id: str):

try:
patient_id = ObjectId(patient_id)
patient = await patients.find_one({"_id": patient_id})
if patient:
patient["_id"] = str(patient["_id"])
return {
"status_code": 2000,
"detail": patient
}
else:
return {
"status_code": 1001,
"detail": "No patient with this id"
}

except Exception as e:
return {
"status_code": 1000,
"detail": "Invalid patient id"
}


Empty file added api/utils/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: '3'

volumes:
mongo_data:

services:
blockchain-med-integrity-api:
restart: always
stdin_open: true
tty: true
build: .
command: uvicorn main:app --host 0.0.0.0 --port 7777 --debug --reload
volumes:
- ./api:/api
- ./smart-contracts:/smart-contracts
ports:
- "7777:7777"
depends_on:
- blockchain-med-integrity-db


blockchain-med-integrity-db:
restart: always
image: mongo
command: mongod --port 27019
environment:
MONGO_INITDB_ROOT_USERNAME: "lupin"
MONGO_INITDB_ROOT_PASSWORD: "BNPH.9z5g^8z6Pr4"
volumes:
- mongo_data:/data/db
ports:
- "27019:27019"
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fastapi[all]
motor
pydantic[email]
bcrypt
pyOpenSSL
python-multipart
web3
Empty file added smart-contracts/deploy.py
Empty file.
Empty file.

0 comments on commit 5c4aa8b

Please sign in to comment.