-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This reverts commit fe28f8c.
- Loading branch information
Showing
10 changed files
with
167 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 0 additions & 86 deletions
86
api/src/alembic/versions/20241217_103425_bc02c1c6f60e_remove_request.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import logging | ||
|
||
import fastapi | ||
|
||
from data_inclusion.api.request.services import save_request | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def save_request_middleware(request: fastapi.Request, call_next): | ||
response = fastapi.Response("Internal server error", status_code=500) | ||
try: | ||
response = await call_next(request) | ||
except: | ||
raise | ||
finally: | ||
try: | ||
save_request(request, response, db_session=request.state.db_session) | ||
except Exception as err: | ||
logger.error(err) | ||
pass | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import sqlalchemy as sqla | ||
from sqlalchemy.orm import Mapped | ||
|
||
from data_inclusion.api.core import db | ||
|
||
|
||
class Request(db.Base): | ||
id: Mapped[db.uuid_pk] | ||
created_at: Mapped[db.timestamp] | ||
status_code: Mapped[int] | ||
method: Mapped[str] | ||
path: Mapped[str] | ||
base_url: Mapped[str] | ||
user: Mapped[str | None] | ||
path_params: Mapped[dict] | ||
query_params: Mapped[dict] | ||
client_host: Mapped[str | None] | ||
client_port: Mapped[int | None] | ||
endpoint_name: Mapped[str | None] | ||
|
||
__table_args__ = ( | ||
sqla.Index(None, "endpoint_name"), | ||
sqla.Index(None, "method"), | ||
sqla.Index(None, "status_code"), | ||
sqla.Index(None, "created_at"), | ||
sqla.Index(None, "user"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import fastapi | ||
|
||
from data_inclusion.api.core import db | ||
from data_inclusion.api.request import models | ||
|
||
|
||
def is_trailing_slash_redirect( | ||
request: fastapi.Request, response: fastapi.Response | ||
) -> bool: | ||
redirect_url = response.headers.get("location") | ||
return response.status_code == 307 and str(request.url) == f"{redirect_url}/" | ||
|
||
|
||
def save_request( | ||
request: fastapi.Request, | ||
response: fastapi.Response, | ||
db_session=fastapi.Depends(db.get_session), | ||
) -> None: | ||
if is_trailing_slash_redirect(request=request, response=response): | ||
return | ||
|
||
endpoint_name = None | ||
if (route := request.scope.get("route")) is not None: | ||
endpoint_name = route.name | ||
|
||
username = None | ||
if (user := request.scope.get("user")) is not None and user.is_authenticated: | ||
username = user.username | ||
|
||
request_instance = models.Request( | ||
status_code=response.status_code, | ||
method=request.method, | ||
path=request.url.path, | ||
base_url=str(request.base_url), | ||
user=username, | ||
path_params=request.path_params, | ||
query_params={ | ||
key: ",".join(request.query_params.getlist(key)) | ||
for key in request.query_params.keys() | ||
}, | ||
client_host=request.client.host if request.client is not None else None, | ||
client_port=request.client.port if request.client is not None else None, | ||
endpoint_name=endpoint_name, | ||
) # type: ignore | ||
|
||
db_session.add(request_instance) | ||
db_session.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import pytest | ||
import sqlalchemy as sqla | ||
|
||
from data_inclusion.api.request import models | ||
|
||
|
||
@pytest.mark.with_token | ||
def test_save_api_request_with_token(api_client, db_session): | ||
url = "/api/v0/structures/foo/bar?baz=1" | ||
response = api_client.get(url) | ||
|
||
assert response.status_code == 404 | ||
assert ( | ||
db_session.scalar(sqla.select(sqla.func.count()).select_from(models.Request)) | ||
== 1 | ||
) | ||
|
||
request_instance = db_session.scalars(sqla.select(models.Request)).first() | ||
assert request_instance.status_code == 404 | ||
assert request_instance.user == "some_user" | ||
assert request_instance.path == "/api/v0/structures/foo/bar" | ||
assert request_instance.method == "GET" | ||
assert request_instance.path_params == {"source": "foo", "id": "bar"} | ||
assert request_instance.query_params == {"baz": "1"} | ||
assert request_instance.endpoint_name == "retrieve_structure_endpoint" | ||
|
||
|
||
@pytest.mark.with_token | ||
def test_ignore_redirect(api_client, db_session): | ||
url = "/api/v0/structures/" | ||
response = api_client.get(url) | ||
|
||
assert response.status_code == 200 | ||
assert ( | ||
db_session.scalar(sqla.select(sqla.func.count()).select_from(models.Request)) | ||
== 1 | ||
) | ||
|
||
|
||
def test_save_api_request_without_token(api_client, db_session): | ||
url = "/api/v0/structures" | ||
response = api_client.get(url) | ||
|
||
assert response.status_code == 403 | ||
assert ( | ||
db_session.scalar(sqla.select(sqla.func.count()).select_from(models.Request)) | ||
== 1 | ||
) | ||
|
||
request_instance = db_session.scalars(sqla.select(models.Request)).first() | ||
assert request_instance.status_code == 403 | ||
assert request_instance.user is None | ||
assert request_instance.path == "/api/v0/structures" | ||
assert request_instance.method == "GET" | ||
assert request_instance.path_params == {} | ||
assert request_instance.query_params == {} | ||
assert request_instance.endpoint_name == "list_structures_endpoint" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters