Skip to content

Commit 0caa8cb

Browse files
Using Annotation to reduce duplication of Depends-logic in router.py (#451)
* Using Annotation to reduce duplication of Depends-logic in router.py * modifying name of annotations
1 parent 6a76153 commit 0caa8cb

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

jbi/router.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Core FastAPI app (setup, middleware)
33
"""
44
from pathlib import Path
5-
from typing import Optional
5+
from typing import Annotated, Optional
66

77
from fastapi import APIRouter, Body, Depends, Request, Response
88
from fastapi.encoders import jsonable_encoder
@@ -15,11 +15,14 @@
1515
from jbi.runner import IgnoreInvalidRequestError, execute_action
1616
from jbi.services import bugzilla, jira
1717

18+
_settings = Annotated[Settings, Depends(get_settings)]
19+
_actions = Annotated[Actions, Depends(get_actions)]
20+
_version = Annotated[dict, Depends(get_version)]
1821
router = APIRouter()
1922

2023

2124
@router.get("/", include_in_schema=False)
22-
def root(request: Request, settings: Settings = Depends(get_settings)):
25+
def root(request: Request, settings: _settings):
2326
"""Expose key configuration"""
2427
return {
2528
"title": request.app.title,
@@ -35,7 +38,7 @@ def root(request: Request, settings: Settings = Depends(get_settings)):
3538

3639
@router.get("/__heartbeat__")
3740
@router.head("/__heartbeat__")
38-
def heartbeat(response: Response, actions: Actions = Depends(get_actions)):
41+
def heartbeat(response: Response, actions: _actions):
3942
"""Return status of backing services, as required by Dockerflow."""
4043
health_map = {
4144
"bugzilla": bugzilla.check_health(),
@@ -57,17 +60,17 @@ def lbheartbeat():
5760

5861

5962
@router.get("/__version__")
60-
def version(version_json=Depends(get_version)):
63+
def version(version_json: _version):
6164
"""Return version.json, as required by Dockerflow."""
6265
return version_json
6366

6467

6568
@router.post("/bugzilla_webhook")
6669
def bugzilla_webhook(
6770
request: Request,
71+
actions: _actions,
72+
settings: _settings,
6873
webhook_request: BugzillaWebhookRequest = Body(..., embed=False),
69-
actions: Actions = Depends(get_actions),
70-
settings: Settings = Depends(get_settings),
7174
):
7275
"""API endpoint that Bugzilla Webhook Events request"""
7376
webhook_request.rid = request.state.rid
@@ -80,8 +83,8 @@ def bugzilla_webhook(
8083

8184
@router.get("/whiteboard_tags/")
8285
def get_whiteboard_tags(
86+
actions: _actions,
8387
whiteboard_tag: Optional[str] = None,
84-
actions: Actions = Depends(get_actions),
8588
):
8689
"""API for viewing whiteboard_tags and associated data"""
8790
if existing := actions.get(whiteboard_tag):
@@ -103,8 +106,8 @@ def get_jira_projects():
103106
@router.get("/powered_by_jbi/", response_class=HTMLResponse)
104107
def powered_by_jbi(
105108
request: Request,
109+
actions: _actions,
106110
enabled: Optional[bool] = None,
107-
actions: Actions = Depends(get_actions),
108111
):
109112
"""API for `Powered By` endpoint"""
110113
context = {

0 commit comments

Comments
 (0)