Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Add PATH_PREFIX configuration #1136

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions api/catalog/api/examples/audio_requests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from django.urls import reverse_lazy


token = os.getenv("AUDIO_REQ_TOKEN", "DLBYIcfnKfolaXKcmMC8RIDCavc2hW")
origin = os.getenv("AUDIO_REQ_ORIGIN", "https://api.openverse.engineering")
Expand All @@ -25,7 +27,7 @@
# Example {index}: Search for audio {purpose}
curl \\
{auth} \\
"{origin}/v1/audio/?q={syntax}"
"{origin}{reverse('audio-list')}?q={syntax}"
"""
for (index, (purpose, syntax)) in enumerate(syntax_examples.items())
)
Expand All @@ -34,28 +36,28 @@
# Search for music titled "Wish You Were Here" by The.madpix.project
curl \\
{auth} \\
"{origin}/v1/audio/?title=Wish%20You%20Were%20Here&creator=The.madpix.project"
"{origin}{reverse('audio-list')}?title=Wish%20You%20Were%20Here&creator=The.madpix.project"
"""

audio_stats_curl = f"""
# Get the statistics for audio sources
curl \\
{auth} \\
"{origin}/v1/audio/stats/"
"{origin}{reverse('audio-list')}stats/"
"""

audio_detail_curl = f"""
# Get the details of audio ID {identifier}
curl \\
{auth} \\
"{origin}/v1/audio/{identifier}/"
"{origin}{reverse('audio-list')}{identifier}/"
"""

audio_related_curl = f"""
# Get related audio files for audio ID {identifier}
curl \\
{auth} \\
"{origin}/v1/audio/{identifier}/related/"
"{origin}{reverse('audio-list')}{identifier}/related/"
"""

audio_complain_curl = f"""
Expand All @@ -65,5 +67,5 @@
-H "Content-Type: application/json" \\
{auth} \\
-d '{{"reason": "mature", "description": "This audio contains sensitive content"}}' \\
"{origin}/v1/audio/{identifier}/report/"
"{origin}{reverse('audio-list')}{identifier}/report/"
"""
15 changes: 8 additions & 7 deletions api/catalog/api/examples/audio_responses.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os

from django.conf import settings
from django.urls import reverse

origin = os.getenv("AUDIO_REQ_ORIGIN", "https://api.openverse.engineering")

identifier = "8624ba61-57f1-4f98-8a85-ece206c319cf"

Expand Down Expand Up @@ -49,10 +50,10 @@
"duration": 270000,
"bit_rate": 128000,
"sample_rate": 44100,
"thumbnail": f"{origin}/v1/audio/{identifier}/thumb/",
"detail_url": f"{origin}/v1/audio/{identifier}/",
"related_url": f"{origin}/v1/audio/{identifier}/related/",
"waveform": f"{origin}/v1/audio/{identifier}/waveform/",
"thumbnail": f"{settings.BASE_URL}{reverse('audio-thumbnail', identifier=idenfitier)}",
"detail_url": f"{settings.BASE_URL}{reverse('audio-retrieve', identifier=identifier)}",
"related_url": f"{settings.BASE_URL}{reverse('audio-related', identifier=identifier)}",
"waveform": f"{settings.BASE_URL}{reverse('audio-waveform', identifier=identifier)}",
}

audio_search_200_example = {
Expand Down Expand Up @@ -122,8 +123,8 @@
"license_version": "2.0",
"license_url": "https://creativecommons.org/licenses/by-sa/2.0/",
"foreign_landing_url": "https://commons.wikimedia.org/w/index.php?curid=3536953", # noqa: E501
"detail_url": "http://api.openverse.engineering/v1/audio/36537842-b067-4ca0-ad67-e00ff2e06b2e", # noqa: E501
"related_url": "http://api.openverse.engineering/v1/recommendations/audio/36537842-b067-4ca0-ad67-e00ff2e06b2e", # noqa: E501
"detail_url": f"{settings.BASE_URL}{reverse('audio-retrieve', identifier='36537842-b067-4ca0-ad67-e00ff2e06b2e')}", # noqa: E501
"related_url": f"{settings.BASE_URL}{reverse('audio-related', identifier='36537842-b067-4ca0-ad67-e00ff2e06b2e')}", # noqa: E501
"fields_matched": ["description", "title"],
"tags": [{"name": "exam"}, {"name": "tactics"}],
}
Expand Down
16 changes: 9 additions & 7 deletions api/catalog/api/examples/image_requests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from django.urls import reverse


