Skip to content

Commit 3b08c71

Browse files
committed
kraken: Connect jobs module with API V2
1 parent ef7b0a4 commit 3b08c71

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

core/services/kraken/api/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
container_router_v2,
1313
extension_router_v2,
1414
index_router_v2,
15+
jobs_router_v2,
1516
manifest_router_v2,
1617
)
1718

@@ -29,6 +30,7 @@
2930
application.include_router(index_router_v2)
3031
application.include_router(container_router_v2)
3132
application.include_router(extension_router_v2)
33+
application.include_router(jobs_router_v2)
3234
application.include_router(manifest_router_v2)
3335

3436
application = VersionedFastAPI(application, prefix_format="/v{major}.{minor}", enable_latest=True)

core/services/kraken/api/v2/routers/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .container import container_router_v2
33
from .extension import extension_router_v2
44
from .index import index_router_v2
5+
from .jobs import jobs_router_v2
56
from .manifest import manifest_router_v2
67

7-
__all__ = ["container_router_v2", "extension_router_v2", "index_router_v2", "manifest_router_v2"]
8+
__all__ = ["container_router_v2", "extension_router_v2", "index_router_v2", "jobs_router_v2", "manifest_router_v2"]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import uuid
2+
from functools import wraps
3+
from typing import Any, Callable, List, Tuple
4+
5+
from fastapi import APIRouter, Body, HTTPException, status
6+
from fastapi_versioning import versioned_api_route
7+
8+
from jobs import JobsManager
9+
from jobs.exceptions import JobNotFound
10+
from jobs.models import Job, JobMethod
11+
12+
jobs_router_v2 = APIRouter(
13+
prefix="/jobs",
14+
tags=["jobs_v2"],
15+
route_class=versioned_api_route(2, 0),
16+
responses={status.HTTP_404_NOT_FOUND: {"description": "Not found"}},
17+
)
18+
19+
20+
def jobs_to_http_exception(endpoint: Callable[..., Any]) -> Callable[..., Any]:
21+
@wraps(endpoint)
22+
async def wrapper(*args: Tuple[Any], **kwargs: dict[str, Any]) -> Any:
23+
try:
24+
return await endpoint(*args, **kwargs)
25+
except JobNotFound as error:
26+
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(error)) from error
27+
except Exception as error:
28+
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(error)) from error
29+
30+
return wrapper
31+
32+
33+
@jobs_router_v2.post("/{route:path}", status_code=status.HTTP_202_ACCEPTED)
34+
@jobs_to_http_exception
35+
async def create(
36+
route: str, body: dict[str, Any] = Body(...), method: JobMethod = JobMethod.POST, retries: int = 5
37+
) -> Job:
38+
job = Job(id=str(uuid.uuid4()), route=route, method=method, body=body, retries=retries)
39+
JobsManager.add(job)
40+
return job
41+
42+
43+
@jobs_router_v2.get("/", status_code=status.HTTP_200_OK)
44+
@jobs_to_http_exception
45+
async def fetch() -> List[Job]:
46+
return JobsManager.get()
47+
48+
49+
@jobs_router_v2.get("/{identifier}", status_code=status.HTTP_200_OK)
50+
@jobs_to_http_exception
51+
async def fetch_by_identifier(identifier: str) -> Job:
52+
return JobsManager.get_by_identifier(identifier)
53+
54+
55+
@jobs_router_v2.delete("/{identifier}", status_code=status.HTTP_204_NO_CONTENT)
56+
@jobs_to_http_exception
57+
async def delete(identifier: str) -> None:
58+
JobsManager.delete(identifier)

0 commit comments

Comments
 (0)