Skip to content
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

Merged
merged 7 commits into from
Dec 2, 2019
Merged

Using routers #99

merged 7 commits into from
Dec 2, 2019

Conversation

CasperWA
Copy link
Member

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, ...

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, ...
@CasperWA CasperWA added the enhancement New feature or request label Nov 28, 2019
@codecov
Copy link

codecov bot commented Nov 28, 2019

Codecov Report

Merging #99 into master will increase coverage by 0.36%.
The diff coverage is 79.25%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
optimade/models/utils.py 87.5% <ø> (ø)
optimade/models/baseinfo.py 92.59% <ø> (ø) ⬆️
optimade/validator/__init__.py 13.04% <ø> (ø) ⬆️
optimade/server/routers/utils.py 84.03% <100%> (ø)
optimade/models/tests/test_models.py 100% <100%> (ø) ⬆️
optimade/models/structures.py 83.33% <100%> (ø) ⬆️
optimade/server/routers/__init__.py 100% <100%> (ø)
optimade/server/routers/structures.py 100% <100%> (ø)
optimade/models/__init__.py 100% <100%> (ø) ⬆️
optimade/server/main.py 93.87% <100%> (+19.83%) ⬆️
... and 16 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bcd2b72...ca19f54. Read the comment docs.

@ml-evs
Copy link
Member

ml-evs commented Nov 28, 2019

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?

@CasperWA
Copy link
Member Author

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 👍

Copy link
Member

@ml-evs ml-evs left a 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).

optimade/server/main.py Outdated Show resolved Hide resolved
Comment on lines 29 to 30
response_model=Union[ReferenceResponseMany, ErrorResponse],
response_model_skip_defaults=True,
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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
Copy link
Member

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.

Copy link
Member Author

@CasperWA CasperWA Nov 29, 2019

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.

Copy link
Member

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

Copy link
Member Author

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/tests/test_server.py Show resolved Hide resolved
optimade/server/tests/test_server.py Outdated Show resolved Hide resolved
provider=Provider(**provider),
data_available=data_available,
**kwargs,
)


def general_exception(
Copy link
Member

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)!

Copy link
Member Author

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)?

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 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)!

Ah, yeah. It should all be utils. And I am indeed trying to move the functions around to the appropriate places.

api_version="v0.10",
available_api_versions=[
{
"url": "http://localhost:5000/optimade/v0.10.0/",
Copy link
Member

@ml-evs ml-evs Nov 28, 2019

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a bad idea! :)

Copy link
Member Author

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.

@CasperWA
Copy link
Member Author

(...) (I assume this needs to be updated to include /links when #95 has been merged, and not vice versa).

Yeah, I think it's more prudent to wait merging this until we've added /links, i.e., merged #95.

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.
@CasperWA CasperWA requested a review from ml-evs December 1, 2019 15:13
@ml-evs
Copy link
Member

ml-evs commented Dec 1, 2019

Thanks for these changes @CasperWA, I'll accept once we've merged #95.

@ml-evs ml-evs mentioned this pull request Dec 1, 2019
@CasperWA CasperWA mentioned this pull request Dec 2, 2019
Copy link
Member

@ml-evs ml-evs left a 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?

optimade/server/routers/links.py Outdated Show resolved Hide resolved
@CasperWA CasperWA requested a review from ml-evs December 2, 2019 13:56
Copy link
Member

@ml-evs ml-evs left a 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.

@CasperWA CasperWA merged commit 6675838 into master Dec 2, 2019
@CasperWA CasperWA deleted the use_routers branch December 2, 2019 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants