# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/03_pingme_api.ipynb.

# %% auto 0
__all__ = ['app', 'help', 'webhook_help', 'webhook_card_default', 'webhook_card_simple', 'webhook_card', 'webservice']

# %% ../nbs/03_pingme_api.ipynb 2
import os
import uvicorn
from fastapi import FastAPI
from fastapi.testclient import TestClient

from .core import settings
from . import core
from .pingme_class import Card
from .services import NotificationService

from fastapi import HTTPException

from fastcore.script import (
    call_parse,
)

# %% ../nbs/03_pingme_api.ipynb 3
app = FastAPI()

# %% ../nbs/03_pingme_api.ipynb 4
@app.get("/")
async def help():
    # Using the default place as a information post
    return {"msg": "PingMe: please POST to /webhook }"}

# %% ../nbs/03_pingme_api.ipynb 6
@app.get("/webhook/")
async def webhook_help():
    # Using the default place as a information post
    return {
        "msg": "webhook: please POST to /webhook/default or /webhook/simple or /webhook/card"
    }

# %% ../nbs/03_pingme_api.ipynb 8
@app.get("/webhook/default")
def webhook_card_default():
    try:
        return NotificationService.send_default_card_to_webhook()
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# %% ../nbs/03_pingme_api.ipynb 10
@app.get("/webhook/simple")
def webhook_card_simple(title: str, text: str):
    try:
        return NotificationService.send_simple_card_to_webhook(title, text)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/webhook/card/")
def webhook_card(card: Card):
    try:
        return NotificationService.send_card_to_webhook(card)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# %% ../nbs/03_pingme_api.ipynb 12
@call_parse
def webservice(
    port: int = 5000,  # Port to run the server on"
    config_file: str = None,  # Path to config file"
):
    """Start the PingMe API server."""
    # Override settings if provided
    if config_file is not None:
        settings.config_file = config_file

    # Run using module path instead of app instance to ensure updated config
    uvicorn.run("pingme.api:app", host="127.0.0.1", port=port, reload=core.DEV_MODE)
