File size: 3,961 Bytes
fddda7b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import httpx
# --------------------
# Hasuraクライアント定義
# --------------------
class HasuraClient:
def __init__(self, url: str, admin_secret: str):
self.url = url
self.headers = {
"x-hasura-admin-secret": admin_secret,
"Content-Type": "application/json"
}
async def execute(self, query: str, variables: dict):
async with httpx.AsyncClient() as client:
res = await client.post(
self.url,
json={"query": query, "variables": variables},
headers=self.headers
)
res.raise_for_status()
return res.json()["data"]
async def insert_chat(self, item: dict):
query = """
mutation InsertChat($object: chat_history_insert_input!) {
insert_chat_history_one(object: $object) {
id
ownerid
messages
status
soundRecord
isread
status_created
}
}
"""
return (await self.execute(query, {"object": item}))["insert_chat_history_one"]
async def get_chat(self, id: int):
query = """
query GetChat($id: Int!) {
chat_history_by_pk(id: $id) {
id
ownerid
messages
status
soundRecord
isread
status_created
}
}
"""
return (await self.execute(query, {"id": id}))["chat_history_by_pk"]
async def update_chat(self, id: int, changes: dict):
query = """
mutation UpdateChat($id: Int!, $changes: chat_history_set_input!) {
update_chat_history_by_pk(pk_columns: {id: $id}, _set: $changes) {
id
messages
status
isread
}
}
"""
return (await self.execute(query, {"id": id, "changes": changes}))["update_chat_history_by_pk"]
async def delete_chat(self, id: int):
query = """
mutation DeleteChat($id: Int!) {
delete_chat_history_by_pk(id: $id) {
id
}
}
"""
return (await self.execute(query, {"id": id}))["delete_chat_history_by_pk"]
# --------------------
# FastAPI アプリ定義
# --------------------
app = FastAPI()
# Hasura設定(自分の環境に置き換えてください)
HASURA_URL = "https://your-hasura-instance/v1/graphql"
HASURA_ADMIN_SECRET = "your-admin-secret"
client = HasuraClient(HASURA_URL, HASURA_ADMIN_SECRET)
# --------------------
# Pydanticモデル
# --------------------
class ChatHistoryCreate(BaseModel):
ownerid: str
messages: str
status: str
soundRecord: str
class ChatHistoryUpdate(BaseModel):
messages: str | None = None
status: str | None = None
isread: bool | None = None
# --------------------
# ルート
# --------------------
@app.post("/chat_history")
async def create_chat(item: ChatHistoryCreate):
try:
return await client.insert_chat(item.dict())
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/chat_history/{id}")
async def get_chat(id: int):
try:
return await client.get_chat(id)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.put("/chat_history/{id}")
async def update_chat(id: int, item: ChatHistoryUpdate):
try:
return await client.update_chat(id, {k: v for k, v in item.dict().items() if v is not None})
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.delete("/chat_history/{id}")
async def delete_chat(id: int):
try:
deleted = await client.delete_chat(id)
return {"deleted_id": deleted["id"]}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
|