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