diff --git a/backend/app/core/__init__.py b/backend/app/core/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/backend/app/core/config.py b/backend/app/core/config.py deleted file mode 100644 index e3ed20f..0000000 --- a/backend/app/core/config.py +++ /dev/null @@ -1,10 +0,0 @@ -from pydantic_settings import BaseSettings - -class Settings(BaseSettings): - API_V1_STR: str = "/api/v1" - PROJECT_NAME: str = "House Price Predictor" - - class Config: - case_sensitive = True - -settings = Settings() \ No newline at end of file diff --git a/backend/app/errors/base_error.py b/backend/app/errors/base_error.py new file mode 100644 index 0000000..e633d1a --- /dev/null +++ b/backend/app/errors/base_error.py @@ -0,0 +1,9 @@ +from fastapi import HTTPException, status + + +class BaseError(HTTPException): + status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR + + def __init__(self, detail: str): + self.detail = detail + super().__init__(status_code=self.status_code, detail=self.detail) \ No newline at end of file diff --git a/backend/app/errors/not_authenticated.py b/backend/app/errors/not_authenticated.py new file mode 100644 index 0000000..b5701ac --- /dev/null +++ b/backend/app/errors/not_authenticated.py @@ -0,0 +1,10 @@ +from fastapi import status + +from .base_error import BaseError + + +class NotAuthenticatedError(BaseError): + status_code: int = status.HTTP_401_UNAUTHORIZED + + def __init__(self): + super().__init__("Not authenticated") \ No newline at end of file diff --git a/backend/app/providers/auth_provider.py b/backend/app/providers/auth_provider.py new file mode 100644 index 0000000..34ce622 --- /dev/null +++ b/backend/app/providers/auth_provider.py @@ -0,0 +1,38 @@ +from ..errors.not_authenticated import NotAuthenticatedError +from ..models.user import User +from ..settings import get_settings + + +class AuthProvider: + """ + Provides authentication methods for the API. + """ + + def __init__( + self, + ) -> None: + if not get_settings().environment == "development": + raise NotImplementedError("AuthProvider is only implemented for development environment.") + + self._authenticated_user = self._get_mocked_user() + + + def _get_mocked_user(self): + return User( + email="test@test.com", + username="test", + password_hash="test", + ) + + @property + def is_authenticated(self) -> bool: + return self._authenticated_user is not None + + @property + def user(self) -> User: + """ + Returns the authenticated user. + """ + if not self._authenticated_user: + raise NotAuthenticatedError() + return self._authenticated_user diff --git a/backend/app/routers/houses.py b/backend/app/routers/houses.py index 32d1f49..cd40957 100644 --- a/backend/app/routers/houses.py +++ b/backend/app/routers/houses.py @@ -1,11 +1,13 @@ -from fastapi import APIRouter +from fastapi import APIRouter, Depends +from typing import Annotated +from ..providers.auth_provider import AuthProvider router = APIRouter() @router.post("") -async def create_house(): - raise NotImplementedError("This endpoint is not implemented yet.") +async def create_house(auth_provider: Annotated[AuthProvider, Depends()]): + return auth_provider.user -@router.get("/all") +@router.get("") async def get_all_houses(): raise NotImplementedError("This endpoint is not implemented yet.") \ No newline at end of file diff --git a/backend/app/settings.py b/backend/app/settings.py index b959546..1426540 100644 --- a/backend/app/settings.py +++ b/backend/app/settings.py @@ -10,11 +10,10 @@ import os load_dotenv() class _BaseConfig(BaseSettings): - pass - -class _AppSettings(_BaseConfig): environment: str = Field(default=os.getenv("ENVIRONMENT", "development")) - + +class _AppSettings(_BaseConfig): + pass class _DbSettings(_BaseConfig): username: str = Field(default=os.getenv("PG_USER"))