token = os.getenv("AUDIO_REQ_TOKEN", "DLBYIcfnKfolaXKcmMC8RIDCavc2hW")
origin = os.getenv("AUDIO_REQ_ORIGIN", "https://api.openverse.engineering")
Expand All @@ -25,7 +27,7 @@
# Example {index}: Search for images {purpose}
curl \\
{auth} \\
"{origin}/v1/images/?q={syntax}"
"{origin}{reverse('image-list')}?q={syntax}"
"""
for (index, (purpose, syntax)) in enumerate(syntax_examples.items())
)
Expand All @@ -34,28 +36,28 @@
# Search for images titled "Bark" by Sullivan
curl \\
{auth} \\
"{origin}/v1/images/?title=Bark&creator=Sullivan"
"{origin}{reverse('image-list')}?title=Bark&creator=Sullivan"
"""

image_stats_curl = f"""
# Get the statistics for image sources
curl \\
{auth} \\
"{origin}/v1/images/stats/"
"{origin}{reverse('image-list')}stats/"
"""

image_detail_curl = f"""
# Get the details of image ID {identifier}
curl \\
{auth} \\
"{origin}/v1/images/{identifier}/"
"{origin}{reverse('image-list')}{identifier}/"
"""

image_related_curl = f"""
# Get related images for image ID {identifier}
curl \\
{auth} \\
"{origin}/v1/images/{identifier}/related/"
"{origin}{reverse('image-list')}{identifier}/related/"
"""

image_complain_curl = f"""
Expand All @@ -65,12 +67,12 @@
-H "Content-Type: application/json" \\
{auth} \\
-d '{{"reason": "mature", "description": "Image contains sensitive content"}}' \\
"{origin}/v1/images/{identifier}/report/"
"{origin}{reverse('image-list')}{identifier}/report/"
"""

image_oembed_curl = f"""
# Retrieve embedded content from an image's URL
curl \\
{auth} \\
"{origin}/v1/images/oembed/?url=https://wordpress.org/openverse/photos/{identifier}"
"{origin}{reverse('image-list')}oembed/?url=https://wordpress.org/openverse/photos/{identifier}"
"""
15 changes: 8 additions & 7 deletions api/catalog/api/examples/image_responses.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os

from django.conf import settings
from django.urls import reverse

origin = os.getenv("AUDIO_REQ_ORIGIN", "https://api.openverse.engineering")

identifier = "4bc43a04-ef46-4544-a0c1-63c63f56e276"

Expand Down Expand Up @@ -52,9 +53,9 @@
"mature": False,
"height": 4016,
"width": 6016,
"thumbnail": f"{origin}/v1/images/{identifier}/thumb/",
"detail_url": f"{origin}/v1/images/{identifier}/",
"related_url": f"{origin}/v1/images/{identifier}/related/",
"thumbnail": f"{settings.BASE_URL}{reverse('image-thumbnail', identifier=identifier)}",
"detail_url": f"{settings.BASE_URL}{reverse('image-retrieve', identifier=identifier)}",
"related_url": f"{settings.BASE_URL}{reverse('image-related', identifier=identifier)}",
}

