Spaces:
Sleeping
Sleeping
Download utils/common.py from WildOjisan/FastAPI2604_HF: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/spaces/WildOjisan/FastAPI2604_HF/resolve/main/utils/common.py
- Command line
-
hf download hf://spaces/WildOjisan/FastAPI2604_HF/utils/common.py
-
curl -L -o common.py https://huggingface.co/spaces/WildOjisan/FastAPI2604_HF/resolve/main/utils/common.py
1.21 kB
| import os | |
| from datetime import date, datetime, timedelta, timezone | |
| from typing import Any, Generic, Optional, TypeVar | |
| from dotenv import load_dotenv | |
| from jose import jwt | |
| from pydantic import BaseModel | |
| load_dotenv() | |
| T = TypeVar("T") | |
| class CommonResponse(BaseModel, Generic[T]): | |
| success: bool=True | |
| data: Optional[T] = None | |
| msg: str = "" | |
| def _json_default(value: Any): | |
| if isinstance(value, (datetime, date)): | |
| return value.isoformat() | |
| raise TypeError(f"{type(value).__name__} is not JSON serializable") | |
| def _json_safe_dict(data: dict) -> dict: | |
| return { | |
| key: _json_default(value) if isinstance(value, (datetime, date)) else value | |
| for key, value in data.items() | |
| } | |
| # = 60분 * 24시간 * 7일 | |
| def create_jwt_token(data: dict, expires_minutes: int = 60 * 24 * 365) -> str: | |
| secret_key = os.getenv("JWT_SECRET_KEY", "change-this-jwt-secret-key") | |
| algorithm = "HS256" | |
| now = datetime.now(timezone.utc) | |
| payload = { | |
| **_json_safe_dict(data), | |
| "iat": now, | |
| "exp": now + timedelta(minutes=expires_minutes), | |
| } | |
| return jwt.encode(payload, secret_key, algorithm=algorithm) | |