celery-fastapi 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,27 @@
1
+ """
2
+ Celery FastAPI - Automatic REST API generation for Celery tasks.
3
+
4
+ This package provides seamless integration between Celery and FastAPI,
5
+ automatically generating REST endpoints for all registered Celery tasks.
6
+ """
7
+
8
+ from celery_fastapi.app import create_app, load_celery_app
9
+ from celery_fastapi.core import (
10
+ CeleryFastAPIBridge,
11
+ GenericTaskPayload,
12
+ TaskResponse,
13
+ TaskRevokePayload,
14
+ TaskStatusResponse,
15
+ )
16
+
17
+ __version__ = "0.1.0"
18
+ __all__ = [
19
+ "CeleryFastAPIBridge",
20
+ "create_app",
21
+ "load_celery_app",
22
+ "GenericTaskPayload",
23
+ "TaskResponse",
24
+ "TaskStatusResponse",
25
+ "TaskRevokePayload",
26
+ "__version__",
27
+ ]
celery_fastapi/app.py ADDED
@@ -0,0 +1,154 @@
1
+ """Application factory for Celery FastAPI."""
2
+
3
+ import importlib
4
+ import importlib.util
5
+ import sys
6
+ from pathlib import Path
7
+ from typing import Any
8
+
9
+ from celery import Celery
10
+ from fastapi import FastAPI
11
+
12
+ from celery_fastapi.core import CeleryFastAPIBridge
13
+
14
+
15
+ def load_celery_app(celery_app_path: str) -> Celery:
16
+ """
17
+ Load a Celery application from a module path.
18
+
19
+ Args:
20
+ celery_app_path: Path to the Celery app in format 'module:attribute'
21
+ or 'module.attribute'. Examples:
22
+ - 'myapp.celery:app'
23
+ - 'celery_app:celery_app'
24
+ - 'tasks.celery_config:celery'
25
+
26
+ Returns:
27
+ The loaded Celery application instance.
28
+
29
+ Raises:
30
+ ImportError: If the module cannot be imported.
31
+ AttributeError: If the attribute doesn't exist in the module.
32
+ TypeError: If the attribute is not a Celery instance.
33
+ """
34
+ # Parse the module path
35
+ if ":" in celery_app_path:
36
+ module_path, attr_name = celery_app_path.rsplit(":", 1)
37
+ elif "." in celery_app_path:
38
+ # Try to split on the last dot
39
+ parts = celery_app_path.rsplit(".", 1)
40
+ if len(parts) == 2:
41
+ module_path, attr_name = parts
42
+ else:
43
+ module_path = celery_app_path
44
+ attr_name = "celery_app"
45
+ else:
46
+ module_path = celery_app_path
47
+ attr_name = "celery_app"
48
+
49
+ # Add current directory to path if not present
50
+ cwd = str(Path.cwd())
51
+ if cwd not in sys.path:
52
+ sys.path.insert(0, cwd)
53
+
54
+ # Try to import as a module first
55
+ try:
56
+ module = importlib.import_module(module_path)
57
+ except ImportError:
58
+ # Try loading as a file path
59
+ module_file = Path(module_path.replace(".", "/") + ".py")
60
+ if module_file.exists():
61
+ spec = importlib.util.spec_from_file_location(module_path, module_file)
62
+ if spec and spec.loader:
63
+ module = importlib.util.module_from_spec(spec)
64
+ sys.modules[module_path] = module
65
+ spec.loader.exec_module(module)
66
+ else:
67
+ raise ImportError(f"Cannot load module from {module_file}")
68
+ else:
69
+ raise
70
+
71
+ celery_app = getattr(module, attr_name)
72
+
73
+ if not isinstance(celery_app, Celery):
74
+ raise TypeError(f"Expected Celery instance, got {type(celery_app).__name__}")
75
+
76
+ return celery_app
77
+
78
+
79
+ def create_app(
80
+ celery_app: Celery | str,
81
+ *,
82
+ title: str = "Celery FastAPI",
83
+ description: str = "Auto-generated REST API for Celery tasks",
84
+ version: str = "1.0.0",
85
+ prefix: str = "",
86
+ include_status_endpoints: bool = True,
87
+ fastapi_kwargs: dict[str, Any] | None = None,
88
+ ) -> FastAPI:
89
+ """
90
+ Create a FastAPI application with Celery task endpoints.
91
+
92
+ This is the main factory function for creating a complete FastAPI
93
+ application that exposes all Celery tasks as REST endpoints.
94
+
95
+ Args:
96
+ celery_app: Either a Celery instance or a string path to load one.
97
+ String format: 'module:attribute' (e.g., 'myapp:celery_app')
98
+ title: Title for the FastAPI application.
99
+ description: Description for the API documentation.
100
+ version: API version string.
101
+ prefix: URL prefix for all endpoints.
102
+ include_status_endpoints: Whether to include /tasks and /tasks/{id} endpoints.
103
+ fastapi_kwargs: Additional keyword arguments to pass to FastAPI.
104
+
105
+ Returns:
106
+ Configured FastAPI application with all routes registered.
107
+
108
+ Example:
109
+ ```python
110
+ from celery_fastapi import create_app
111
+
112
+ # From a Celery instance
113
+ from myapp import celery_app
114
+ app = create_app(celery_app)
115
+
116
+ # From a module path
117
+ app = create_app("myapp.celery:app")
118
+
119
+ # With custom settings
120
+ app = create_app(
121
+ celery_app,
122
+ title="My Task API",
123
+ prefix="/api/v1",
124
+ )
125
+ ```
126
+ """
127
+ # Load Celery app if string path provided
128
+ if isinstance(celery_app, str):
129
+ celery_app = load_celery_app(celery_app)
130
+
131
+ # Create FastAPI application
132
+ fastapi_kwargs = fastapi_kwargs or {}
133
+ fastapi_app = FastAPI(
134
+ title=title,
135
+ description=description,
136
+ version=version,
137
+ **fastapi_kwargs,
138
+ )
139
+
140
+ # Create and configure the bridge
141
+ bridge = CeleryFastAPIBridge(
142
+ celery_app=celery_app,
143
+ fastapi_app=fastapi_app,
144
+ prefix=prefix,
145
+ include_status_endpoints=include_status_endpoints,
146
+ )
147
+
148
+ # Register all routes
149
+ bridge.register_routes()
150
+
151
+ # Store bridge reference for later access
152
+ fastapi_app.state.celery_bridge = bridge
153
+
154
+ return fastapi_app