integrate/app/main.py

56 lines
1.4 KiB
Python
Raw Normal View History

2022-03-06 15:29:00 +01:00
import logging
import structlog
from unicodedata import name
from urllib.error import HTTPError
2022-03-03 16:36:08 +01:00
from fastapi import FastAPI, Request
2022-03-06 15:29:00 +01:00
from pydantic import BaseModel
2022-03-04 18:51:33 +01:00
2022-03-06 15:29:00 +01:00
from app.authentik.api import Authentik
from app.authentik.models import User
2022-03-03 16:36:08 +01:00
from .wekan.api import Wekan
import json
2022-03-02 16:10:17 +01:00
2022-03-06 15:29:00 +01:00
logging = structlog.get_logger()
class Hook_Model(BaseModel):
pk: str
app: str
name: str
model_name: str
class Http_request(BaseModel):
args: dict
path: str
method: str
2022-03-02 16:10:17 +01:00
app = FastAPI()
@app.get("/")
async def root():
2022-03-03 16:36:08 +01:00
return {'message': 'Hello World'}
2022-03-06 15:29:00 +01:00
@app.post("/authentik/hook/")
async def hook(model: Hook_Model, http_request: Http_request):
2022-03-04 18:51:33 +01:00
# print(await request.body())
2022-03-06 15:29:00 +01:00
logging.info(model)
logging.info(http_request)
return 200
2022-03-03 16:36:08 +01:00
# model_created = json.loads(r['body'].split("model_created: ")[1])["model"]
2022-03-04 18:51:33 +01:00
# hook wekan.create_user(model_created["pk"])
2022-03-06 15:29:00 +01:00
@app.get("/authentik/create_hook/")
2022-03-04 18:51:33 +01:00
async def hook(request: Request):
2022-03-06 15:29:00 +01:00
a = Authentik(base="http://localhost:9000/", token="foobar123")
res = a.create_web_hook()
logging.info(res)
@app.get("/authentik/users/create_demo_user/")
async def create_demo_user(request: Request):
a = Authentik(base="http://localhost:9000/", token="foobar123")
try:
user = a.create_user(User(username="demo", name="dmeo", email="foo@example.org"))
except Exception as e: # TODO
return e
logging.info(user)
return user.dict