Skip to content

Commit

Permalink
fix(middleware): fix middleware csc export
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxin688 committed May 19, 2024
1 parent d39e56e commit c59b432
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/libs/redis/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def set_response_headers(self, response: Response, cache_hit: bool, ttl: int | N
redis_client: FastapiCache = None


def _get_cache_key(func: Callable, *args: P.args, **kwargs: P.kwargs) -> str:
def _get_cache_key(func: Callable, *args: Any, **kwargs: Any) -> str:
sig = signature(func)
type_hints = get_type_hints(func)
func_args = _get_func_args(sig, *args, **kwargs)
Expand All @@ -154,7 +154,7 @@ def _get_cache_key(func: Callable, *args: P.args, **kwargs: P.kwargs) -> str:
return md5(f"{func.__module__}.{func.__name__}({args_str})".encode()).hexdigest() # noqa: S324


def _get_func_args(sig: Signature, *args: P.args, **kwargs: P.kwargs) -> OrderedDict[str, Any]:
def _get_func_args(sig: Signature, *args: Any, **kwargs: Any) -> OrderedDict[str, Any]:
func_args = sig.bind(*args, **kwargs)
func_args.apply_defaults()
return func_args.arguments
Expand All @@ -170,7 +170,7 @@ async def _get_api_response_async(
)


def cache(*, expire: int | None = 600): # noqa: ANN201
def cache(*, expire: int = 600): # noqa: ANN201
def outer(func: Callable[P, Awaitable[R]]) -> Callable[P, Awaitable[R, Response]]:
@wraps(func)
async def inner(*args: P.args, **kwargs: P.kwargs) -> R | Response:
Expand Down
6 changes: 3 additions & 3 deletions src/register/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import uuid
from collections.abc import Callable
from dataclasses import dataclass, field
from datetime import datetime
from datetime import UTC, datetime

import pandas as pd
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
Expand Down Expand Up @@ -40,11 +40,11 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -
response_data = _res.decode()
if response_data:
response_data = json.loads(response_data)
csv_result = response_data.get("data", {}).get("results", [])
csv_result = response_data.get("results", [])
df = pd.DataFrame(csv_result)
output = io.StringIO()
df.to_csv(output, encoding="utf-8", index=False)
filename = f"exporting_data_{datetime.utcnow().strftime('%Y%m%d %H%M%S')}.csv"
filename = f"exporting_data_{datetime.now(tz=UTC).strftime('%Y%m%d %H%M%S')}.csv"
csv_resp = StreamingResponse(iter([output.getvalue()]), media_type="application/otect-stream")
csv_resp.headers["Content-Disposition"] = f'attachment; filename="{filename}.csv"'
csv_resp.headers[self.id_header] = request_id
Expand Down

0 comments on commit c59b432

Please sign in to comment.