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

Use https for hyperlinked APIs by replacing the URLs #576

Merged
merged 2 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 5 additions & 4 deletions api/catalog/api/serializers/audio_serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from catalog.api.docs.media_docs import fields_to_md
from catalog.api.models import AudioReport
from catalog.api.models.audio import Audio
from catalog.api.serializers.base import SchemableHyperlinkedIdentityField
from catalog.api.serializers.media_serializers import (
MediaSearchRequestSerializer,
MediaSearchSerializer,
Expand Down Expand Up @@ -142,25 +143,25 @@ class AudioSerializer(MediaSerializer):
)

# Hyperlinks
thumbnail = serializers.HyperlinkedIdentityField(
thumbnail = SchemableHyperlinkedIdentityField(
read_only=True,
view_name="audio-thumb",
lookup_field="identifier",
help_text="A direct link to the miniature artwork.",
)
waveform = serializers.HyperlinkedIdentityField(
waveform = SchemableHyperlinkedIdentityField(
read_only=True,
view_name="audio-waveform",
lookup_field="identifier",
help_text="A direct link to the waveform peaks.",
)
detail_url = serializers.HyperlinkedIdentityField(
detail_url = SchemableHyperlinkedIdentityField(
read_only=True,
view_name="audio-detail",
lookup_field="identifier",
help_text="A direct link to the detail view of this audio file.",
)
related_url = serializers.HyperlinkedIdentityField(
related_url = SchemableHyperlinkedIdentityField(
read_only=True,
view_name="audio-related",
lookup_field="identifier",
Expand Down
19 changes: 19 additions & 0 deletions api/catalog/api/serializers/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import re

from django.conf import settings
from rest_framework import serializers


class SchemableHyperlinkedIdentityField(serializers.HyperlinkedIdentityField):
"""
This field returns the link but allows the option to replace the URL scheme.
"""

def __init__(self, scheme=settings.API_LINK_SCHEME, *args, **kwargs):
super().__init__(*args, **kwargs)

self.scheme = scheme

def get_url(self, *args, **kwargs):
url = super().get_url(*args, **kwargs)
return re.sub(r"\w+://", f"{self.scheme}://", url, 1)
dhruvkb marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 4 additions & 3 deletions api/catalog/api/serializers/image_serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from catalog.api.docs.media_docs import fields_to_md
from catalog.api.models import Image, ImageReport
from catalog.api.serializers.base import SchemableHyperlinkedIdentityField
from catalog.api.serializers.media_serializers import (
MediaSearchRequestSerializer,
MediaSearchSerializer,
Expand Down Expand Up @@ -94,19 +95,19 @@ class ImageSerializer(MediaSerializer):
)

# Hyperlinks
thumbnail = serializers.HyperlinkedIdentityField(
thumbnail = SchemableHyperlinkedIdentityField(
read_only=True,
view_name="image-thumb",
lookup_field="identifier",
help_text="A direct link to the miniature image.",
)
detail_url = serializers.HyperlinkedIdentityField(
detail_url = SchemableHyperlinkedIdentityField(
read_only=True,
view_name="image-detail",
lookup_field="identifier",
help_text="A direct link to the detail view of this image.",
)
related_url = serializers.HyperlinkedIdentityField(
related_url = SchemableHyperlinkedIdentityField(
view_name="image-related",
lookup_field="identifier",
read_only=True,
Expand Down
3 changes: 3 additions & 0 deletions api/catalog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@
# Whether to boost results by authority and popularity
USE_RANK_FEATURES = config("USE_RANK_FEATURES", default=True, cast=bool)

# The scheme to use for the hyperlinks in the API responses
API_LINK_SCHEME = config("API_LINK_SCHEME", default="https")

SENTRY_DSN = config(
"SENTRY_DSN",
default="https://[email protected]/6107216",
Expand Down
2 changes: 2 additions & 0 deletions api/env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ UPSTREAM_DATABASE_PORT="5432"
SEMANTIC_VERSION="1.0.0"

ELASTICSEARCH_URL="es"

API_LINK_SCHEME="http"
2 changes: 2 additions & 0 deletions api/env.template
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ SEMANTIC_VERSION="1.0.0"
#DEBUG_SCORES="False"
#USE_RANK_FEATURES="True"

API_LINK_SCHEME="http"

#SENTRY_SAMPLE_RATE=1.0
#SENTRY_DSN="https://[email protected]/6107216"