cylon86
cylon86

Reputation: 634

fastapi - import config from main.py

I'm new to fastapi, which is really great so far, but I struggle to find a clean way to import my app config to in another module.

EDIT: I need to be able to change the config when running unit test

Here is my dir tree:

/app
| __init__.py
| /router
| | __init__.py
| | my_router.py
| /test
| | test_api.py
| config.py
| main.py

Here is my main.py file:

from functools import lru_cache

from fastapi import FastAPI

from .router import my_router
from . import config

app = FastAPI()

app.include_router(
    my_router.router,
    prefix="/r",
    tags=["my-router"],
)


@lru_cache()
def get_setting():
    return config.Settings(admin_email="[email protected]")


@app.get('/')
def hello():
    return 'Hello world'

Here is the router.py:

from fastapi import APIRouter

from ..main import get_setting

router = APIRouter()

@router.get('/test')
def get_param_list(user_id: int):
    config = get_setting()
    return 'Import Ok'

And here is the config file

from pydantic import BaseSettings


class Settings(BaseSettings):
    param_folder: str = "param"
    result_folder: str = "output"

    class Config:
        env_prefix = "APP_"

Then runing uvicorn app.main:app --reload I got : ERROR: Error loading ASGI app. Could not import module "app.main". I guess because of a kind of circular import. But then I don't how to pass my config to my router ?

Thanks for your help :)

Upvotes: 6

Views: 13374

Answers (3)

peter
peter

Reputation: 41

The only draw back with this is that I must add the setting: config.Setting = Depends(config.get_setting), which is quite "heavy", to every function call that needs the setting.

You can use Class Based Views from the fastapi_utils package:

from fastapi import APIRouter, Depends
from fastapi_utils.cbv import cbv
from starlette import requests
from logging import Logger
from .. import config

router = APIRouter()

@cbv(router)
class MyQueryCBV:
    settings: config.Setting = Depends(config.get_setting)  # you can introduce settings dependency here

    def __init__(self, r: requests.Request):  # called for each query
        self.logger: Logger = self.settings.logger
        self.logger.warning(str(r.headers))

    @router.get('/test')
    def get_param_list(self, user_id: int)
        self.logger.warning(f"get_param_list: {user_id}")
        return self.settings

    @router.get("/test2")
    def get_param_list2(self):
        self.logger.warning(f"get_param_list2")
        return self.settings

Upvotes: 1

Kassym Dorsel
Kassym Dorsel

Reputation: 4843

How about setting up the lru cache directly inside the config.py.

from functools import lru_cache
from pydantic import BaseSettings


class Settings(BaseSettings):
    admin_email: str = "[email protected]"
    param_folder: str = "param"
    result_folder: str = "output"

    class Config:
        env_prefix = "APP_"

@lru_cache()
def get_setting():
    return Settings()

And my_router.py

from fastapi import APIRouter, Depends

from ..config import Settings, get_setting

router = APIRouter()

@router.get('/test')
def get_param_list(config: Settings = Depends(get_setting)):
    return config

And test.py

from fastapi.testclient import TestClient

from . import config, main

client = TestClient(main.app)


def get_settings_override():
    return config.Settings(admin_email="[email protected]")


main.app.dependency_overrides[config.get_settings] = get_settings_override

def test_app():
    response = client.get("/r/test")
    data = response.json()
    assert data == config.Settings(admin_email="[email protected]")

Upvotes: 5

cylon86
cylon86

Reputation: 634

I got it working using the FastAPI Dependency system and, as suggested by @Kassym Dorsel, by moving the lru_cache to the config.py.

The only draw back with this is that I must add the setting: config.Setting = Depends(config.get_setting), which is quite "heavy", to every function call that needs the setting.

Here is how I did it:

config.py file:

from pydantic import BaseSettings


class Settings(BaseSettings):
    param_folder: str = "param"
    result_folder: str = "output"

    class Config:
        env_prefix = "APP_"

@lru_cache()
def get_setting():
    return config.Settings(admin_email="[email protected]")

main.py file:

from functools import lru_cache

from fastapi import FastAPI

from .router import my_router

app = FastAPI()

app.include_router(
    my_router.router,
    prefix="/r",
    tags=["my-router"],
)

@app.get('/')
def hello():
    return 'Hello world'

router.py file:

from fastapi import APIRouter, Depends

from .. import config

router = APIRouter()

@router.get('/test')
def get_param_list(user_id: int, setting: config.Setting = Depends(config.get_setting)):
    return setting

This way I can use the dependency_overrides in my test_api.py to change the config for the test:

from fastapi.testclient import TestClient

from .. import config, server

client = TestClient(server.app)

TEST_PARAM_FOLDER = 'server/test/param'
TEST_RESULT_FOLDER = 'server/test/result'

def get_setting_override():
    return config.Setting(param_folder=TEST_PARAM_FOLDER, result_folder=TEST_RESULT_FOLDER)


server.app.dependency_overrides[config.get_setting] = get_setting_override

def test_1():
    ...

Upvotes: 0

Related Questions