Skip to content

Commit

Permalink
Add additional param to the BaseService class to allow for passing ot…
Browse files Browse the repository at this point in the history
…her licenses to FastAPI (#526)

* added optional parameter to the BaseService class to accept either the DIBBs default Creative Commons Zero v1.0 or the MIT license from a new LicenseType class 
* added a test to pass a both the DIBBs default and MIT licenses to the BaseService
  • Loading branch information
robertmitchellv authored May 5, 2023
1 parent 4fd35ac commit 6632a53
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
33 changes: 24 additions & 9 deletions phdi/containers/base_service.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
from fastapi import FastAPI
from pathlib import Path
from enum import Enum
from importlib import metadata


# create a class with the DIBBs default Creative Commons Zero v1.0 and
# MIT license to be used by the BaseService class
class LicenseType(Enum):
CreativeCommonsZero = {
"name": "Creative Commons Zero v1.0 Universal",
"url": "https://creativecommons.org/publicdomain/zero/1.0/",
}
MIT = {"name": "The MIT License", "url": "https://mit-license.org/"}


class BaseService:
"""
Base class for all DIBBs services. This class provides a FastAPI instance with DIBBs
metadata and optionally a health check endpoint.
"""

LICENSE_INFO = LicenseType.CreativeCommonsZero
DIBBS_CONTACT = {
"name": "CDC Public Health Data Infrastructure",
"url": "https://cdcgov.github.io/phdi-site/",
"email": "dmibuildingblocks@cdc.gov",
}

def __init__(
self,
service_name: str,
description_path: str,
include_health_check_endpoint: bool = True,
license_info: LicenseType = LICENSE_INFO,
):
"""
Initialize a BaseService instance.
Expand All @@ -23,21 +42,17 @@ def __init__(
the service.
:param include_health_check_endpoint: If True, the standard DIBBs health check
endpoint will be added.
:param license_info: If empty, the standard DIBBs Creative Commons Zero v1.0
Universal license will be used. The other available option is to use the
MIT license.
"""
description = Path(description_path).read_text(encoding="utf-8")
self.include_health_check_endpoint = include_health_check_endpoint
self.app = FastAPI(
title=service_name,
version=metadata.version("phdi"),
contact={
"name": "CDC Public Health Data Infrastructure",
"url": "https://cdcgov.github.io/phdi-site/",
"email": "dmibuildingblocks@cdc.gov",
},
license_info={
"name": "Creative Commons Zero v1.0 Universal",
"url": "https://creativecommons.org/publicdomain/zero/1.0/",
},
contact=self.DIBBS_CONTACT,
license_info=license_info,
description=description,
)

Expand Down
40 changes: 28 additions & 12 deletions tests/containers/test_base_service.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
from phdi.containers.base_service import BaseService
from phdi.containers.base_service import LicenseType, BaseService
from fastapi.testclient import TestClient
from pathlib import Path
from importlib import metadata

default_app_version = metadata.version("phdi")
default_app_contact = BaseService.DIBBS_CONTACT
default_app_license = LicenseType.CreativeCommonsZero
alternate_app_license = LicenseType.MIT


def test_base_service():
service = BaseService(
"test_service", Path(__file__).parent.parent / "assets" / "test_description.md"
service_name="test_service",
description_path=Path(__file__).parent.parent
/ "assets"
/ "test_description.md",
)
assert service.app.title == "test_service"
assert service.app.version == metadata.version("phdi")
assert service.app.contact == {
"name": "CDC Public Health Data Infrastructure",
"url": "https://cdcgov.github.io/phdi-site/",
"email": "dmibuildingblocks@cdc.gov",
}
assert service.app.license_info == {
"name": "Creative Commons Zero v1.0 Universal",
"url": "https://creativecommons.org/publicdomain/zero/1.0/",
}
assert service.app.version == default_app_version
assert service.app.contact == default_app_contact
assert service.app.license_info == default_app_license
assert service.app.description == "This is a test description."

client = TestClient(service.start())
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"status": "OK"}


def test_base_service_alternate_license():
service = BaseService(
service_name="test_service",
description_path=Path(__file__).parent.parent
/ "assets"
/ "test_description.md",
license_info=alternate_app_license,
)
assert service.app.title == "test_service"
assert service.app.version == default_app_version
assert service.app.contact == default_app_contact
assert service.app.license_info == alternate_app_license
assert service.app.description == "This is a test description."

0 comments on commit 6632a53

Please sign in to comment.