-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using routers #99
Using routers #99
Conversation
All endpoints are now specified by FastAPI's APIRouter class. This means that we can create the same endpoints several times, which is useful for the different base URL's required by the OPTiMaDe specification, e.g., /optimade, /optimade/v0.10, ...
Codecov Report
@@ Coverage Diff @@
## master #99 +/- ##
==========================================
+ Coverage 84.99% 85.36% +0.36%
==========================================
Files 39 45 +6
Lines 2352 2425 +73
==========================================
+ Hits 1999 2070 +71
- Misses 353 355 +2
Continue to review full report at Codecov.
|
This is an extremely useful PR @CasperWA! I was trying to figure out how to do something similar with my implementation so I could just import most of the endpoints from the example server code. Is this ready to review? |
😄 🎉
Sure is 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am just as impressed after reading it as I was after skimming it, great work, definitely want to use these features ASAP (I assume this needs to be updated to include /links
when #95 has been merged, and not vice versa).
response_model=Union[ReferenceResponseMany, ErrorResponse], | ||
response_model_skip_defaults=True, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting lots of warnings about resposne_model_skip_defaults
from the new FastAPI, could be worth updating it here to expedite the upgrade to pydantic>1.
response_model_skip_defaults has been deprecated in favor of response_model_exclude_unset to keep in line with Pydantic v1, support for it will be removed soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: This keyword only works for fastapi>=0.44.0
. So we would need to pin this version as a minimum version if we use this keyword.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair, happy to leave it for big deps update PR then
router = APIRouter() | ||
|
||
structures_coll = MongoCollection( | ||
client[CONFIG.mongo_database]["structures"], StructureResource, StructureMapper |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'm still in favour of a CONFIG.structure_collection
that defaults to "structures"
, but that's very minor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is very back-end specific. We can definitely add a [MONGO]
section to the config, where it can be added. I also think we shouldn't use the [DEFAULT]
section, since it's special for config.ini
and it's fields will always be included in all other sections. This is a problem for the sections specifying provider-specific fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get your point, I guess I'm just being lazy by wanting to reuse even main.py
:P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I have actually updated this PR with all your suggested changes - check it out :)
optimade/server/utils.py
Outdated
provider=Provider(**provider), | ||
data_available=data_available, | ||
**kwargs, | ||
) | ||
|
||
|
||
def general_exception( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason this can't be moved to exception_handlers
(which I guess could renamed exceptions
)? Doesn't really fit with the other utils
submodules now. Also I noticed that we have models.util
, server.utils
and server.routers.utils
, please standardize this however you see fit (if server.utils
is going, probably better to rename to server.routers.util
as all imports of it will already be in this PR)!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason this can't be moved to
exception_handlers
(which I guess could renamedexceptions
)?
I indeed call it this in my own implementation. But I think it's a bit misleading, since one would most likely expect Python exceptions to be the content here...
Doesn't really fit with the other
utils
submodules now. Also I noticed that we havemodels.util
,server.utils
andserver.routers.utils
, please standardize this however you see fit (ifserver.utils
is going, probably better to rename toserver.routers.util
as all imports of it will already be in this PR)!
Ah, yeah. It should all be utils
. And I am indeed trying to move the functions around to the appropriate places.
optimade/server/routers/info.py
Outdated
api_version="v0.10", | ||
available_api_versions=[ | ||
{ | ||
"url": "http://localhost:5000/optimade/v0.10.0/", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we now use CONFIG.index_base_url
to figure out what this url should be?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a bad idea! :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: This should be put here, but rather, it should be used in the top-level meta
response under provider
- which it is automatically :)
What I have done in my own implementation, however, is to add version
to the config file. In this way it can be used here and retrieved whenever needed, without having to import the app
to get the version from there.
Up min. required dependency for fastapi to 0.44.0. This lets us begin to slowly update to be pydantic v1 ready. Change config.ini significantly, splitting it up in backend, implementation, and endpoints.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why Travis isn't reporting success back to GitHub, maybe if you make this one change it will work again?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cheers @CasperWA! Merge when you're ready.
All endpoints are now specified by FastAPI's APIRouter class.
This means that we can create the same endpoints several times, which is useful for the different base URL's required by the OPTiMaDe specification, e.g.,
/optimade
,/optimade/v0.10
, ...