Skip to content

Commit

Permalink
chore: Enable Jobs API flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ramedina86 committed Dec 16, 2024
1 parent d2680aa commit 7fd586d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
11 changes: 7 additions & 4 deletions src/writer/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ def main():
@main.command()
@click.option('--host', default="127.0.0.1", help="Host to run the app on")
@click.option('--port', default=None, help="Port to run the app on")
@click.option("--enable-jobs-api", help="Set this flag to enable the Jobs API, allowing you to execute jobs without user interaction.", is_flag=True)
@click.argument('path')
def run(path: str, host: str, port: Optional[int]):
def run(path: str, host: str, port: Optional[int], enable_jobs_api: bool):
"""Run the app from PATH folder in run mode."""

abs_path = os.path.abspath(path)
Expand All @@ -35,22 +36,24 @@ def run(path: str, host: str, port: Optional[int]):
raise click.ClickException(f"There’s no Writer Framework project at this location : {abs_path}")

writer.serve.serve(
abs_path, mode="run", port=port, host=host, enable_server_setup=True)
abs_path, mode="run", port=port, host=host, enable_server_setup=True, enable_jobs_api=enable_jobs_api)

@main.command()
@click.option('--host', default="127.0.0.1", help="Host to run the app on")
@click.option('--port', default=None, help="Port to run the app on")
@click.option('--enable-remote-edit', help="Set this flag to allow non-local requests in edit mode.", is_flag=True)
@click.option('--enable-server-setup', help="Set this flag to enable server setup hook in edit mode.", is_flag=True)
@click.option("--no-interactive", help="Set the flask to ask the app to run without asking anything to the user", is_flag=True)
@click.option("--enable-jobs-api", help="Set this flag to enable the Jobs API, allowing you to execute jobs without user interaction.", is_flag=True)
@click.argument('path')
def edit(
path: str,
port: Optional[int],
host: str,
enable_remote_edit: bool,
enable_server_setup: bool,
no_interactive: bool
no_interactive: bool,
enable_jobs_api: bool,
):
"""Run the app from PATH folder in edit mode."""
abs_path = os.path.abspath(path)
Expand All @@ -70,7 +73,7 @@ def edit(

writer.serve.serve(
abs_path, mode="edit", port=port, host=host,
enable_remote_edit=enable_remote_edit, enable_server_setup=enable_server_setup)
enable_remote_edit=enable_remote_edit, enable_server_setup=enable_server_setup, enable_jobs_api=enable_jobs_api)

@main.command()
@click.argument('path')
Expand Down
13 changes: 10 additions & 3 deletions src/writer/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ def get_asgi_app(
enable_remote_edit: bool = False,
enable_server_setup: bool = True,
on_load: Optional[Callable] = None,
on_shutdown: Optional[Callable] = None
on_shutdown: Optional[Callable] = None,
enable_jobs_api: bool = False
) -> WriterFastAPI:
"""
Builds an ASGI server that can be injected into another ASGI application
Expand Down Expand Up @@ -328,6 +329,9 @@ async def _get_payload_as_json(request: Request):

@app.post("/api/job/workflow/{workflow_key}")
async def create_workflow_job(workflow_key: str, request: Request, response: Response):
if not enable_jobs_api:
raise HTTPException(status_code=404)

crypto.verify_message_authorization_signature(f"create_job_{workflow_key}", request)

def serialize_result(data):
Expand Down Expand Up @@ -402,6 +406,9 @@ def job_done_callback(task: asyncio.Task, job_id: str):

@app.get("/api/job/{job_id}")
async def get_workflow_job(job_id: str, request: Request, response: Response):
if not enable_jobs_api:
raise HTTPException(status_code=404)

crypto.verify_message_authorization_signature(f"get_job_{job_id}", request)
job = app.state.job_vault.get(job_id)

Expand Down Expand Up @@ -725,7 +732,7 @@ def register_auth(
):
auth.register(app, callback=callback, unauthorized_action=unauthorized_action)

def serve(app_path: str, mode: ServeMode, port: Optional[int], host, enable_remote_edit=False, enable_server_setup=False):
def serve(app_path: str, mode: ServeMode, port: Optional[int], host, enable_remote_edit=False, enable_server_setup=False, enable_jobs_api=False):
""" Initialises the web server. """

print_init_message()
Expand All @@ -747,7 +754,7 @@ def on_load():
port = _next_localhost_available_port(mode_allowed_ports[mode])

enable_server_setup = mode == "run" or enable_server_setup
app = get_asgi_app(app_path, mode, enable_remote_edit, on_load=on_load, enable_server_setup=enable_server_setup)
app = get_asgi_app(app_path, mode, enable_remote_edit, on_load=on_load, enable_server_setup=enable_server_setup, enable_jobs_api=enable_jobs_api)
log_level = "warning"
uvicorn.run(app, host=host, port=port, log_level=log_level, ws_max_size=MAX_WEBSOCKET_MESSAGE_SIZE)

Expand Down
8 changes: 4 additions & 4 deletions tests/backend/test_serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def test_feature_flags(self):

def test_create_workflow_job_api(self, monkeypatch):
asgi_app: fastapi.FastAPI = writer.serve.get_asgi_app(
test_app_dir, "run")
test_app_dir, "run", enable_jobs_api=True)
monkeypatch.setenv("WRITER_SECRET_KEY", "abc")
workflow_key = "workflow2"

Expand All @@ -242,7 +242,7 @@ def test_create_workflow_job_api(self, monkeypatch):

def test_create_workflow_job_api_incorrect_token(self, monkeypatch):
asgi_app: fastapi.FastAPI = writer.serve.get_asgi_app(
test_app_dir, "run")
test_app_dir, "run", enable_jobs_api=True)
monkeypatch.setenv("WRITER_SECRET_KEY", "abc")
workflow_key = "workflow2"

Expand All @@ -258,7 +258,7 @@ def test_create_workflow_job_api_incorrect_token(self, monkeypatch):

def test_create_workflow_job_api_incorrect_token_for_get(self, monkeypatch):
asgi_app: fastapi.FastAPI = writer.serve.get_asgi_app(
test_app_dir, "run")
test_app_dir, "run", enable_jobs_api=True)
monkeypatch.setenv("WRITER_SECRET_KEY", "abc")
workflow_key = "workflow2"

Expand Down Expand Up @@ -294,7 +294,7 @@ def get(self, job_id: str):

writer.serve.JobVault.register(TestJobVault)
asgi_app: fastapi.FastAPI = writer.serve.get_asgi_app(
test_app_dir, "run")
test_app_dir, "run", enable_jobs_api=True)
with fastapi.testclient.TestClient(asgi_app) as client:
create_job_token = crypto.get_hash(f"create_job_{workflow_key}")
res = client.post(f"/api/job/workflow/{workflow_key}", json={
Expand Down

0 comments on commit 7fd586d

Please sign in to comment.