File size: 2,737 Bytes
948547c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from fastapi import APIRouter, Request, HTTPException
from service_config import ServiceConfig
import json
import datetime

router = APIRouter()
service_config = ServiceConfig()
service_config.load()

def get_utc_now():
    return datetime.datetime.utcnow().isoformat()

@router.get("/list")
def list_apis():
    return service_config.apis

@router.post("/add")
async def add_api(request: Request):
    data = await request.json()
    api_name = data.get("api_name")
    api_data = data.get("api_data")

    if not api_name or not api_data:
        raise HTTPException(status_code=400, detail="api_name and api_data are required")

    if api_name in service_config.apis:
        raise HTTPException(status_code=400, detail="API with this name already exists")

    service_config.apis[api_name] = api_data

    with open(service_config.config_path, "w", encoding="utf-8") as f:
        json.dump(service_config, f, indent=2)

    return {"message": f"API {api_name} added"}

@router.post("/update")
async def update_api(request: Request):
    data = await request.json()
    api_name = data.get("api_name")
    api_data = data.get("api_data")

    if not api_name or not api_data:
        raise HTTPException(status_code=400, detail="api_name and api_data are required")

    if api_name not in service_config.apis:
        raise HTTPException(status_code=404, detail="API not found")

    service_config.apis[api_name] = api_data

    with open(service_config.config_path, "w", encoding="utf-8") as f:
        json.dump(service_config, f, indent=2)

    return {"message": f"API {api_name} updated"}

@router.post("/delete")
async def delete_api(request: Request):
    data = await request.json()
    api_name = data.get("api_name")

    if not api_name:
        raise HTTPException(status_code=400, detail="api_name is required")

    if api_name not in service_config.apis:
        raise HTTPException(status_code=404, detail="API not found")

    # Check if API is used in any intent
    used_in = []
    for project_name, project in service_config.projects.items():
        for version in project.get("versions", []):
            for intent in version.get("intents", []):
                if intent.get("action") == api_name:
                    used_in.append(f"{project_name} β†’ v{version.get('version_number')} β†’ {intent.get('name')}")

    if used_in:
        raise HTTPException(
            status_code=400,
            detail=f"API '{api_name}' is used in intents: {', '.join(used_in)}. Cannot delete."
        )

    del service_config.apis[api_name]

    with open(service_config.config_path, "w", encoding="utf-8") as f:
        json.dump(service_config, f, indent=2)

    return {"message": f"API {api_name} deleted"}