Skip to content

Commit

Permalink
Introduce a CLI API constant instead of depending on the CLI version
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Jul 6, 2024
1 parent bd73ede commit 4cadeb3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,4 @@

DEEPL_AUTH_KEY = env("DEEPL_AUTH_KEY")

CLI_VERSION = "0.0.3"
CLI_API = "1" # Bump this when changing the API in incompatible ways
10 changes: 4 additions & 6 deletions cli/trd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
from importlib.metadata import version
from pathlib import Path
from urllib.parse import urljoin

Expand All @@ -8,11 +7,14 @@
import tomllib


CLI_API = "1" # Bump this when changing the API in incompatible ways


def _session(project):
session = requests.Session()
session.headers = {
"x-token": project["token"],
"x-cli-version": _version(),
"x-cli-api": CLI_API,
}
return session

Expand Down Expand Up @@ -81,10 +83,6 @@ def _terminate(msg):
sys.exit(1)


def _version():
return version("traduire_cli")


def current_project():
config = Path.home() / ".config" / "traduire.toml"
if not config.exists():
Expand Down
20 changes: 10 additions & 10 deletions projects/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,44 +125,44 @@ def test_api(self):
# API test
r = su_client.get(
"/api/pofile/test/fr/djangojs/",
headers={"x-cli-version": "anything"},
headers={"x-cli-api": "anything"},
)
self.assertEqual(r.status_code, 400)

r = su_client.get(
"/api/pofile/test/fr/djangojs/",
headers={"x-cli-version": settings.CLI_VERSION},
headers={"x-cli-api": settings.CLI_API},
)
self.assertEqual(r.status_code, 403)

r = su_client.get(
"/api/pofile/not-exists/fr/djangojs/",
headers={"x-token": superuser.token, "x-cli-version": settings.CLI_VERSION},
headers={"x-token": superuser.token, "x-cli-api": settings.CLI_API},
)
self.assertEqual(r.status_code, 404)

r = su_client.get(
"/api/pofile/test/fr/djangojs/",
headers={"x-token": superuser.token, "x-cli-version": settings.CLI_VERSION},
headers={"x-token": superuser.token, "x-cli-api": settings.CLI_API},
)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.content.decode("utf-8"), c.pofile)

r = su_client.patch(
"/api/pofile/test/fr/djangojs/",
headers={"x-token": superuser.token, "x-cli-version": settings.CLI_VERSION},
headers={"x-token": superuser.token, "x-cli-api": settings.CLI_API},
)
self.assertEqual(r.status_code, 405)

r = su_client.get(
"/api/pofile/test/de/djangojs/",
headers={"x-token": superuser.token, "x-cli-version": settings.CLI_VERSION},
headers={"x-token": superuser.token, "x-cli-api": settings.CLI_API},
)
self.assertEqual(r.status_code, 404)

r = su_client.post(
"/api/pofile/test/fr/djangojs/",
headers={"x-token": superuser.token, "x-cli-version": settings.CLI_VERSION},
headers={"x-token": superuser.token, "x-cli-api": settings.CLI_API},
data=b"""\
#: conf/strings.js frontend/intro/intro.js frontend/people/person.js
msgid "Continue"
Expand Down Expand Up @@ -199,7 +199,7 @@ def test_api(self):
# Different language!
r = su_client.put(
"/api/pofile/test/de/djangojs/",
headers={"x-token": superuser.token, "x-cli-version": settings.CLI_VERSION},
headers={"x-token": superuser.token, "x-cli-api": settings.CLI_API},
data=b"""\
#: conf/strings.js frontend/intro/intro.js frontend/people/person.js
msgid "Continue"
Expand Down Expand Up @@ -236,13 +236,13 @@ def test_api(self):
# Delete catalogs through the API. Not currently exposed in the CLI.
r = su_client.delete(
"/api/pofile/test/fr/djangojs/",
headers={"x-token": superuser.token, "x-cli-version": settings.CLI_VERSION},
headers={"x-token": superuser.token, "x-cli-api": settings.CLI_API},
)
self.assertEqual(r.status_code, 204)

r = su_client.delete(
"/api/pofile/test/fr/djangojs/",
headers={"x-token": superuser.token, "x-cli-version": settings.CLI_VERSION},
headers={"x-token": superuser.token, "x-cli-api": settings.CLI_API},
)
self.assertEqual(r.status_code, 404)

Expand Down
4 changes: 2 additions & 2 deletions projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ def suggest(request):

@csrf_exempt
def pofile(request, project, language_code, domain):
if not (version := request.headers.get("x-cli-version")) or (
version != settings.CLI_VERSION
if not (version := request.headers.get("x-cli-api")) or (
version != settings.CLI_API
):
return http.HttpResponseBadRequest(f"Incorrect CLI version {version!r}")

Expand Down

0 comments on commit 4cadeb3

Please sign in to comment.