Skip to content

Commit

Permalink
allow json body for encode request, backwards compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronshiel committed May 12, 2023
1 parent cc00320 commit 5a56cf8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 5 additions & 2 deletions server/api/blueprints/encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
@encode_blueprint.route("", methods=["GET", "POST"])
@authenticate
def encode():
if "query" not in request.args:
if request.get_json(silent=True) is not None and "query" in request.json:
sentence = request.json["query"].strip()
elif "query" in request.args:
sentence = request.args["query"].strip()
else:
return (jsonify({"query": ["required field"]}), 400)
sentence = request.args["query"].strip()
result = encoder.encode(sentence)
return (
jsonify(
Expand Down
13 changes: 12 additions & 1 deletion tests/test_api_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_returns_400_response_when_query_missing(client):
assert res.status_code == 400


def test_hello_world(client):
def test_hello_world_query_param(client):
res = client.get(
"/v1/encode?query=hello+world", headers={"Authorization": "bearer dummykey"}
)
Expand All @@ -24,6 +24,17 @@ def test_hello_world(client):
assert len(res.json["encoding"]) > 20


def test_hello_world_json_body(client):
res = client.get(
"/v1/encode",
json={"query": "hello world"},
headers={"Authorization": "bearer dummykey"},
)
assert res.status_code == 200
assert res.json["query"] == "hello world"
assert len(res.json["encoding"]) > 20


def test_multiple_encode(client):
res = client.get(
"/v1/encode/multiple_encode/",
Expand Down
1 change: 1 addition & 0 deletions tools/clint_transcripts.json

Large diffs are not rendered by default.

0 comments on commit 5a56cf8

Please sign in to comment.