Spaces:
Build error
Build error
Create api/main.py
Browse files- api/main.py +54 -0
api/main.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel, ValidationError, validator
|
| 3 |
+
|
| 4 |
+
from pytezos import pytezos
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
pytezos = pytezos.using(shell = 'https://rpc.tzkt.io/ghostnet', key='edsk3MrRkoidY2SjEgufvi44orvyjxgZoy4LhaJNTNcddWykW6SssL')
|
| 12 |
+
contract = pytezos.contract('KT1KvCVKiZhkPG8s9CCoxW3r135phk2HhZUV')
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Define the request body model
|
| 16 |
+
class Patient(BaseModel):
|
| 17 |
+
name: str
|
| 18 |
+
email: str
|
| 19 |
+
contact: int
|
| 20 |
+
age: int
|
| 21 |
+
gender: str
|
| 22 |
+
number: int
|
| 23 |
+
#is_offer: bool = None
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@validator('name')
|
| 27 |
+
def name_must_not_be_empty(cls, v):
|
| 28 |
+
if v == ' ':
|
| 29 |
+
raise ValueError('must contain a space')
|
| 30 |
+
return v
|
| 31 |
+
|
| 32 |
+
@validator('email')
|
| 33 |
+
def name_must_contain_at(cls, v):
|
| 34 |
+
if '@' not in v:
|
| 35 |
+
raise ValueError('must contain @ ')
|
| 36 |
+
return v
|
| 37 |
+
|
| 38 |
+
@app.get("/")
|
| 39 |
+
async def read_root():
|
| 40 |
+
return {"Hello": "World"}
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# Define the POST route handler
|
| 46 |
+
@app.post("/patient/")
|
| 47 |
+
async def create_patient(patient: Patient):
|
| 48 |
+
# patient object is validated automatically by FastAPI
|
| 49 |
+
patient_dict = patient.dict()
|
| 50 |
+
contract.addUser(email = patient.email, name = patient.name, age = patient.age, gender = patient.gender, number = patient.number).with_amount(0).as_transaction().fill().sign().inject()
|
| 51 |
+
return {"message": f"User {patient.name} with email {patient.email} created successfully"}
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|