File size: 2,805 Bytes
833cf90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, HTTPException, Request, Depends
from config_provider import get_config, ServiceConfig
from service_config import ServiceConfig
import json

router = APIRouter()

@router.get("/list")
def list_apis(config: ServiceConfig = Depends(get_config)):
    return config.apis

@router.post("/add")
async def add_api(request: Request, config: ServiceConfig = Depends(get_config)):
    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 config.apis:
        raise HTTPException(status_code=400, detail="API with this name already exists")

    config.apis[api_name] = api_data

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

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

@router.post("/update")
async def update_api(request: Request, config: ServiceConfig = Depends(get_config)):
    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 config.apis:
        raise HTTPException(status_code=404, detail="API not found")

    config.apis[api_name] = api_data

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

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

@router.post("/delete")
async def delete_api(request: Request, config: ServiceConfig = Depends(get_config)):
    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 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 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 config.apis[api_name]

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

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