detailed_image = base_image | {
Expand Down Expand Up @@ -118,15 +119,15 @@
"creator_url": "https://www.flickr.com/photos/18090920@N07",
"tags": [{"name": "exam"}, {"name": "tactics"}],
"url": "https://live.staticflickr.com/4065/4459771899_07595dc42e.jpg", # noqa: E501
"thumbnail": "https://api.openverse.engineering/v1/thumbs/610756ec-ae31-4d5e-8f03-8cc52f31b71d", # noqa: E501
"thumbnail": f"{settings.BASE_URL}{reverse('image-thumbnail', identifier='610756ec-ae31-4d5e-8f03-8cc52f31b71d')}", # noqa: E501
"provider": "flickr",
"source": "flickr",
"license": "by",
"license_version": "2.0",
"license_url": "https://creativecommons.org/licenses/by/2.0/",
"foreign_landing_url": "https://www.flickr.com/photos/18090920@N07/4459771899", # noqa: E501
"detail_url": "http://api.openverse.engineering/v1/images/610756ec-ae31-4d5e-8f03-8cc52f31b71d", # noqa: E501
"related_url": "http://api.openverse.engineering/v1/recommendations/images/610756ec-ae31-4d5e-8f03-8cc52f31b71d", # noqa: E501
"detail_url": f"{settings.BASE_URL}{reverse('image-retrieve', identifier='610756ec-ae31-4d5e-8f03-8cc52f31b71d')}", # noqa: E501
"related_url": f"{settings.BASE_URL}{reverse('image-related', identifier='610756ec-ae31-4d5e-8f03-8cc52f31b71d')}", # noqa: E501
}
],
}
Expand Down
32 changes: 25 additions & 7 deletions api/catalog/api/views/audio_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
from catalog.api.views.media_views import MediaViewSet


@method_decorator(swagger_auto_schema(**AudioSearch.swagger_setup), "list")
@method_decorator(swagger_auto_schema(**AudioStats.swagger_setup), "stats")
@method_decorator(swagger_auto_schema(**AudioDetail.swagger_setup), "retrieve")
@method_decorator(swagger_auto_schema(**AudioRelated.swagger_setup), "related")
@method_decorator(swagger_auto_schema(**AudioComplain.swagger_setup), "report")
@method_decorator(swagger_auto_schema(**AudioThumbnail.swagger_setup), "thumbnail")
@method_decorator(swagger_auto_schema(auto_schema=None), "waveform")
# @method_decorator(swagger_auto_schema(**AudioSearch.swagger_setup), "list")
# @method_decorator(swagger_auto_schema(**AudioStats.swagger_setup), "stats")
# @method_decorator(swagger_auto_schema(**AudioDetail.swagger_setup), "retrieve")
# @method_decorator(swagger_auto_schema(**AudioRelated.swagger_setup), "related")
# @method_decorator(swagger_auto_schema(**AudioComplain.swagger_setup), "report")
# @method_decorator(swagger_auto_schema(**AudioThumbnail.swagger_setup), "thumbnail")
# @method_decorator(swagger_auto_schema(auto_schema=None), "waveform")
class AudioViewSet(MediaViewSet):
"""Viewset for all endpoints pertaining to audio."""

Expand Down Expand Up @@ -92,3 +92,21 @@ def waveform(self, *_, **__):
)
def report(self, *args, **kwargs):
return super().report(*args, **kwargs)

@staticmethod
def apply_swagger():
options = (
(AudioSearch.swagger_setup, "list"),
(AudioStats.swagger_setup, "stats"),
(AudioDetail.swagger_setup, "retrieve"),
(AudioRelated.swagger_setup, "related"),
(AudioComplain.swagger_setup, "report"),
(AudioThumbnail.swagger_setup, "thumbnail"),
({"auto_schema": None}, "waveform"),
)

for decorator_kwargs, method_name in options:
nonlocal AudioViewSet
AudioViewSet = method_decorator(
swagger_auto_schema(**decorator_kwargs), method_name
)(AudioViewSet)
10 changes: 9 additions & 1 deletion api/catalog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,16 @@ def _make_cache_config(dbnum: int, **overrides) -> dict:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

PATH_PREFIX = config("PATH_PREFIX", default=None)

if PATH_PREFIX and (PATH_PREFIX[0] == "/" or PATH_PREFIX[-1] == "/"):
raise ValueError("Path prefix must not start or end with `/`")

STATIC_URL = "/static/"

if PATH_PREFIX:
STATIC_URL = f"/{PATH_PREFIX}{STATIC_URL}"

