Skip to content

Commit

Permalink
Addons: move the HTTP header to a GET parameter
Browse files Browse the repository at this point in the history
Move `X-RTD-Hosting-Integration-Version` header to `api-version` querystring
parameter. This has some benefits:

- make links to this URL clickable (there is no need to use a browser extension
  to send a specific header in the request)
- easier to cache using the default strategy supported by CF

Related #10536
  • Loading branch information
humitos committed Sep 19, 2023
1 parent 8566a62 commit 9ea5769
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion readthedocs/proxito/tests/responses/v2.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"error": "The version specified in 'X-RTD-Hosting-Integrations-Version' is currently not supported"
"error": "The version specified in 'api-version' is currently not supported"
}
18 changes: 12 additions & 6 deletions readthedocs/proxito/tests/test_hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ def _normalize_datetime_fields(self, obj):
def test_get_config_v0(self):
r = self.client.get(
reverse("proxito_readthedocs_docs_addons"),
{"url": "https://project.dev.readthedocs.io/en/latest/"},
{
"url": "https://project.dev.readthedocs.io/en/latest/",
"api-version": "0.1.0",
},
secure=True,
headers={
"host": "project.dev.readthedocs.io",
"x-rtd-hosting-integrations-version": "0.1.0",
},
)
assert r.status_code == 200
Expand All @@ -95,11 +97,13 @@ def test_get_config_v0(self):
def test_get_config_v1(self):
r = self.client.get(
reverse("proxito_readthedocs_docs_addons"),
{"url": "https://project.dev.readthedocs.io/en/latest/"},
{
"url": "https://project.dev.readthedocs.io/en/latest/",
"api-version": "1.0.0",
},
secure=True,
headers={
"host": "project.dev.readthedocs.io",
"x-rtd-hosting-integrations-version": "1.0.0",
},
)
assert r.status_code == 200
Expand All @@ -108,11 +112,13 @@ def test_get_config_v1(self):
def test_get_config_unsupported_version(self):
r = self.client.get(
reverse("proxito_readthedocs_docs_addons"),
{"url": "https://project.dev.readthedocs.io/en/latest/"},
{
"url": "https://project.dev.readthedocs.io/en/latest/",
"api-version": "2.0.0",
},
secure=True,
headers={
"host": "project.dev.readthedocs.io",
"x-rtd-hosting-integrations-version": "2.0.0",
},
)
assert r.status_code == 400
Expand Down
16 changes: 6 additions & 10 deletions readthedocs/proxito/views/hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@

class ClientError(Exception):
VERSION_NOT_CURRENTLY_SUPPORTED = (
"The version specified in 'X-RTD-Hosting-Integrations-Version'"
" is currently not supported"
)
VERSION_INVALID = "'X-RTD-Hosting-Integrations-Version' header version is invalid"
VERSION_HEADER_MISSING = (
"'X-RTD-Hosting-Integrations-Version' header attribute is required"
"The version specified in 'api-version' is currently not supported"
)
VERSION_INVALID = "The version specifified in 'api-version' is invalid"


class BaseReadTheDocsConfigJson(CDNCacheTagsMixin, APIView):
Expand All @@ -52,6 +48,8 @@ class BaseReadTheDocsConfigJson(CDNCacheTagsMixin, APIView):
url (required): absolute URL from where the request is performed
(e.g. ``window.location.href``)
api-version (required): API JSON structure version.
"""

http_method_names = ["get"]
Expand Down Expand Up @@ -112,12 +110,10 @@ def get(self, request, format=None):
status=400,
)

addons_version = request.headers.get("X-RTD-Hosting-Integrations-Version")
addons_version = request.GET.get("api-version")
if not addons_version:
return JsonResponse(
{
"error": ClientError.VERSION_HEADER_MISSING,
},
{"error": "'api-version' GET attribute is required"},
status=400,
)
try:
Expand Down

0 comments on commit 9ea5769

Please sign in to comment.