-
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.
Implement Core Path of Exile APIs (#21)
* feat: initialize PoE APIs, add 'get category' API * chore: use json-compatible types when preparing response data * feat: add initial implementation for get items API - refactor core Item model's fields into ItemBase schema model - use ItemBase as response model type * feat: implement filter-sort and pagination * chore: add openapi responses * refactor: standardize categories API's response structure - add nested key to data values - exclude unneeded group field from response - define apt parent and member columns
- Loading branch information
1 parent
a42ee2c
commit 491f40c
Showing
5 changed files
with
196 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,36 @@ | ||
from fastapi import APIRouter, Depends, Query | ||
|
||
from src import dependencies as deps | ||
from src.schemas.web_responses import users as resp | ||
from src.schemas.requests import FilterSortInput, PaginationInput | ||
from src.schemas.web_responses import poe as resp | ||
from src.schemas.responses import AppResponse, BaseResponse | ||
from src.services import poe as service | ||
from src.utils.routers import create_pagination_response | ||
|
||
|
||
dependencies = [Depends(deps.check_access_token)] | ||
router = APIRouter(prefix="/poe", tags=["Path of Exile"], dependencies=dependencies) | ||
|
||
|
||
# TODO: add responses | ||
@router.get("/categories", responses=resp.GET_USERS_RESPONSES) | ||
@router.get("/categories", responses=resp.GET_CATEGORIES_RESPONSES) | ||
async def get_all_categories(): | ||
"""Gets a list of all item categories from the database, mapped by their group names.""" | ||
|
||
item_categories = await service.get_item_categories() | ||
item_category_mapping = service.group_item_categories(item_categories) | ||
|
||
return AppResponse(BaseResponse(data=item_category_mapping)) | ||
return AppResponse(BaseResponse(data=item_category_mapping, key="category_groups")) | ||
|
||
|
||
@router.get("/items", responses=resp.GET_USERS_RESPONSES) | ||
async def get_items_by_group(category_group: str = Query(..., min_length=3, max_length=50)): | ||
@router.get("/items", responses=resp.GET_ITEMS_RESPONSES) | ||
async def get_items_by_group( | ||
category_group: str | None = Query(None, min_length=3, max_length=50), | ||
pagination: PaginationInput = Depends(), # using Depends allows us to encapsulate Query params within Pydantic models | ||
filter_sort_input: FilterSortInput | None = None, | ||
): | ||
"""Gets a list of all items belonging to the given category group.""" | ||
|
||
items = await service.get_items_by_group(category_group) | ||
return AppResponse(BaseResponse(data=items)) | ||
items, total_items = await service.get_items(category_group, pagination, filter_sort_input) | ||
|
||
response = create_pagination_response(items, total_items, pagination, "items") | ||
return AppResponse(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
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,113 @@ | ||
from typing import Any, Dict | ||
|
||
from src.schemas.web_responses.common import COMMON_RESPONSES | ||
|
||
|
||
GET_CATEGORIES_RESPONSES: Dict[int | str, Dict[str, Any]] = { | ||
200: { | ||
"content": { | ||
"application/json": { | ||
"example": { | ||
"error": "null", | ||
"data": { | ||
"category_groups": [ | ||
{ | ||
"group": "Currency", | ||
"members": [ | ||
{"name": "Currency", "internal_name": "Currency"}, | ||
{"name": "Fragments", "internal_name": "Fragment"}, | ||
], | ||
} | ||
] | ||
}, | ||
} | ||
} | ||
} | ||
}, | ||
**COMMON_RESPONSES, | ||
} | ||
|
||
GET_ITEMS_RESPONSES: Dict[int | str, Dict[str, Any]] = { | ||
200: { | ||
"content": { | ||
"application/json": { | ||
"example": { | ||
"error": None, | ||
"pagination": {"page": 1, "per_page": 1, "total_items": 27431, "total_pages": 27431}, | ||
"data": { | ||
"items": [ | ||
{ | ||
"poe_ninja_id": 81, | ||
"id_type": "pay", | ||
"name": "Mirror Shard", | ||
"price": { | ||
"price": "4", | ||
"currency": "chaos", | ||
"price_history": { | ||
"2024-06-16T14:33:41.439096Z": "3", | ||
"2024-06-17T14:33:41.439099Z": "3", | ||
"2024-06-18T14:33:41.439101Z": "3", | ||
"2024-06-19T14:33:41.439104Z": "4", | ||
"2024-06-20T14:33:41.439106Z": "4", | ||
"2024-06-21T14:33:41.439108Z": "4", | ||
"2024-06-22T14:33:41.439110Z": "4", | ||
}, | ||
"price_history_currency": "chaos", | ||
"price_prediction": { | ||
"2024-06-23T14:33:41.438877Z": "5365.58", | ||
"2024-06-24T14:33:41.438877Z": "5402.02", | ||
"2024-06-25T14:33:41.438877Z": "5435.12", | ||
"2024-06-26T14:33:41.438877Z": "5464.90", | ||
}, | ||
"price_prediction_currency": "chaos", | ||
}, | ||
"type_": None, | ||
"variant": None, | ||
"icon_url": "https://web.poecdn.com/6604b7aa32/MirrorShard.png", | ||
"enabled": True, | ||
} | ||
] | ||
}, | ||
} | ||
} | ||
} | ||
}, | ||
400: { | ||
"content": { | ||
"application/json": { | ||
"example": { | ||
"error": { | ||
"type": "invalid_input", | ||
"message": "Invalid category group.", | ||
"fields": None, | ||
}, | ||
} | ||
} | ||
}, | ||
"data": None, | ||
}, | ||
422: { | ||
"content": { | ||
"application/json": { | ||
"example": { | ||
"data": None, | ||
"error": { | ||
"type": "validation_error", | ||
"message": "Input failed validation.", | ||
"fields": [ | ||
{"error_type": "greater_than", "field": "page"}, | ||
{"error_type": "expected_int", "field": "page"}, | ||
{"error_type": "greater_than", "field": "per_page"}, | ||
{"error_type": "less_than_equal", "field": "per_page"}, | ||
{"error_type": "expected_int", "field": "per_page"}, | ||
{"error_type": "list_type", "field": "filter"}, | ||
{"error_type": "list_type", "field": "sort"}, | ||
], | ||
}, | ||
} | ||
} | ||
}, | ||
"data": None, | ||
}, | ||
**COMMON_RESPONSES, | ||
} |
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