-
Notifications
You must be signed in to change notification settings - Fork 965
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(deps): include pyramid_openapi3 (#15553)
- Loading branch information
1 parent
081dffd
commit 8f79d90
Showing
12 changed files
with
582 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import orjson | ||
import pretend | ||
import pytest | ||
|
||
from warehouse.api import config | ||
|
||
|
||
def test_api_set_content_type(): | ||
response = pretend.stub() | ||
|
||
@pretend.call_recorder | ||
def view(context, request): | ||
return response | ||
|
||
info = pretend.stub(options={"api_version": "v1"}) | ||
wrapped_view = config._api_set_content_type(view, info) | ||
|
||
context = pretend.stub() | ||
request = pretend.stub(response=pretend.stub(content_type=None)) | ||
|
||
assert wrapped_view(context, request) is response | ||
assert request.response.content_type == "application/vnd.pypi.v1+json" | ||
|
||
|
||
def test_api_set_content_type_no_api_version(): | ||
response = pretend.stub() | ||
|
||
@pretend.call_recorder | ||
def view(context, request): | ||
return response | ||
|
||
info = pretend.stub(options={}) | ||
wrapped_view = config._api_set_content_type(view, info) | ||
|
||
context = pretend.stub() | ||
request = pretend.stub(response=pretend.stub(content_type=None)) | ||
|
||
assert wrapped_view(context, request) is response | ||
assert request.response.content_type is None | ||
|
||
|
||
@pytest.mark.parametrize("env_name", ["development", "production"]) | ||
def test_includeme(monkeypatch, env_name): | ||
# We use `str(Path(__file__).parent / 'openapi.yaml'` to get the path. | ||
# In our test, monkeypatch to a known value. | ||
monkeypatch.setattr(config, "__file__", "/mnt/dummy/config.py") | ||
|
||
conf = pretend.stub( | ||
add_view_deriver=pretend.call_recorder( | ||
lambda deriver, over=None, under=None: None | ||
), | ||
include=pretend.call_recorder(lambda x: None), | ||
pyramid_openapi3_spec=pretend.call_recorder(lambda *a, **kw: None), | ||
pyramid_openapi3_add_deserializer=pretend.call_recorder(lambda *a, **kw: None), | ||
pyramid_openapi3_add_explorer=pretend.call_recorder(lambda *a, **kw: None), | ||
registry=pretend.stub(settings={"warehouse.env": env_name}), | ||
) | ||
|
||
config.includeme(conf) | ||
|
||
assert conf.add_view_deriver.calls == [pretend.call(config._api_set_content_type)] | ||
assert conf.include.calls == [pretend.call("pyramid_openapi3")] | ||
assert conf.pyramid_openapi3_spec.calls == [ | ||
pretend.call("/mnt/dummy/openapi.yaml", route="/api/openapi.yaml") | ||
] | ||
assert conf.pyramid_openapi3_add_deserializer.calls == [ | ||
pretend.call("application/vnd.pypi.api-v0-danger+json", orjson.loads) | ||
] | ||
if env_name == "development": | ||
assert conf.pyramid_openapi3_add_explorer.calls == [ | ||
pretend.call(route="/api/explorer/") | ||
] | ||
else: | ||
assert not conf.pyramid_openapi3_add_explorer.calls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
Configuration for the warehouse API | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
import typing | ||
|
||
from pathlib import Path | ||
|
||
import orjson | ||
|
||
from warehouse.config import Environment | ||
|
||
if typing.TYPE_CHECKING: | ||
from pyramid.config import Configurator | ||
|
||
|
||
def _api_set_content_type(view, info): | ||
""" | ||
Set the content type based on API version parameter. | ||
Use in a `@view_config` decorator like so: | ||
@view_config(renderer="json", api_version="v1", ...) | ||
def my_view(request): | ||
return {"hello": "world"} | ||
This will set the content type to `application/vnd.pypi.v1+json` and | ||
pass to whatever `json` renderer is configured. | ||
""" | ||
if api_version := info.options.get("api_version"): # pragma: no cover | ||
|
||
def wrapper(context, request): | ||
request.response.content_type = f"application/vnd.pypi.{api_version}+json" | ||
return view(context, request) | ||
|
||
return wrapper | ||
return view | ||
|
||
|
||
_api_set_content_type.options = ("api_version",) # type: ignore[attr-defined] | ||
|
||
|
||
def includeme(config: Configurator) -> None: | ||
config.add_view_deriver(_api_set_content_type) | ||
|
||
# Set up OpenAPI | ||
config.include("pyramid_openapi3") | ||
config.pyramid_openapi3_spec( | ||
str(Path(__file__).parent / "openapi.yaml"), | ||
route="/api/openapi.yaml", | ||
) | ||
# We use vendor prefixes to indicate the API version, so we need to add | ||
# deserializers for each version. | ||
config.pyramid_openapi3_add_deserializer( | ||
"application/vnd.pypi.api-v0-danger+json", orjson.loads | ||
) | ||
if config.registry.settings["warehouse.env"] == Environment.development: | ||
# Set up the route for the OpenAPI Web UI | ||
config.pyramid_openapi3_add_explorer(route="/api/explorer/") | ||
|
||
# Helpful toggles for development. | ||
# config.registry.settings["pyramid_openapi3.enable_endpoint_validation"] = False | ||
# config.registry.settings["pyramid_openapi3.enable_request_validation"] = False | ||
# config.registry.settings["pyramid_openapi3.enable_response_validation"] = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.