Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
Refactor API endpoints to comply with REST conventions. (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxyu1115 authored Jan 2, 2024
1 parent e73a05e commit 3d81f9a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
16 changes: 8 additions & 8 deletions integration-tests/test_controlplane.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@


def test_create_user(test_client):
resp = test_client.post("/cp/create_user", json={"namespace_pathname": "create_user_1"})
resp = test_client.post("/cp/user", json={"namespace_pathname": "create_user_1"})
assert resp.status_code == 200


def test_create_user_existing(test_client):
test_client.post("/cp/create_user", json={"namespace_pathname": "create_user_2"})
resp = test_client.post("/cp/create_user", json={"namespace_pathname": "create_user_2"})
test_client.post("/cp/user", json={"namespace_pathname": "create_user_2"})
resp = test_client.post("/cp/user", json={"namespace_pathname": "create_user_2"})
assert resp.status_code == 400
assert resp.json["error_code"] == "namespace_exists"


def test_create_corpus(test_client):
resp = test_client.post("/cp/create_corpus", json={"corpus_pathname": "create_user_1:create_corpus_1"})
resp = test_client.post("/cp/corpus", json={"corpus_pathname": "create_user_1:create_corpus_1", "namespace_pathname": "create_user_1"})
assert resp.status_code == 200


def test_create_corpus_existing(test_client):
test_client.post("/cp/create_corpus", json={"corpus_pathname": "create_user_2:create_corpus_2"})
resp = test_client.post("/cp/create_corpus", json={"corpus_pathname": "create_user_2:create_corpus_2"})
test_client.post("/cp/corpus", json={"corpus_pathname": "create_user_2:create_corpus_2", "namespace_pathname": "create_user_2"})
resp = test_client.post("/cp/corpus", json={"corpus_pathname": "create_user_2:create_corpus_2", "namespace_pathname": "create_user_2"})
assert resp.status_code == 400
assert resp.json["error_code"] == "namespace_exists"


@mock.patch("memas.celery_worker.delete_corpus")
def test_delete_corpus(mock_delete_corpus, test_client):
test_client.post("/cp/create_corpus", json={"corpus_pathname": "create_user_2:delete_corpus"})
test_client.post("/cp/corpus", json={"corpus_pathname": "create_user_2:delete_corpus", "namespace_pathname": "create_user_2"})

resp = test_client.post("/cp/delete_corpus", json={"corpus_pathname": "create_user_2:delete_corpus"})
resp = test_client.delete("/cp/corpus", json={"corpus_pathname": "create_user_2:delete_corpus", "namespace_pathname": "create_user_2"})
assert resp.status_code == 200
assert mock_delete_corpus.has_called()
6 changes: 3 additions & 3 deletions integration-tests/test_dataplane.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
def test_memorize_then_recall(test_client):
namespace_pathname = "memorize"
corpus_pathname = namespace_pathname + ":memorize_1"
resp1 = test_client.post("/cp/create_user", json={"namespace_pathname": namespace_pathname})
resp1 = test_client.post("/cp/user", json={"namespace_pathname": namespace_pathname})
print(resp1)
resp2 = test_client.post("/cp/create_corpus", json={"corpus_pathname": corpus_pathname})
resp2 = test_client.post("/cp/corpus", json={"corpus_pathname": corpus_pathname, "namespace_pathname": namespace_pathname})
print(resp2)

resp3 = test_client.post(
Expand All @@ -16,7 +16,7 @@ def test_memorize_then_recall(test_client):

time.sleep(1)

resp4 = test_client.post("/dp/recall", json={"namespace_pathname": namespace_pathname, "clue": "What's MeMaS"})
resp4 = test_client.get("/dp/recall", json={"namespace_pathname": namespace_pathname, "clue": "What's MeMaS"})
assert resp4.status_code == 200
assert len(resp4.json) == 1
assert resp4.json[0]["document"] == "What's MeMaS"
4 changes: 2 additions & 2 deletions memas/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from celery import Celery, Task
from flask import Flask
from memas.context_manager import read_env, ContextManager
from memas.controlplane import controlplane
from memas.dataplane import dataplane
from memas.interface.exceptions import MemasException


Expand Down Expand Up @@ -42,8 +44,6 @@ def handle_memas_exception(e: MemasException):

celery_init_app(app)

from memas.dataplane import dataplane
from memas.controlplane import controlplane
app.register_blueprint(dataplane)
app.register_blueprint(controlplane)

Expand Down
39 changes: 31 additions & 8 deletions memas/controlplane.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,45 @@
controlplane = Blueprint("cp", __name__, url_prefix="/cp")


@controlplane.route('/create_user', methods=["POST"])
def create_user():
@controlplane.route('/user', methods=["POST", "DELETE"])
def user_endpoint():
namespace_pathname = request.json["namespace_pathname"]
if request.method == 'POST':
return create_user(namespace_pathname)
elif request.method == 'DELETE':
return delete_user(namespace_pathname)
else:
raise NotImplementedError(f"Request Type '{request.method}' not supported")


def create_user(namespace_pathname: str):
current_app.logger.info(f"Create user [namespace_pathname=\"{namespace_pathname}\"]")

ctx.memas_metadata.create_namespace(namespace_pathname)
return {"success": True}


@controlplane.route('/create_corpus', methods=["POST"])
def create_corpus():
def delete_user(namespace_pathname: str):
current_app.logger.info(f"Delete user [namespace_pathname=\"{namespace_pathname}\"]")

# TODO: need to implement
ctx.memas_metadata.create_namespace(namespace_pathname)
return {"success": True}


@controlplane.route('/corpus', methods=["POST", "DELETE"])
def corpus_endpoint():
namespace_pathname = request.json["namespace_pathname"]
corpus_pathname = request.json["corpus_pathname"]
if request.method == 'POST':
return create_corpus(namespace_pathname, corpus_pathname)
elif request.method == 'DELETE':
return delete_corpus(namespace_pathname, corpus_pathname)
else:
raise NotImplementedError(f"Request Type '{request.method}' not supported")


def create_corpus(namespace_pathname: str, corpus_pathname: str):
corpus_type = request.json.get("corpus_type", CorpusType.CONVERSATION.value)

current_app.logger.info(f"Create corpus [corpus_pathname=\"{corpus_pathname}\"] [corpus_type={corpus_type}]")
Expand All @@ -33,10 +59,7 @@ def create_corpus():
return {"success": True}


@controlplane.route('/delete_corpus', methods=["POST"])
def delete_corpus():
corpus_pathname = request.json["corpus_pathname"]

def delete_corpus(namespace_pathname: str, corpus_pathname: str):
current_app.logger.info(f"Delete corpus [corpus_pathname=\"{corpus_pathname}\"]")

# Get ids will raise an exception if the pathname is not found
Expand Down
2 changes: 1 addition & 1 deletion memas/dataplane.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
dataplane = Blueprint("dp", __name__, url_prefix="/dp")


@dataplane.route('/recall', methods=["POST"])
@dataplane.route('/recall', methods=["GET"])
def recall():
namespace_pathname: str = request.json["namespace_pathname"]
clue: str = request.json["clue"]
Expand Down

0 comments on commit 3d81f9a

Please sign in to comment.