-
Notifications
You must be signed in to change notification settings - Fork 0
Create endpoint to get daily questions #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| from fastapi import APIRouter | ||
|
|
||
| from app.api.routes import auth, user | ||
| from app.api.routes import auth, questions, user | ||
|
|
||
| api_router = APIRouter() | ||
|
|
||
| api_router.include_router(user.router, prefix="/users", tags=["users"]) | ||
| api_router.include_router(auth.router, prefix="/auth", tags=["auth"]) | ||
| api_router.include_router(questions.router, prefix="/questions", tags=["questions"]) |
This file contains hidden or 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,16 @@ | ||
| from fastapi import APIRouter | ||
|
|
||
| from app.settings import settings | ||
| from app.utils.http_client import make_request | ||
|
|
||
| router = APIRouter() | ||
|
|
||
|
|
||
| GENERATOR_SERVICE_URL = settings.GENERATOR_SERVICE_URL | ||
|
|
||
|
|
||
| @router.get("/daily_questions") | ||
| def get_daily_questions(): | ||
| """Fetch daily questions from the external generator service.""" | ||
| url = f"{GENERATOR_SERVICE_URL}/api/v1/questions/get_daily_question" | ||
| return make_request("GET", url) | ||
This file contains hidden or 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 hidden or 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,10 @@ | ||
| from typing import Optional | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class ErrorResponse(BaseModel): | ||
| detail: str | ||
| code: str | ||
| status_code: int | ||
| context: Optional[dict] = None |
This file contains hidden or 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 hidden or 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,49 @@ | ||
| class AppException(Exception): | ||
| """Base application exception with additional context.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| detail: str, | ||
| code: str = "app_error", | ||
| status_code: int = 400, | ||
| context: dict | None = None, | ||
| ): | ||
| self.detail = detail | ||
| self.code = code | ||
| self.status_code = status_code | ||
| self.context = context or {} | ||
| super().__init__(detail) # keeps the built-in Exception message | ||
|
|
||
|
|
||
| class RequestTimeoutError(AppException): | ||
| """Raised when a request times out.""" | ||
|
|
||
| def __init__(self, detail: str = "Request timed out", context: dict | None = None): | ||
| super().__init__( | ||
| detail=detail, code="timeout_error", status_code=408, context=context | ||
| ) | ||
|
|
||
|
|
||
| class RequestConnectionError(AppException): | ||
| """Raised when a connection to the server fails.""" | ||
|
|
||
| def __init__( | ||
| self, detail: str = "Failed to connect to server", context: dict | None = None | ||
| ): | ||
| super().__init__( | ||
| detail=detail, code="connection_error", status_code=503, context=context | ||
| ) | ||
|
|
||
|
|
||
| class RequestHTTPError(AppException): | ||
| """Raised for non-2xx HTTP responses.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| detail: str = "HTTP error occurred", | ||
| status_code: int = 500, | ||
| context: dict | None = None, | ||
| ): | ||
| super().__init__( | ||
| detail=detail, code="http_error", status_code=status_code, context=context | ||
| ) |
This file contains hidden or 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 @@ | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| import requests | ||
|
|
||
| from app.utils.exceptions import (AppException, RequestConnectionError, | ||
| RequestHTTPError, RequestTimeoutError) | ||
|
|
||
|
|
||
| def make_request( | ||
| method: str, | ||
| url: str, | ||
| params: Optional[Dict[str, Any]] = None, | ||
| data: Optional[Any] = None, | ||
| json: Optional[Any] = None, | ||
| headers: Optional[Dict[str, str]] = None, | ||
| timeout: int = 60, | ||
| ) -> Dict[str, Any]: | ||
| """ | ||
| Generic HTTP request utility. | ||
| """ | ||
| try: | ||
| response = requests.request( | ||
| method=method, | ||
| url=url, | ||
| params=params, | ||
| data=data, | ||
| json=json, | ||
| headers=headers, | ||
| timeout=timeout, | ||
| ) | ||
| response.raise_for_status() | ||
| return response.json() | ||
|
|
||
| except requests.Timeout as exc: | ||
| raise RequestTimeoutError( | ||
| "The request timed out", context={"url": url} | ||
| ) from exc | ||
|
|
||
| except requests.ConnectionError as exc: | ||
| raise RequestConnectionError( | ||
| "Failed to connect to the service", context={"url": url} | ||
| ) from exc | ||
|
|
||
| except requests.HTTPError as exc: | ||
| status_code = exc.response.status_code if exc.response else 500 | ||
| message = exc.response.text if exc.response else str(exc) | ||
| raise RequestHTTPError( | ||
| detail=message, status_code=status_code, context={"url": url} | ||
| ) from exc | ||
|
|
||
| except Exception as exc: | ||
| # Catch-all wrapped in your base AppException | ||
| raise AppException( | ||
| "Unexpected error in HTTP request", | ||
| code="unexpected_request_error", | ||
| context={"url": url, "error": str(exc)}, | ||
| ) from exc |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.