Access Messages via API from Python
This commit is contained in:
@ -10,6 +10,7 @@
|
||||
|
||||
import os
|
||||
|
||||
from typing import List
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi import FastAPI, Request, BackgroundTasks, HTTPException
|
||||
@ -78,7 +79,7 @@ class WebMain():
|
||||
{"request" : request}
|
||||
)
|
||||
|
||||
@self.app.post("/message", summary="Send a message to the management")
|
||||
@self.app.post("/message", summary="Send a message to the management", tags=['agents'])
|
||||
def message(request: Request, message:AgentMessage, background_tasks: BackgroundTasks) -> AgentResponse:
|
||||
|
||||
receiver = request.headers['host']
|
||||
@ -89,14 +90,36 @@ class WebMain():
|
||||
|
||||
return self.msg_process.new_message(sender, receiver, message, background_tasks)
|
||||
|
||||
@self.app.get("/status", summary="Get status of a message")
|
||||
@self.app.get("/list", summary="Get list of messages (like table)", tags=["cli, agents"])
|
||||
def list(id:str|None=None, sender:str|None=None, recipient:str|None=None,
|
||||
processed:bool|None=None, solution:bool|None=None,
|
||||
time_after:int|None=None, time_before:int|None=None,
|
||||
limit:int=10, offset:int=0
|
||||
) -> List[MessageDbRow]:
|
||||
|
||||
db_args = {
|
||||
"limit" : limit,
|
||||
"offset" : offset
|
||||
}
|
||||
|
||||
for v,n in (
|
||||
(id,'id'), (sender,'sender'), (recipient,'recipient'),
|
||||
(processed,'processed'), (solution,'solution'),
|
||||
(time_after, 'time_after'), (time_before, 'time_before')
|
||||
):
|
||||
if not v is None:
|
||||
db_args[n] = v
|
||||
|
||||
return [row for row in self.db.iterate(**db_args)]
|
||||
|
||||
@self.app.get("/list/single", summary="Get a single message", tags=["cli, agents"])
|
||||
def status(count:int) -> MessageDbRow:
|
||||
msg = self.db.by_count(count)
|
||||
if msg is None:
|
||||
raise HTTPException(status_code=404, detail="Message not found")
|
||||
|
||||
return msg
|
||||
|
||||
|
||||
if __name__ == "ums.management.main" and os.environ.get('SERVE', 'false') == 'true':
|
||||
main = WebMain()
|
||||
app = main.app
|
Reference in New Issue
Block a user