From 2dab53e88e85c2db6253d86d75925cfc6c147172 Mon Sep 17 00:00:00 2001 From: Hazal Date: Tue, 17 Dec 2024 11:35:08 +0000 Subject: [PATCH] Simple API endpoint (#6) * added simple router * included basic router to the main --- spacy_keyword_extraction_api/api_router.py | 16 ++++++++++++++++ spacy_keyword_extraction_api/main.py | 5 ++++- tests/unit_test/api_router_test.py | 18 ++++++++++++++++++ tests/unit_test/main_test.py | 6 ++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 spacy_keyword_extraction_api/api_router.py create mode 100644 tests/unit_test/api_router_test.py diff --git a/spacy_keyword_extraction_api/api_router.py b/spacy_keyword_extraction_api/api_router.py new file mode 100644 index 0000000..658462f --- /dev/null +++ b/spacy_keyword_extraction_api/api_router.py @@ -0,0 +1,16 @@ +import logging + +from fastapi import APIRouter + + +LOGGER = logging.getLogger(__name__) + + +def create_api_router() -> APIRouter: + router = APIRouter() + + @router.get("/v1/extract-keywords") + def extract_keywords(text: str): + return text.split() + + return router diff --git a/spacy_keyword_extraction_api/main.py b/spacy_keyword_extraction_api/main.py index 07e6077..e9edf7d 100644 --- a/spacy_keyword_extraction_api/main.py +++ b/spacy_keyword_extraction_api/main.py @@ -3,6 +3,8 @@ from fastapi import FastAPI from fastapi.staticfiles import StaticFiles +from spacy_keyword_extraction_api.api_router import create_api_router + LOGGER = logging.getLogger(__name__) @@ -10,7 +12,8 @@ def create_app(): app = FastAPI() - app = FastAPI(docs_url=None, redoc_url=None) + app.include_router(create_api_router()) + app.mount('/', StaticFiles(directory='static', html=True), name='static') return app diff --git a/tests/unit_test/api_router_test.py b/tests/unit_test/api_router_test.py new file mode 100644 index 0000000..161e432 --- /dev/null +++ b/tests/unit_test/api_router_test.py @@ -0,0 +1,18 @@ +from fastapi.testclient import TestClient +from fastapi import FastAPI + +from spacy_keyword_extraction_api.api_router import create_api_router + + +def create_test_client(): + app = FastAPI() + app.include_router(create_api_router()) + client = TestClient(app) + return client + + +class TestExtractKeyword: + def test_should_return_keywords(self): + client = create_test_client() + response = client.get('/v1/extract-keywords', params={'text': 'some text'}) + assert response.json() == ['some', 'text'] diff --git a/tests/unit_test/main_test.py b/tests/unit_test/main_test.py index 95a65da..d35f791 100644 --- a/tests/unit_test/main_test.py +++ b/tests/unit_test/main_test.py @@ -7,3 +7,9 @@ def test_read_main(): client = TestClient(create_app()) response = client.get("/") assert response.status_code == 200 + + +def test_extract_keywords_endpoint_is_available(): + client = TestClient(create_app()) + response = client.get("/v1/extract-keywords", params={'text': 'some text'}) + assert response.status_code == 200