-
Notifications
You must be signed in to change notification settings - Fork 2
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
HDX-9848 currency endpoint #115
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 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,33 @@ | ||
import logging | ||
from typing import Optional | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from sqlalchemy import select | ||
|
||
from hdx_hapi.db.models.views.all_views import CurrencyView | ||
from hdx_hapi.db.dao.util.util import apply_pagination, case_insensitive_filter | ||
from hdx_hapi.endpoints.util.util import PaginationParams | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def currencies_view_list( | ||
pagination_parameters: PaginationParams, | ||
db: AsyncSession, | ||
code: Optional[str] = None, | ||
): | ||
logger.info(f'currency_view_list called with params: code={code}') | ||
|
||
query = select(CurrencyView) | ||
if code: | ||
query = case_insensitive_filter(query, CurrencyView.code, code) | ||
|
||
query = apply_pagination(query, pagination_parameters) | ||
|
||
logger.debug(f'Executing SQL query: {query}') | ||
|
||
result = await db.execute(query) | ||
currencies = result.scalars().all() | ||
|
||
logger.info(f'Retrieved {len(currencies)} rows from the database') | ||
|
||
return currencies |
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,49 @@ | ||
from typing import Annotated | ||
from fastapi import Depends, Query, APIRouter | ||
|
||
|
||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from hdx_hapi.config.doc_snippets import DOC_CURRENCY_CODE | ||
|
||
from hdx_hapi.endpoints.models.base import HapiGenericResponse | ||
from hdx_hapi.endpoints.models.currency import CurrencyResponse | ||
from hdx_hapi.endpoints.util.util import ( | ||
CommonEndpointParams, | ||
OutputFormat, | ||
common_endpoint_parameters, | ||
) | ||
from hdx_hapi.services.csv_transform_logic import transform_result_to_csv_stream_if_requested | ||
|
||
from hdx_hapi.services.currency_logic import get_currencies_srv | ||
from hdx_hapi.services.sql_alchemy_session import get_db | ||
|
||
router = APIRouter( | ||
tags=['Metadata'], | ||
) | ||
|
||
|
||
@router.get( | ||
'/api/metadata/currency', | ||
response_model=HapiGenericResponse[CurrencyResponse], | ||
summary='Get information about how currencies are classified', | ||
include_in_schema=False, | ||
) | ||
@router.get( | ||
'/api/v1/metadata/currency', | ||
response_model=HapiGenericResponse[CurrencyResponse], | ||
summary='Get information about how currencies are classified', | ||
) | ||
async def get_currencies( | ||
common_parameters: Annotated[CommonEndpointParams, Depends(common_endpoint_parameters)], | ||
db: AsyncSession = Depends(get_db), | ||
code: Annotated[ | ||
str, Query(max_length=32, description=f'{DOC_CURRENCY_CODE}', openapi_examples={'usd': {'value': 'usd'}}) | ||
] = None, | ||
output_format: OutputFormat = OutputFormat.JSON, | ||
): | ||
result = await get_currencies_srv( | ||
pagination_parameters=common_parameters, | ||
db=db, | ||
code=code, | ||
) | ||
return transform_result_to_csv_stream_if_requested(result, output_format, CurrencyResponse) |
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,9 @@ | ||
from pydantic import ConfigDict, Field | ||
from hdx_hapi.endpoints.models.base import HapiBaseModel | ||
|
||
|
||
class CurrencyResponse(HapiBaseModel): | ||
code: str = Field(max_length=32) | ||
name: str = Field(max_length=512) | ||
|
||
model_config = ConfigDict(from_attributes=True) |
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,17 @@ | ||
from typing import Optional | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from hdx_hapi.db.dao.currency_view_dao import currencies_view_list | ||
from hdx_hapi.endpoints.util.util import PaginationParams | ||
|
||
|
||
async def get_currencies_srv( | ||
pagination_parameters: PaginationParams, | ||
db: AsyncSession, | ||
code: Optional[str] = None, | ||
): | ||
return await currencies_view_list( | ||
pagination_parameters=pagination_parameters, | ||
db=db, | ||
code=code, | ||
) |
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
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,6 @@ | ||
-- dummy data | ||
INSERT INTO currency (code, name) | ||
VALUES | ||
('USD', 'United states dollar'), | ||
('RON', 'Romanian leu'), | ||
('EUR', 'Euro'); |
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,60 @@ | ||
import pytest | ||
import logging | ||
|
||
from httpx import AsyncClient | ||
from main import app | ||
from tests.test_endpoints.endpoint_data import endpoint_data | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
ENDPOINT_ROUTER = '/api/v1/metadata/currency' | ||
endpoint_data = endpoint_data[ENDPOINT_ROUTER] | ||
query_parameters = endpoint_data['query_parameters'] | ||
expected_fields = endpoint_data['expected_fields'] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_get_currencies(event_loop, refresh_db): | ||
log.info('started test_get_currencies') | ||
async with AsyncClient(app=app, base_url='http://test') as ac: | ||
response = await ac.get(ENDPOINT_ROUTER) | ||
assert response.status_code == 200 | ||
assert len(response.json()['data']) > 0, 'There should be at least one currency in the database' | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_get_currency_params(event_loop, refresh_db): | ||
log.info('started test_get_currency_params') | ||
|
||
for param_name, param_value in query_parameters.items(): | ||
async with AsyncClient(app=app, base_url='http://test', params={param_name: param_value}) as ac: | ||
response = await ac.get(ENDPOINT_ROUTER) | ||
|
||
assert response.status_code == 200 | ||
assert len(response.json()['data']) > 0, ( | ||
f'There should be at least one currency entry for parameter "{param_name}" with value "{param_value}" ' | ||
'in the database' | ||
) | ||
|
||
async with AsyncClient(app=app, base_url='http://test', params=query_parameters) as ac: | ||
response = await ac.get(ENDPOINT_ROUTER) | ||
|
||
assert response.status_code == 200 | ||
assert ( | ||
len(response.json()['data']) > 0 | ||
), 'There should be at least one currency entry for all parameters in the database' | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_get_currency_result(event_loop, refresh_db): | ||
log.info('started test_get_currency_result') | ||
|
||
async with AsyncClient(app=app, base_url='http://test', params=query_parameters) as ac: | ||
response = await ac.get(ENDPOINT_ROUTER) | ||
|
||
for field in expected_fields: | ||
assert field in response.json()['data'][0], f'Field "{field}" not found in the response' | ||
|
||
assert len(response.json()['data'][0]) == len( | ||
expected_fields | ||
), 'Response has a different number of fields than expected' |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you attempting to check for case invariance here or is "usD" a typo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initially I did it with sensitive, but i checked other endpoints and switch to insensitive. we can easy change if DS team give us feedback