# Allow anybody to access the API from any domain
CORS_ORIGIN_ALLOW_ALL = True

Expand Down Expand Up @@ -380,4 +388,4 @@ def _make_cache_config(dbnum: int, **overrides) -> dict:
MAX_AUTHED_PAGE_SIZE = 500
MAX_PAGINATION_DEPTH = 20

BASE_URL = config("BASE_URL", default="https://wordpress.org/openverse/")
BASE_URL = config("BASE_URL", default="https://openverse.org/api")
4 changes: 4 additions & 0 deletions api/catalog/urls/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.conf import settings
from django.conf.urls import include
from django.contrib import admin
from django.urls import path, re_path
Expand Down Expand Up @@ -84,3 +85,6 @@
# API
path("v1/", include(versioned_paths)),
]

if settings.PATH_PREFIX:
urlpatterns = [path(f"{settings.PATH_PREFIX}/", include(urlpatterns))]
12 changes: 8 additions & 4 deletions api/test/audio_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
uuid_validation,
)

from django.urls import reverse

import pytest
import requests
from django_redis import get_redis_connection
Expand Down Expand Up @@ -52,7 +54,7 @@ def force_validity(query_response):

@pytest.fixture
def audio_fixture(force_result_validity):
res = requests.get(f"{API_URL}/v1/audio/", verify=False)
res = requests.get(f"{API_URL}{reverse('audio-list')}", verify=False)
parsed = res.json()
force_result_validity(parsed)
assert res.status_code == 200
Expand All @@ -68,7 +70,7 @@ def jamendo_audio_fixture(force_result_validity):
sample audio results do not have thumbnails.
"""
res = requests.get(
f"{API_URL}/v1/audio/",
f"{API_URL}{reverse('audio-list')}",
data={"source": "jamendo"},
verify=False,
)
Expand Down Expand Up @@ -127,15 +129,17 @@ def test_audio_stats():


def test_audio_detail_without_thumb():
resp = requests.get(f"{API_URL}/v1/audio/44540200-91eb-483d-9e99-38ce86a52fb6")
resp = requests.get(
f"{API_URL}{reverse('audio-retrieve', identifier='44540200-91eb-483d-9e99-38ce86a52fb6')}"
)
assert resp.status_code == 200
parsed = json.loads(resp.text)
assert parsed["thumbnail"] is None


def test_audio_search_without_thumb():
"""The first audio of this search should not have a thumbnail."""
resp = requests.get(f"{API_URL}/v1/audio/?q=zaus")
resp = requests.get(f"{API_URL}{reverse('audio-list')}?q=zaus")
assert resp.status_code == 200
parsed = json.loads(resp.text)
assert parsed["results"][0]["thumbnail"] is None
Expand Down
12 changes: 8 additions & 4 deletions api/test/auth_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ def test_auth_rate_limit_reporting(
@pytest.mark.django_db
def test_page_size_limit_unauthed(client):
query_params = {"page_size": 20}
res = client.get("/v1/images/", query_params)
res = client.get(reverse("image-list"), query_params)
assert res.status_code == 200
query_params["page_size"] = 21
res = client.get("/v1/images/", query_params)
res = client.get(reverse("image-list"), query_params)
assert res.status_code == 401


Expand All @@ -111,9 +111,13 @@ def test_page_size_limit_authed(client, test_auth_token_exchange):
time.sleep(1)
token = test_auth_token_exchange["access_token"]
query_params = {"page_size": 21}
res = client.get("/v1/images/", query_params, HTTP_AUTHORIZATION=f"Bearer {token}")
res = client.get(
reverse("image-list"), query_params, HTTP_AUTHORIZATION=f"Bearer {token}"
)
assert res.status_code == 200

query_params = {"page_size": 500}
res = client.get("/v1/images/", query_params, HTTP_AUTHORIZATION=f"Bearer {token}")
res = client.get(
reverse("image-list"), query_params, HTTP_AUTHORIZATION=f"Bearer {token}"
)
assert res.status_code == 200
Loading