Skip to content

Commit

Permalink
Add reports api
Browse files Browse the repository at this point in the history
  • Loading branch information
anetteu committed Feb 15, 2022
1 parent 5f4814d commit e73ba25
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
1 change: 0 additions & 1 deletion backend/src/flotilla/api/missions_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from flotilla_openapi.models.mission import Mission
from flotilla_openapi.models.problem_details import ProblemDetails
from requests import HTTPError, RequestException
from requests import Response as RequestResponse

from flotilla.api.authentication import authentication_scheme
from flotilla.services.echo import (
Expand Down
69 changes: 69 additions & 0 deletions backend/src/flotilla/api/reports_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from http import HTTPStatus
from logging import getLogger
from typing import List

from fastapi import APIRouter, Depends, Path, Response, Security
from flotilla_openapi.models.problem_details import ProblemDetails
from flotilla_openapi.models.report import Report
from pytest import Session

from flotilla.api.authentication import authentication_scheme
from flotilla.database.crud import DBException, read_report_by_id, read_reports
from flotilla.database.db import get_db
from flotilla.database.models import ReportDBModel

router = APIRouter()

logger = getLogger("api")

NOT_FOUND_DESCRIPTION = "Not Found - No report with given id"


@router.get(
"/reports",
responses={
HTTPStatus.OK.value: {
"model": List[Report],
"description": "Request successful",
},
},
tags=["Reports"],
summary="List all available reports on the asset",
dependencies=[Security(authentication_scheme)],
)
async def get_reports(
db: Session = Depends(get_db),
) -> List[Report]:
"""List all available reports on the asset."""
db_reports: List[ReportDBModel] = read_reports(db)
reports: List[Report] = [report.get_api_report() for report in db_reports]
return reports


@router.get(
"/reports/{report_id}",
responses={
HTTPStatus.OK.value: {
"model": Report,
"description": "Request successful",
},
},
tags=["Reports"],
summary="Lookup a single report",
)
async def get_report(
response: Response,
report_id: int = Path(None, description=""),
db: Session = Depends(get_db),
) -> Report:
"""Lookup the report with the specified id"""
try:
db_report: ReportDBModel = read_report_by_id(db=db, report_id=report_id)
report: Report = db_report.get_api_report()
except DBException:
logger.exception(f"Could not get report with id {report_id}.")
response.status_code = HTTPStatus.NOT_FOUND.value
return ProblemDetails(
title=NOT_FOUND_DESCRIPTION, status=HTTPStatus.NOT_FOUND.value
)
return report
14 changes: 14 additions & 0 deletions backend/src/flotilla/database/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ def read_robot_by_id(db: Session, robot_id: int) -> RobotDBModel:
return robot


def read_reports(db: Session) -> List[ReportDBModel]:
reports: List[ReportDBModel] = db.query(ReportDBModel).all()
return reports


def read_report_by_id(db: Session, report_id: int) -> ReportDBModel:
report: Optional[ReportDBModel] = (
db.query(ReportDBModel).filter(ReportDBModel.id == report_id).first()
)
if not report:
raise DBException(f"No report with id {report_id}")
return report


def create_report(
db: Session,
robot_id: int,
Expand Down
2 changes: 2 additions & 0 deletions backend/src/flotilla/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from flotilla.api.authentication import authenticator
from flotilla.api.missions_api import router as missions_router
from flotilla.api.reports_api import router as reports_router
from flotilla.api.robots_api import router as robots_router
from flotilla.config.log_config import LogConfig
from flotilla.database.db import Base, SessionLocal, engine
Expand Down Expand Up @@ -44,6 +45,7 @@ def startup_event():

app.include_router(robots_router)
app.include_router(missions_router)
app.include_router(reports_router)


if __name__ == "__main__":
Expand Down
25 changes: 25 additions & 0 deletions backend/tests/test_api/test_reports_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from http import HTTPStatus

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient


def test_get_reports(test_app: FastAPI):
with TestClient(test_app) as client:
response = client.get("/reports")
assert response.status_code == HTTPStatus.OK.value
assert len(response.json()) == 2


@pytest.mark.parametrize(
"report_id, expected_status_code",
[
(1, HTTPStatus.OK.value),
(3, HTTPStatus.NOT_FOUND.value),
],
)
def test_get_report(test_app: FastAPI, report_id: int, expected_status_code: int):
with TestClient(test_app) as client:
response = client.get(f"/reports/{report_id}")
assert response.status_code == expected_status_code
1 change: 0 additions & 1 deletion backend/tests/test_api/test_robots_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from fastapi.testclient import TestClient
from requests import RequestException, Response

from flotilla.database.models import ReportDBModel
from flotilla.services.isar import get_isar_service
from tests.mocks.isar_service_mock import IsarServiceMock

Expand Down

0 comments on commit e73ba25

Please sign in to comment.