mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-25 00:16:23 +02:00
- Keeps processing in different flows separate so that data can go to different stores / collections etc. - Potentially supports different processing flows - Tidies the processing API with common base-classes for e.g. LLMs, and automatic configuration of 'clients' to use the right queue names in a flow
65 lines
1.4 KiB
Python
65 lines
1.4 KiB
Python
|
|
import asyncio
|
|
from aiohttp import web
|
|
import uuid
|
|
import logging
|
|
|
|
logger = logging.getLogger("endpoint")
|
|
logger.setLevel(logging.INFO)
|
|
|
|
class ServiceEndpoint:
|
|
|
|
def __init__(self, endpoint_path, auth, requestor):
|
|
|
|
self.path = endpoint_path
|
|
|
|
self.auth = auth
|
|
self.operation = "service"
|
|
|
|
self.requestor = requestor
|
|
|
|
async def start(self):
|
|
await self.requestor.start()
|
|
|
|
def add_routes(self, app):
|
|
|
|
app.add_routes([
|
|
web.post(self.path, self.handle),
|
|
])
|
|
|
|
async def handle(self, request):
|
|
|
|
print(request.path, "...")
|
|
|
|
try:
|
|
ht = request.headers["Authorization"]
|
|
tokens = ht.split(" ", 2)
|
|
if tokens[0] != "Bearer":
|
|
return web.HTTPUnauthorized()
|
|
token = tokens[1]
|
|
except:
|
|
token = ""
|
|
|
|
if not self.auth.permitted(token, self.operation):
|
|
return web.HTTPUnauthorized()
|
|
|
|
try:
|
|
|
|
data = await request.json()
|
|
|
|
print(data)
|
|
|
|
async def responder(x, fin):
|
|
print(x)
|
|
|
|
resp = await self.requestor.process(data, responder)
|
|
|
|
return web.json_response(resp)
|
|
|
|
except Exception as e:
|
|
logging.error(f"Exception: {e}")
|
|
|
|
return web.json_response(
|
|
{ "error": str(e) }
|
|
)
|
|
|