diff --git a/http-api/.env b/http-api/.env new file mode 100644 index 0000000..3445596 --- /dev/null +++ b/http-api/.env @@ -0,0 +1,7 @@ +# If it does not load this file, try running `export UV_ENV_FILE=.env` +# See more details at https://docs.astral.sh/uv/configuration/files/#env + +ELASTIC_HOSTS=http://127.0.0.1:9200 +DYNAMODB_ENDPOINT_URL=http://127.0.0.1:8000 +DYNAMODB_PARTITION_KEY=id +DYNAMODB_SORT_KEY=sk diff --git a/http-api/app.py b/http-api/app.py index a6e115a..669d4da 100644 --- a/http-api/app.py +++ b/http-api/app.py @@ -50,3 +50,9 @@ def exc_error(exc: ServiceError): @tracer.capture_lambda_handler def lambda_handler(event: dict, context: LambdaContext) -> dict: return app.resolve(event, context) + + +if __name__ == '__main__': + print( + app.get_openapi_json_schema(), + ) diff --git a/http-api/elastic.py b/http-api/elastic.py index 469e648..00285bd 100644 --- a/http-api/elastic.py +++ b/http-api/elastic.py @@ -20,15 +20,15 @@ def search( page_size: int = 25, elastic_client: Elasticsearch, ) -> PaginatedResult: + if page_size > MAX_PAGE_SIZE: + page_size = MAX_PAGE_SIZE + s = Search( using=elastic_client, index=index, ) s.update_from_dict(query) - - if page_size > MAX_PAGE_SIZE: - page_size = MAX_PAGE_SIZE - s.extra(size=page_size) + s.extra(size=page_size) try: r = s.execute() diff --git a/http-api/pyproject.toml b/http-api/pyproject.toml index cff0cff..a0b78b7 100644 --- a/http-api/pyproject.toml +++ b/http-api/pyproject.toml @@ -10,7 +10,6 @@ dependencies = ["layercake"] dev = [ "pytest>=8.3.4", "pytest-cov>=6.0.0", - "pytest-env>=1.1.5", "ruff>=0.9.1", ] diff --git a/http-api/pytest.ini b/http-api/pytest.ini deleted file mode 100644 index cf820c8..0000000 --- a/http-api/pytest.ini +++ /dev/null @@ -1,5 +0,0 @@ -[pytest] -env = - DYNAMODB_ENDPOINT_URL=http://127.0.0.1:8000 - DYNAMODB_PARTITION_KEY=id - DYNAMODB_SORT_KEY=sk diff --git a/http-api/routes/courses/__init__.py b/http-api/routes/courses/__init__.py index 5c42204..fe326bb 100644 --- a/http-api/routes/courses/__init__.py +++ b/http-api/routes/courses/__init__.py @@ -19,7 +19,7 @@ def get_courses(): query = event.get_query_string_value('query', '{}') page_size = event.get_query_string_value('page_size', '25') - return elastic.search( # type: ignore + return elastic.search( index=COURSE_TABLE, page_size=int(page_size), query=json.loads(query), diff --git a/http-api/settings.py b/http-api/settings.py index 64e5cbb..4706331 100644 --- a/http-api/settings.py +++ b/http-api/settings.py @@ -7,7 +7,12 @@ COURSE_TABLE: str = os.getenv('COURSE_TABLE') # type: ignore ELASTIC_CLOUD_ID = os.getenv('ELASTIC_CLOUD_ID') ELASTIC_AUTH_PASS = os.getenv('ELASTIC_AUTH_PASS') -ELASTIC_CONN = { - 'cloud_id': ELASTIC_CLOUD_ID, - 'basic_auth': ('elastic', ELASTIC_AUTH_PASS), -} +ELASTIC_HOSTS = os.getenv('ELASTIC_HOSTS', None) + +if ELASTIC_HOSTS: + ELASTIC_CONN = {'hosts': ELASTIC_HOSTS} +else: + ELASTIC_CONN = { + 'cloud_id': ELASTIC_CLOUD_ID, + 'basic_auth': ('elastic', ELASTIC_AUTH_PASS), + } diff --git a/http-api/tests/conftest.py b/http-api/tests/conftest.py index f8bc820..fcbf473 100644 --- a/http-api/tests/conftest.py +++ b/http-api/tests/conftest.py @@ -132,8 +132,7 @@ def dynamodb_seeds(dynamodb_client): @pytest.fixture -def mock_app(monkeypatch): - monkeypatch.setattr('settings.ELASTIC_CONN', {'hosts': 'http://127.0.0.1:9200'}) +def mock_app(): import app return app diff --git a/http-api/uv.lock b/http-api/uv.lock index d1ad4ec..86f2046 100644 --- a/http-api/uv.lock +++ b/http-api/uv.lock @@ -353,7 +353,6 @@ dependencies = [ dev = [ { name = "pytest" }, { name = "pytest-cov" }, - { name = "pytest-env" }, { name = "ruff" }, ] @@ -364,7 +363,6 @@ requires-dist = [{ name = "layercake", directory = "../layercake" }] dev = [ { name = "pytest", specifier = ">=8.3.4" }, { name = "pytest-cov", specifier = ">=6.0.0" }, - { name = "pytest-env", specifier = ">=1.1.5" }, { name = "ruff", specifier = ">=0.9.1" }, ] @@ -641,18 +639,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/36/3b/48e79f2cd6a61dbbd4807b4ed46cb564b4fd50a76166b1c4ea5c1d9e2371/pytest_cov-6.0.0-py3-none-any.whl", hash = "sha256:eee6f1b9e61008bd34975a4d5bab25801eb31898b032dd55addc93e96fcaaa35", size = 22949 }, ] -[[package]] -name = "pytest-env" -version = "1.1.5" -source = { registry = "https://pypi.org/simple" } -dependencies = [ - { name = "pytest" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/1f/31/27f28431a16b83cab7a636dce59cf397517807d247caa38ee67d65e71ef8/pytest_env-1.1.5.tar.gz", hash = "sha256:91209840aa0e43385073ac464a554ad2947cc2fd663a9debf88d03b01e0cc1cf", size = 8911 } -wheels = [ - { url = "https://files.pythonhosted.org/packages/de/b8/87cfb16045c9d4092cfcf526135d73b88101aac83bc1adcf82dfb5fd3833/pytest_env-1.1.5-py3-none-any.whl", hash = "sha256:ce90cf8772878515c24b31cd97c7fa1f4481cd68d588419fd45f10ecaee6bc30", size = 6141 }, -] - [[package]] name = "python-dateutil" version = "2.9.0.post0"