from fastapi import FastAPI from pydantic import BaseModel, ValidationError, validator from pytezos import pytezos app = FastAPI() pytezos = pytezos.using(shell = 'https://rpc.tzkt.io/ghostnet', key='edsk3MrRkoidY2SjEgufvi44orvyjxgZoy4LhaJNTNcddWykW6SssL') contract = pytezos.contract('KT1KvCVKiZhkPG8s9CCoxW3r135phk2HhZUV') # Define the request body model class Patient(BaseModel): name: str email: str contact: int age: int gender: str number: int #is_offer: bool = None @validator('name') def name_must_not_be_empty(cls, v): if v == ' ': raise ValueError('must contain a space') return v @validator('email') def name_must_contain_at(cls, v): if '@' not in v: raise ValueError('must contain @ ') return v @app.get("/") async def read_root(): return {"Hello": "World"} # Define the POST route handler @app.post("/patient/") async def create_patient(patient: Patient): # patient object is validated automatically by FastAPI patient_dict = patient.dict() 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() return {"message": f"User {patient.name} with email {patient.email} created successfully"}