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

Add api_hint query parameter #421

Merged

Conversation

CasperWA
Copy link
Member

@CasperWA CasperWA commented Jul 21, 2020

Closes #392

This parameter is handled in a Middleware.
This is because it can lead to changing the original request by moving to a versioned base URL.

We also need to include a new Exception, which is actually an HTTP response, similar to BadRequest, namely VersionNotSupported (553).

The addition of the parameter to query_params.py is merely for adding it to the OpenAPI specification.


Missing:

  • Tests
  • Add warnings
  • Add better doc strings

@codecov
Copy link

codecov bot commented Jul 21, 2020

Codecov Report

Merging #421 into master will increase coverage by 0.14%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #421      +/-   ##
==========================================
+ Coverage   91.45%   91.60%   +0.14%     
==========================================
  Files          60       60              
  Lines        2751     2800      +49     
==========================================
+ Hits         2516     2565      +49     
  Misses        235      235              
Flag Coverage Δ
#unittests 91.60% <100.00%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
optimade/server/query_params.py 100.00% <ø> (ø)
...made/server/entry_collections/entry_collections.py 98.82% <100.00%> (ø)
optimade/server/main.py 96.07% <100.00%> (+0.07%) ⬆️
optimade/server/main_index.py 96.07% <100.00%> (+0.07%) ⬆️
optimade/server/middleware.py 94.93% <100.00%> (+2.07%) ⬆️
optimade/server/routers/utils.py 91.15% <100.00%> (+0.06%) ⬆️
optimade/server/warnings.py 85.71% <100.00%> (ø)

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 c4e6f3f...11fbb9b. Read the comment docs.

@CasperWA CasperWA force-pushed the close_392_api_hint-query-parameter branch 3 times, most recently from 6a999d8 to 68d382f Compare July 22, 2020 23:17
@CasperWA
Copy link
Member Author

So I've been slightly rewriting according to some misinterpretation on my part, I think.
I understood the api_hint as a form of redirect, but I think this is not the case.
Instead it should simply serve the response as best it can without redirecting, but it may do so underneath the hood though, however, a proper redirect / forwarded response should never be returned, i.e., no 30X responses.

How do you understand this?

The issues I was having was that I was using a RedirectResponse to redirect to the versioned base URLs, but this would then resolve in headers not being forwarded as well. And now I've been trying to muck about trying to get that work and it dawned on me that I don't need to actually have a RedirectResponse...

@CasperWA CasperWA requested a review from ml-evs July 24, 2020 12:49
@CasperWA CasperWA marked this pull request as ready for review July 24, 2020 12:49
@CasperWA
Copy link
Member Author

So I've been slightly rewriting according to some misinterpretation on my part, I think.
I understood the api_hint as a form of redirect, but I think this is not the case.
Instead it should simply serve the response as best it can without redirecting, but it may do so underneath the hood though, however, a proper redirect / forwarded response should never be returned, i.e., no 30X responses.

How do you understand this?

The issues I was having was that I was using a RedirectResponse to redirect to the versioned base URLs, but this would then resolve in headers not being forwarded as well. And now I've been trying to muck about trying to get that work and it dawned on me that I don't need to actually have a RedirectResponse...

I've kept in both solutions for now.
I also just realized that sending headers in a request does of course not also mean that one should get those headers in the response ...

Currently, I've implemented the "cleaner" solution of using a RedirectResponse to visibly show the redirect.

@CasperWA CasperWA force-pushed the close_392_api_hint-query-parameter branch from 346c0fa to 47f6a16 Compare July 30, 2020 21:35
shyamd
shyamd previously approved these changes Jul 31, 2020
Copy link
Contributor

@shyamd shyamd left a comment

Choose a reason for hiding this comment

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

Two lazy-loaded imports I have an issue with, but otherwise, this looks good.

optimade/server/middleware.py Outdated Show resolved Hide resolved
optimade/server/middleware.py Show resolved Hide resolved
@CasperWA
Copy link
Member Author

@shyamd I have moved all imports to the top-level of the middleware file now. It passed all tests locally. Let's see what happens here now with the CI.

@CasperWA CasperWA requested a review from shyamd July 31, 2020 14:50
shyamd
shyamd previously approved these changes Jul 31, 2020
This parameter is handled in a Middleware.
This is because it can lead to changing the original request by moving
to a versioned base URL.

We also need to include a new Exception, which is actually an HTTP
response, similar to BadRequest, namely VersionNotSupported (553).

The addition of the parameter to `query_params.py` is merely for adding
it to the OpenAPI specification.
Fix HandleApiHint according to mistakes found from tests.
Also, updated a Warning name used in sort parameter.
And fix attributes in collections by moving them from MongoCollection to
EntryCollection.
Move `AddWarning` middleware to be the last added middleware.
This is to ensure it becomes the first custom middleware invoked, in
order to override the `warnings.showwarning` function.
This found a mistake in the middleware, where `None` returns from
`handle_api_hint()` was not handled. This has been fixed.

Also, add extra parsing of values to support `,`-separated list values.
@CasperWA CasperWA requested review from shyamd and ml-evs July 31, 2020 16:09
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.

LGTM now! Cheers @CasperWA

@CasperWA CasperWA merged commit 033800d into Materials-Consortia:master Jul 31, 2020
@CasperWA CasperWA deleted the close_392_api_hint-query-parameter branch July 31, 2020 21:15
ml-evs pushed a commit that referenced this pull request Aug 3, 2020
* Add `api_hint` query parameter:

This parameter is handled in a middleware.
This is because it can lead to changing the original request by moving
to a versioned base URL.

The addition of the parameter to `query_params.py` is merely for adding
it to the OpenAPI specification.

* Split middleware tests into separate files.

* Fix middleware inclusion sequence:

Move `AddWarning` middleware to be the last added middleware.
This is to ensure it becomes the first custom middleware invoked, in
order to override the `warnings.showwarning` function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add api_hint query parameter
3 participants