From 4352fedf5b45f146924685f611e17c6ab85179ab Mon Sep 17 00:00:00 2001 From: maudetes Date: Fri, 10 Nov 2023 17:13:21 +0100 Subject: [PATCH 1/4] Add SERVER_NAME config --- api_tabular/config_default.toml | 1 + api_tabular/utils.py | 6 ++++-- tests/test_api.py | 24 +++++++++++++----------- tests/test_utils.py | 5 +++-- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/api_tabular/config_default.toml b/api_tabular/config_default.toml index ed55565..efed8ba 100644 --- a/api_tabular/config_default.toml +++ b/api_tabular/config_default.toml @@ -1,4 +1,5 @@ PG_RST_URL = "http://localhost:8080" +SERVER_NAME = 'http://localhost:8005' SENTRY_DSN = "" PAGE_SIZE_DEFAULT = 20 PAGE_SIZE_MAX = 50 diff --git a/api_tabular/utils.py b/api_tabular/utils.py index 4efa6b1..e4ae073 100644 --- a/api_tabular/utils.py +++ b/api_tabular/utils.py @@ -1,5 +1,7 @@ from aiohttp.web_request import Request +from api_tabular import config + def build_sql_query_string( request_arg: list, page_size: int = None, offset: int = 0 @@ -45,11 +47,11 @@ def build_link_with_page(request: Request, query_string: str, page: int, page_si q = [string for string in query_string if not string.startswith("page")] q.extend([f"page={page}", f"page_size={page_size}"]) rebuilt_q = "&".join(q) - return f"{request.scheme}://{request.host}{request.path}?{rebuilt_q}" + return f"{config.SERVER_NAME}{request.path}?{rebuilt_q}" def url_for(request: Request, route: str, *args, **kwargs): router = request.app.router if kwargs.pop("_external", None): - return f"{request.scheme}://{request.host}{router[route].url_for(**kwargs)}" + return f"{config.SERVER_NAME}{router[route].url_for(**kwargs)}" return router[route].url_for(**kwargs) diff --git a/tests/test_api.py b/tests/test_api.py index 7e3c6da..40931f8 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,5 +1,7 @@ import pytest +from api_tabular import config + from .conftest import RESOURCE_ID, DATE, PG_RST_URL, TABLES_INDEX_PATTERN pytestmark = pytest.mark.asyncio @@ -22,12 +24,12 @@ async def test_api_resource_meta(client, rmock): "url": "https://example.com", "links": [ { - "href": f"http://127.0.0.1:{client.port}/api/resources/{RESOURCE_ID}/profile/", + "href": f"{config.SERVER_NAME}/api/resources/{RESOURCE_ID}/profile/", "type": "GET", "rel": "profile", }, { - "href": f"http://127.0.0.1:{client.port}/api/resources/{RESOURCE_ID}/data/", + "href": f"{config.SERVER_NAME}/api/resources/{RESOURCE_ID}/data/", "type": "GET", "rel": "data", }, @@ -66,7 +68,7 @@ async def test_api_resource_data(client, rmock): "links": { "next": None, "prev": None, - "profile": f"http://127.0.0.1:{client.port}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", + "profile": f"{config.SERVER_NAME}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", }, "meta": {"page": 1, "page_size": 20, "total": 10}, } @@ -88,7 +90,7 @@ async def test_api_resource_data_with_args(client, rmock): "links": { "next": None, "prev": None, - "profile": f"http://127.0.0.1:{client.port}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", + "profile": f"{config.SERVER_NAME}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", }, "meta": {"page": 1, "page_size": 20, "total": 10}, } @@ -110,7 +112,7 @@ async def test_api_resource_data_with_args_case(client, rmock): "links": { "next": None, "prev": None, - "profile": f"http://127.0.0.1:{client.port}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", + "profile": f"{config.SERVER_NAME}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", }, "meta": {"page": 1, "page_size": 20, "total": 10}, } @@ -183,7 +185,7 @@ async def test_api_percent_encoding_arabic(client, rmock): "links": { "next": None, "prev": None, - "profile": f"http://127.0.0.1:{client.port}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", + "profile": f"{config.SERVER_NAME}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", }, "meta": {"page": 1, "page_size": 20, "total": 10}, } @@ -205,7 +207,7 @@ async def test_api_with_unsupported_args(client, rmock): "links": { "next": None, "prev": None, - "profile": f"http://127.0.0.1:{client.port}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", + "profile": f"{config.SERVER_NAME}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", }, "meta": {"page": 1, "page_size": 20, "total": 10}, } @@ -225,10 +227,10 @@ async def test_api_pagination(client, rmock): body = { "data": [{"such": "data"}], "links": { - "next": f"http://127.0.0.1:{client.port}" + "next": f"{config.SERVER_NAME}" "/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/data/?page=2&page_size=1", "prev": None, - "profile": f"http://127.0.0.1:{client.port}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", + "profile": f"{config.SERVER_NAME}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", }, "meta": {"page": 1, "page_size": 1, "total": 2}, } @@ -247,9 +249,9 @@ async def test_api_pagination(client, rmock): "data": [{"such": "data"}], "links": { "next": None, - "prev": f"http://127.0.0.1:{client.port}" + "prev": f"{config.SERVER_NAME}" "/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/data/?page=1&page_size=1", - "profile": f"http://127.0.0.1:{client.port}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", + "profile": f"{config.SERVER_NAME}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", }, "meta": {"page": 2, "page_size": 1, "total": 2}, } diff --git a/tests/test_utils.py b/tests/test_utils.py index 6cf332a..3f984bb 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,13 +1,14 @@ from aiohttp.test_utils import make_mocked_request +from api_tabular import config from api_tabular.utils import build_link_with_page, url_for def test_build_link_with_page(): request = make_mocked_request("GET", "/api/test?foo=bar") link = build_link_with_page(request, query_string=["foo=1", "bar=3"], page=2, page_size=10) - assert link == f"{request.scheme}://{request.host}/api/test?foo=1&bar=3&page=2&page_size=10" + assert link == f"{config.SERVER_NAME}/api/test?foo=1&bar=3&page=2&page_size=10" def test_url_for(client): @@ -21,4 +22,4 @@ def test_url_for_external(client): request = make_mocked_request("GET", "/api/test?foo=bar") request.app.router = client.app.router url = url_for(request, 'profile', rid='rid', _external=True) - assert str(url) == f'{request.scheme}://{request.host}/api/resources/rid/profile/' + assert str(url) == f'{config.SERVER_NAME}/api/resources/rid/profile/' From 6699d86723a7367fa6b878e507896d9bbf4bf37b Mon Sep 17 00:00:00 2001 From: maudetes Date: Fri, 10 Nov 2023 17:15:43 +0100 Subject: [PATCH 2/4] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 644f370..96e9053 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,3 +2,5 @@ - Add endpoint to stream a CSV response [#5](https://github.com/etalab/api-tabular/pull/5) - Make URL in links absolute [#7](https://github.com/etalab/api-tabular/pull/7) +- Add health route [#16](https://github.com/etalab/api-tabular/pull/16) +- Add SERVER NAME config [#17](https://github.com/etalab/api-tabular/pull/17) \ No newline at end of file From 72707c8cf807f8debaf7c0d679b377bc3a042d50 Mon Sep 17 00:00:00 2001 From: maudetes Date: Fri, 10 Nov 2023 17:51:03 +0100 Subject: [PATCH 3/4] Add SCHEME config --- .gitignore | 5 ++++- api_tabular/config_default.toml | 3 ++- api_tabular/utils.py | 8 ++++++-- tests/test_api.py | 28 ++++++++++++++-------------- tests/test_utils.py | 7 +++---- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 80bb7bc..1ca990c 100644 --- a/.gitignore +++ b/.gitignore @@ -71,4 +71,7 @@ coverage.xml Procfile # PyCharm -.idea \ No newline at end of file +.idea + +# Config +config.toml \ No newline at end of file diff --git a/api_tabular/config_default.toml b/api_tabular/config_default.toml index efed8ba..de7e59c 100644 --- a/api_tabular/config_default.toml +++ b/api_tabular/config_default.toml @@ -1,5 +1,6 @@ PG_RST_URL = "http://localhost:8080" -SERVER_NAME = 'http://localhost:8005' +SERVER_NAME = 'localhost:8005' +SCHEME = 'http' SENTRY_DSN = "" PAGE_SIZE_DEFAULT = 20 PAGE_SIZE_MAX = 50 diff --git a/api_tabular/utils.py b/api_tabular/utils.py index e4ae073..049b604 100644 --- a/api_tabular/utils.py +++ b/api_tabular/utils.py @@ -43,15 +43,19 @@ def process_total(raw_total: str) -> int: return int(str_total) +def external_url(url): + return f"{config.SCHEME}://{config.SERVER_NAME}{url}" + + def build_link_with_page(request: Request, query_string: str, page: int, page_size: int): q = [string for string in query_string if not string.startswith("page")] q.extend([f"page={page}", f"page_size={page_size}"]) rebuilt_q = "&".join(q) - return f"{config.SERVER_NAME}{request.path}?{rebuilt_q}" + return external_url(f"{request.path}?{rebuilt_q}") def url_for(request: Request, route: str, *args, **kwargs): router = request.app.router if kwargs.pop("_external", None): - return f"{config.SERVER_NAME}{router[route].url_for(**kwargs)}" + return external_url(router[route].url_for(**kwargs)) return router[route].url_for(**kwargs) diff --git a/tests/test_api.py b/tests/test_api.py index 40931f8..2ec6423 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,6 +1,6 @@ import pytest -from api_tabular import config +from api_tabular.utils import external_url from .conftest import RESOURCE_ID, DATE, PG_RST_URL, TABLES_INDEX_PATTERN @@ -24,12 +24,12 @@ async def test_api_resource_meta(client, rmock): "url": "https://example.com", "links": [ { - "href": f"{config.SERVER_NAME}/api/resources/{RESOURCE_ID}/profile/", + "href": external_url(f"/api/resources/{RESOURCE_ID}/profile/"), "type": "GET", "rel": "profile", }, { - "href": f"{config.SERVER_NAME}/api/resources/{RESOURCE_ID}/data/", + "href": external_url(f"/api/resources/{RESOURCE_ID}/data/"), "type": "GET", "rel": "data", }, @@ -68,7 +68,7 @@ async def test_api_resource_data(client, rmock): "links": { "next": None, "prev": None, - "profile": f"{config.SERVER_NAME}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", + "profile": external_url("/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/"), }, "meta": {"page": 1, "page_size": 20, "total": 10}, } @@ -90,7 +90,7 @@ async def test_api_resource_data_with_args(client, rmock): "links": { "next": None, "prev": None, - "profile": f"{config.SERVER_NAME}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", + "profile": external_url("/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/"), }, "meta": {"page": 1, "page_size": 20, "total": 10}, } @@ -112,7 +112,7 @@ async def test_api_resource_data_with_args_case(client, rmock): "links": { "next": None, "prev": None, - "profile": f"{config.SERVER_NAME}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", + "profile": external_url("/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/"), }, "meta": {"page": 1, "page_size": 20, "total": 10}, } @@ -185,7 +185,7 @@ async def test_api_percent_encoding_arabic(client, rmock): "links": { "next": None, "prev": None, - "profile": f"{config.SERVER_NAME}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", + "profile": external_url("/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/"), }, "meta": {"page": 1, "page_size": 20, "total": 10}, } @@ -207,7 +207,7 @@ async def test_api_with_unsupported_args(client, rmock): "links": { "next": None, "prev": None, - "profile": f"{config.SERVER_NAME}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", + "profile": external_url("/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/"), }, "meta": {"page": 1, "page_size": 20, "total": 10}, } @@ -227,10 +227,10 @@ async def test_api_pagination(client, rmock): body = { "data": [{"such": "data"}], "links": { - "next": f"{config.SERVER_NAME}" - "/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/data/?page=2&page_size=1", + "next": external_url( + "/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/data/?page=2&page_size=1"), "prev": None, - "profile": f"{config.SERVER_NAME}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", + "profile": external_url("/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/"), }, "meta": {"page": 1, "page_size": 1, "total": 2}, } @@ -249,9 +249,9 @@ async def test_api_pagination(client, rmock): "data": [{"such": "data"}], "links": { "next": None, - "prev": f"{config.SERVER_NAME}" - "/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/data/?page=1&page_size=1", - "profile": f"{config.SERVER_NAME}/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/", + "prev": external_url( + "/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/data/?page=1&page_size=1"), + "profile": external_url("/api/resources/60963939-6ada-46bc-9a29-b288b16d969b/profile/"), }, "meta": {"page": 2, "page_size": 1, "total": 2}, } diff --git a/tests/test_utils.py b/tests/test_utils.py index 3f984bb..d658936 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,14 +1,13 @@ from aiohttp.test_utils import make_mocked_request -from api_tabular import config -from api_tabular.utils import build_link_with_page, url_for +from api_tabular.utils import build_link_with_page, url_for, external_url def test_build_link_with_page(): request = make_mocked_request("GET", "/api/test?foo=bar") link = build_link_with_page(request, query_string=["foo=1", "bar=3"], page=2, page_size=10) - assert link == f"{config.SERVER_NAME}/api/test?foo=1&bar=3&page=2&page_size=10" + assert link == external_url("/api/test?foo=1&bar=3&page=2&page_size=10") def test_url_for(client): @@ -22,4 +21,4 @@ def test_url_for_external(client): request = make_mocked_request("GET", "/api/test?foo=bar") request.app.router = client.app.router url = url_for(request, 'profile', rid='rid', _external=True) - assert str(url) == f'{config.SERVER_NAME}/api/resources/rid/profile/' + assert str(url) == external_url("/api/resources/rid/profile/") From edb150ccdf7e407772266ad727ccea99a6579642 Mon Sep 17 00:00:00 2001 From: maudetes Date: Fri, 10 Nov 2023 17:51:51 +0100 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96e9053..d0ba945 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,4 +3,4 @@ - Add endpoint to stream a CSV response [#5](https://github.com/etalab/api-tabular/pull/5) - Make URL in links absolute [#7](https://github.com/etalab/api-tabular/pull/7) - Add health route [#16](https://github.com/etalab/api-tabular/pull/16) -- Add SERVER NAME config [#17](https://github.com/etalab/api-tabular/pull/17) \ No newline at end of file +- Add SERVER NAME and SCHEME config [#17](https://github.com/etalab/api-tabular/pull/17) \ No newline at end of file