Skip to content
Snippets Groups Projects
Commit 68ff9548 authored by Erik Reid's avatar Erik Reid
Browse files

ruffed and typing

parent 6699c096
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ from datetime import datetime ...@@ -7,6 +7,7 @@ from datetime import datetime
import json import json
import os import os
import sys import sys
import tempfile
sys.path.insert( sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) 0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
...@@ -53,7 +54,19 @@ html_theme = 'sphinx_rtd_theme' ...@@ -53,7 +54,19 @@ html_theme = 'sphinx_rtd_theme'
html_static_path = ['_static'] html_static_path = ['_static']
api_schema = create_app().openapi() # we need a minimal/parseable config file in order to
# start the server and dump the schema
with tempfile.NamedTemporaryFile(delete=False) as f:
bogus_config = {'inventory': 'http://bogus'}
with open(f.name, 'w') as f:
json.dump(bogus_config, f)
f.flush()
os.environ['SETTINGS_FILENAME'] = f.name
api_schema = create_app().openapi()
openapi_filename = os.path.join(os.path.dirname(__file__), "openapi.json") openapi_filename = os.path.join(os.path.dirname(__file__), "openapi.json")
with open(openapi_filename, 'w') as f: with open(openapi_filename, 'w') as f:
json.dump(api_schema, f, indent=4) json.dump(api_schema, f, indent=4)
...@@ -4,16 +4,19 @@ Default entry point for the FastAPI application. ...@@ -4,16 +4,19 @@ Default entry point for the FastAPI application.
from fastapi import FastAPI from fastapi import FastAPI
from mapping_provider import config, environment
from mapping_provider.api import common, map from mapping_provider.api import common, map
from mapping_provider import config
from mapping_provider import environment
def create_app() -> FastAPI: def create_app() -> FastAPI:
""" """
Creates the FastAPI application instance, with routers attached. Creates the FastAPI application instance, with routers attached.
""" """
environment.setup_logging() environment.setup_logging()
environment.setup_sentry(config.load().sentry)
app_config = config.load()
if app_config.sentry:
environment.setup_sentry(app_config.sentry)
app = FastAPI( app = FastAPI(
title="Mapping provider", title="Mapping provider",
......
import os import os
from pydantic import BaseModel, Field, HttpUrl from pydantic import BaseModel, Field, HttpUrl
class SentryConfig(BaseModel): class SentryConfig(BaseModel):
dsn: str dsn: str
environment: str environment: str
...@@ -16,44 +18,11 @@ class SentryConfig(BaseModel): ...@@ -16,44 +18,11 @@ class SentryConfig(BaseModel):
# return v # return v
class Configuration(BaseModel): class Configuration(BaseModel):
sentry: SentryConfig sentry: SentryConfig | None = None
inventory: HttpUrl inventory: HttpUrl
# CONFIG_SCHEMA = { def load() -> Configuration:
# '$schema': 'https://json-schema.org/draft/2020-12/schema',
# 'definitions': {
# 'sentry-info': {
# 'type': 'object',
# 'properties': {
# 'dsn': {'type': 'string'},
# 'environment': {'type': 'string'},
# 'level': {
# 'type': 'string',
# 'enum': ['debug', 'info', 'warning', 'error'],
# 'default': DEFAULT_SENTRY_LOG_LEVEL
# }
# },
# 'required': ['dsn'],
# 'additionalProperties': False,
# },
# },
# 'type': 'object',
# 'properties': {
# 'sentry': {'$ref': '#/definitions/sentry-info'},
# 'inventory': {
# 'type': 'string',
# 'format': 'uri',
# }
# },
# 'required': ['sentry', 'inventory'],
# 'additionalProperties': False
# }
def load():
""" """
Loads, validates and returns configuration parameters from Loads, validates and returns configuration parameters from
the file named in the environment variable 'SETTINGS_FILENAME'. the file named in the environment variable 'SETTINGS_FILENAME'.
......
...@@ -8,8 +8,8 @@ Environment setup ...@@ -8,8 +8,8 @@ Environment setup
""" """
import json import json
import logging.config
import logging import logging
import logging.config
import os import os
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
...@@ -19,7 +19,6 @@ if TYPE_CHECKING: ...@@ -19,7 +19,6 @@ if TYPE_CHECKING:
import sentry_sdk import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration from sentry_sdk.integrations.logging import LoggingIntegration
LOGGING_DEFAULT_CONFIG = { LOGGING_DEFAULT_CONFIG = {
'version': 1, 'version': 1,
'disable_existing_loggers': False, 'disable_existing_loggers': False,
...@@ -55,7 +54,7 @@ LOGGING_DEFAULT_CONFIG = { ...@@ -55,7 +54,7 @@ LOGGING_DEFAULT_CONFIG = {
} }
def setup_logging(): def setup_logging() -> None:
""" """
Sets up logging using the configured filename. Sets up logging using the configured filename.
...@@ -71,15 +70,19 @@ def setup_logging(): ...@@ -71,15 +70,19 @@ def setup_logging():
logging.config.dictConfig(logging_config) logging.config.dictConfig(logging_config)
def setup_sentry(sentry_config_params: 'SentryConfig'): def setup_sentry(sentry_config_params: 'SentryConfig') -> None:
""" """
Sets up the sentry instrumentation based on the Configuration.sentry params. Sets up the sentry instrumentation based on the Configuration.sentry params.
""" """
match str(sentry_config_params.level): match str(sentry_config_params.level):
case 'debug': level = logging.DEBUG case 'debug':
case 'info': level = logging.INFO level = logging.DEBUG
case 'warning': level = logging.WARNING case 'info':
case 'error': level = logging.ERROR level = logging.INFO
case 'warning':
level = logging.WARNING
case 'error':
level = logging.ERROR
sentry_sdk.init( sentry_sdk.init(
dsn=sentry_config_params.dsn, dsn=sentry_config_params.dsn,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment