Adds config files for the API#104
Conversation
Benjamin-Nussbaum
left a comment
There was a problem hiding this comment.
The only comment I have is to consider using pydantic_settings, which should handle a lot of the boilerplate here. https://docs.pydantic.dev/latest/concepts/pydantic_settings/
|
Not entirely sure why it says I dismissed your review but I have updated it to use |
| from pqnstack.constants import QKDEncodingBasis | ||
| from pqnstack.pqn.protocols.measurement import MeasurementConfig | ||
|
|
||
| load_dotenv() |
There was a problem hiding this comment.
I don't think this is necessary since pydantic-settings has dotenv support - see https://docs.pydantic.dev/latest/concepts/pydantic_settings/#dotenv-env-support
| def get_settings() -> Settings: | ||
| raise NotImplementedError(static_typecheck_msg) | ||
| return Settings() |
There was a problem hiding this comment.
Consider using @lru_cache from functools https://fastapi.tiangolo.com/advanced/settings/?h=settings#creating-the-settings-only-once-with-lru_cache
There was a problem hiding this comment.
Since we are simply importing the settings object and using it there, the get_settings() only gets called once already. I don't think most people know about that function so I don't want to add more complexity for basically no new functionality
There was a problem hiding this comment.
If we have dependencies with other endpoints in addition to the config, it might add to the complexity of why one is a (Annotated) Depends dpendency and another is directly imported. If we want to directly import, the get_settings() function itself is unnecessary; we can just instantiate the Settings object at the module level directly. But it makes more sense to me to stick to the FastAPI standard using Depends, in which case I also recommend using @lru_cache.
| "pydantic>=2.11.7", | ||
| "pydantic-settings>=2.6.0", |
There was a problem hiding this comment.
pydantic is a dependency of pydantic-settings (it should be an option, but that's not how they set it up). Anyway, the separate pydantic dependency here is not necessary with pydantic-settings.
There was a problem hiding this comment.
That was the case with the first version of pydantic but they split it up for the 2.0 version the info box in this section
There was a problem hiding this comment.
There was a problem hiding this comment.
Maybe this example should be in ./configs/? Or perhaps a new ./examples/ directory?
There was a problem hiding this comment.
done, I have also added the messaging config example as well
| "pydantic>=2.11.7", | ||
| "pydantic-settings>=2.6.0", |
There was a problem hiding this comment.
| @@ -31,6 +31,7 @@ webapp = [ | |||
| "fastapi[standard]>=0.115.14", | |||
| "httpx>=0.28.1", | |||
| "pydantic>=2.11.7", | |||
There was a problem hiding this comment.
| "pydantic>=2.11.7", |
| def get_settings() -> Settings: | ||
| raise NotImplementedError(static_typecheck_msg) | ||
| return Settings() |
There was a problem hiding this comment.
If we have dependencies with other endpoints in addition to the config, it might add to the complexity of why one is a (Annotated) Depends dpendency and another is directly imported. If we want to directly import, the get_settings() function itself is unnecessary; we can just instantiate the Settings object at the module level directly. But it makes more sense to me to stick to the FastAPI standard using Depends, in which case I also recommend using @lru_cache.
|
ok I think that was all the suggestions. If it looks good can we merge @Benjamin-Nussbaum ? |
Benjamin-Nussbaum
left a comment
There was a problem hiding this comment.
@lru_cache should be on get_settings(), not the Settings object itself. https://fastapi.tiangolo.com/advanced/settings/?h=settings#creating-the-settings-only-once-with-lru_cache
Otherwise looks good
|
|
||
| def get_settings() -> Settings: |
There was a problem hiding this comment.
| def get_settings() -> Settings: | |
| @lru_cache | |
| def get_settings() -> Settings: |
| router_port: int | ||
| chsh_settings: CHSHSettings | ||
| qkd_settings: QKDSettings | ||
| @lru_cache |
There was a problem hiding this comment.
| @lru_cache |
There was a problem hiding this comment.
that was dumb of me 😅
135a3b7 to
6c3b868
Compare
6c3b868 to
e854456
Compare
The server looks for a file called
config.tomlwherever is running, and if it cannot find it there it looks for a file specified in the environment variableAPI_CONFIG_PATHConfig files should look something like this: