Spaces:
Building
Building
from fastapi import APIRouter, Depends, HTTPException | |
from config_provider import get_config, ServiceConfig | |
from utils import save_service_config, log | |
import copy | |
router = APIRouter() | |
def get_project_details(config: ServiceConfig = Depends(get_config)): | |
project = config.get_current_project() | |
return project | |
def update_project(config: ServiceConfig = Depends(get_config)): | |
# Implement project update logic here if needed | |
save_service_config(config) | |
log("β Project updated") | |
return { "status": "updated" } | |
def publish_project(config: ServiceConfig = Depends(get_config)): | |
project = config.get_current_project() | |
current_version = next((v for v in project['versions'] if not v.get('published', False)), None) | |
if not current_version: | |
raise HTTPException(status_code=400, detail="No unpublished version found") | |
current_version['published'] = True | |
save_service_config(config) | |
log(f"β Version {current_version['no']} published for project {project['name']}") | |
return { "status": "published" } | |
def create_project(name: str, config: ServiceConfig = Depends(get_config)): | |
if any(p['name'] == name for p in config.data['projects']): | |
raise HTTPException(status_code=400, detail="Project already exists") | |
new_project = { | |
"name": name, | |
"versions": [] | |
} | |
config.data['projects'].append(new_project) | |
save_service_config(config) | |
log(f"β New project '{name}' created") | |
return { "status": "success" } | |
def delete_project(name: str, config: ServiceConfig = Depends(get_config)): | |
project = next((p for p in config.data['projects'] if p['name'] == name), None) | |
if not project: | |
raise HTTPException(status_code=404, detail="Project not found") | |
config.data['projects'].remove(project) | |
save_service_config(config) | |
log(f"ποΈ Project '{name}' deleted") | |
return { "status": "success" } | |
def get_project_versions(config: ServiceConfig = Depends(get_config)): | |
project = config.get_current_project() | |
return project['versions'] | |
def create_new_version(project_name: str, base_version_no: int, config: ServiceConfig = Depends(get_config)): | |
project = next((p for p in config.data['projects'] if p['name'] == project_name), None) | |
if not project: | |
raise HTTPException(status_code=404, detail="Project not found") | |
base_version = next((v for v in project['versions'] if v['no'] == base_version_no), None) | |
if not base_version: | |
raise HTTPException(status_code=404, detail="Base version not found") | |
new_version_no = max((v['no'] for v in project['versions']), default=0) + 1 | |
new_version = copy.deepcopy(base_version) | |
new_version['no'] = new_version_no | |
new_version['published'] = False | |
project['versions'].append(new_version) | |
save_service_config(config) | |
log(f"β New version {new_version_no} created for project {project_name}") | |
return { "status": "success", "new_version": new_version_no } | |
def delete_version(project_name: str, version_no: int, config: ServiceConfig = Depends(get_config)): | |
project = next((p for p in config.data['projects'] if p['name'] == project_name), None) | |
if not project: | |
raise HTTPException(status_code=404, detail="Project not found") | |
version = next((v for v in project['versions'] if v['no'] == version_no), None) | |
if not version: | |
raise HTTPException(status_code=404, detail="Version not found") | |
project['versions'].remove(version) | |
save_service_config(config) | |
log(f"ποΈ Version {version_no} deleted from project {project_name}") | |
return { "status": "success" } | |
def update_api(project_name: str, version_no: int, api_data: dict, config: ServiceConfig = Depends(get_config)): | |
project = next((p for p in config.data['projects'] if p['name'] == project_name), None) | |
if not project: | |
raise HTTPException(status_code=404, detail="Project not found") | |
version = next((v for v in project['versions'] if v['no'] == version_no), None) | |
if not version: | |
raise HTTPException(status_code=404, detail="Version not found") | |
existing_api = next((a for a in version['apis'] if a['name'] == api_data['name']), None) | |
if existing_api: | |
version['apis'].remove(existing_api) | |
version['apis'].append(api_data) | |
save_service_config(config) | |
log(f"π§ API '{api_data['name']}' updated in project {project_name} version {version_no}") | |
return { "status": "success" } | |