Skip to content

Commit 3d09a8f

Browse files
Merge pull request #89 from OCHA-DAP/feature/HDX-9804_response_in_data_key
HDX-9804 Put responses into a "data" key
2 parents d670620 + 492bdda commit 3d09a8f

31 files changed

+427
-254
lines changed

hdx_hapi/endpoints/get_admin_level.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from typing import Annotated
12
from datetime import date
2-
from typing import List, Annotated
3+
34
from fastapi import Depends, Query, APIRouter
45
from pydantic import NaiveDatetime
56

@@ -20,6 +21,7 @@
2021
DOC_HAPI_REPLACED_DATE_MAX,
2122
)
2223

24+
from hdx_hapi.endpoints.models.base import HapiGenericResponse
2325
from hdx_hapi.endpoints.models.admin_level import Admin1Response, Admin2Response, LocationResponse
2426
from hdx_hapi.endpoints.util.util import (
2527
CommonEndpointParams,
@@ -41,13 +43,13 @@
4143

4244
@router.get(
4345
'/api/location',
44-
response_model=List[LocationResponse],
46+
response_model=HapiGenericResponse[LocationResponse],
4547
summary='Get the list of locations (typically countries) included in HAPI',
4648
include_in_schema=False,
4749
)
4850
@router.get(
4951
'/api/v1/location',
50-
response_model=List[LocationResponse],
52+
response_model=HapiGenericResponse[LocationResponse],
5153
summary='Get the list of locations (typically countries) included in HAPI',
5254
)
5355
async def get_locations(
@@ -94,13 +96,13 @@ async def get_locations(
9496

9597
@router.get(
9698
'/api/admin1',
97-
response_model=List[Admin1Response],
99+
response_model=HapiGenericResponse[Admin1Response],
98100
summary='Get the list of first-level subnational administrative divisions available in HAPI',
99101
include_in_schema=False,
100102
)
101103
@router.get(
102104
'/api/v1/admin1',
103-
response_model=List[Admin1Response],
105+
response_model=HapiGenericResponse[Admin1Response],
104106
summary='Get the list of first-level subnational administrative divisions available in HAPI',
105107
)
106108
async def get_admin1(
@@ -151,13 +153,13 @@ async def get_admin1(
151153

152154
@router.get(
153155
'/api/admin2',
154-
response_model=List[Admin2Response],
156+
response_model=HapiGenericResponse[Admin2Response],
155157
summary='Get the list of second-level administrative divisions available in HAPI',
156158
include_in_schema=False,
157159
)
158160
@router.get(
159161
'/api/v1/admin2',
160-
response_model=List[Admin2Response],
162+
response_model=HapiGenericResponse[Admin2Response],
161163
summary='Get the list of second-level administrative divisions available in HAPI',
162164
)
163165
async def get_admin2(

hdx_hapi/endpoints/get_demographic.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Annotated
1+
from typing import Annotated
22
from fastapi import Depends, Query, APIRouter
33

44

@@ -11,6 +11,7 @@
1111
DOC_GENDER_SUMMARY,
1212
)
1313

14+
from hdx_hapi.endpoints.models.base import HapiGenericResponse
1415
from hdx_hapi.endpoints.models.demographic import AgeRangeResponse, GenderResponse
1516
from hdx_hapi.endpoints.util.util import CommonEndpointParams, common_endpoint_parameters, OutputFormat
1617
from hdx_hapi.services.age_range_logic import get_age_ranges_srv
@@ -25,9 +26,14 @@
2526

2627

2728
@router.get(
28-
'/api/age_range', response_model=List[AgeRangeResponse], summary=f'{DOC_AGE_RANGE_SUMMARY}', include_in_schema=False
29+
'/api/age_range',
30+
response_model=HapiGenericResponse[AgeRangeResponse],
31+
summary=f'{DOC_AGE_RANGE_SUMMARY}',
32+
include_in_schema=False,
33+
)
34+
@router.get(
35+
'/api/v1/age_range', response_model=HapiGenericResponse[AgeRangeResponse], summary=f'{DOC_AGE_RANGE_SUMMARY}'
2936
)
30-
@router.get('/api/v1/age_range', response_model=List[AgeRangeResponse], summary=f'{DOC_AGE_RANGE_SUMMARY}')
3137
async def get_age_ranges(
3238
common_parameters: Annotated[CommonEndpointParams, Depends(common_endpoint_parameters)],
3339
db: AsyncSession = Depends(get_db),
@@ -49,9 +55,12 @@ async def get_age_ranges(
4955

5056

5157
@router.get(
52-
'/api/gender', response_model=List[GenderResponse], summary=f'{DOC_GENDER_SUMMARY}', include_in_schema=False
58+
'/api/gender',
59+
response_model=HapiGenericResponse[GenderResponse],
60+
summary=f'{DOC_GENDER_SUMMARY}',
61+
include_in_schema=False,
5362
)
54-
@router.get('/api/v1/gender', response_model=List[GenderResponse], summary=f'{DOC_GENDER_SUMMARY}')
63+
@router.get('/api/v1/gender', response_model=HapiGenericResponse[GenderResponse], summary=f'{DOC_GENDER_SUMMARY}')
5564
async def get_genders(
5665
common_parameters: Annotated[CommonEndpointParams, Depends(common_endpoint_parameters)],
5766
db: AsyncSession = Depends(get_db),

hdx_hapi/endpoints/get_encoded_identifier.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
from typing import Annotated
33
from fastapi import APIRouter
44
from pydantic import EmailStr
5-
from hdx_hapi.services.csv_transform_logic import transform_result_to_csv_stream_if_requested
65

76
from hdx_hapi.endpoints.models.encoded_identifier import IdentifierResponse
8-
from hdx_hapi.endpoints.util.util import OutputFormat, app_name_identifier_query, email_identifier_query
7+
from hdx_hapi.endpoints.util.util import app_name_identifier_query, email_identifier_query
98

109
router = APIRouter(
1110
tags=['Utility'],
@@ -35,4 +34,5 @@ async def get_encoded_identifier(
3534
encoded_identifier = base64.b64encode(bytes(f'{application}:{email}', 'utf-8'))
3635

3736
result = {'encoded_identifier': encoded_identifier.decode('utf-8')}
38-
return transform_result_to_csv_stream_if_requested(result, OutputFormat.JSON, IdentifierResponse)
37+
return result
38+
# return transform_result_to_csv_stream_if_requested(result, OutputFormat.JSON, IdentifierResponse)

hdx_hapi/endpoints/get_food_security.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import date
2-
from typing import List, Annotated
2+
from typing import Annotated
33
from fastapi import Depends, Query, APIRouter
44
from pydantic import NaiveDatetime
55

@@ -18,6 +18,7 @@
1818
DOC_HAPI_REPLACED_DATE_MAX,
1919
)
2020

21+
from hdx_hapi.endpoints.models.base import HapiGenericResponse
2122
from hdx_hapi.endpoints.models.food_security import FoodSecurityResponse
2223
from hdx_hapi.endpoints.util.util import AdminLevel, CommonEndpointParams, OutputFormat, common_endpoint_parameters
2324
from hdx_hapi.services.csv_transform_logic import transform_result_to_csv_stream_if_requested
@@ -31,11 +32,15 @@
3132

3233
@router.get(
3334
'/api/themes/food_security',
34-
response_model=List[FoodSecurityResponse],
35+
response_model=HapiGenericResponse[FoodSecurityResponse],
3536
summary='Get food security data',
3637
include_in_schema=False,
3738
)
38-
@router.get('/api/v1/themes/food_security', response_model=List[FoodSecurityResponse], summary='Get food security data')
39+
@router.get(
40+
'/api/v1/themes/food_security',
41+
response_model=HapiGenericResponse[FoodSecurityResponse],
42+
summary='Get food security data',
43+
)
3944
async def get_food_security(
4045
common_parameters: Annotated[CommonEndpointParams, Depends(common_endpoint_parameters)],
4146
db: AsyncSession = Depends(get_db),

hdx_hapi/endpoints/get_hdx_metadata.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import date
2-
from typing import List, Annotated
2+
from typing import Annotated
33
from fastapi import Depends, Query, APIRouter
44
from pydantic import NaiveDatetime
55

@@ -24,6 +24,7 @@
2424
DOC_HAPI_REPLACED_DATE_MAX,
2525
)
2626

27+
from hdx_hapi.endpoints.models.base import HapiGenericResponse
2728
from hdx_hapi.endpoints.models.hdx_metadata import DatasetResponse, ResourceResponse
2829
from hdx_hapi.endpoints.util.util import (
2930
CommonEndpointParams,
@@ -42,13 +43,13 @@
4243

4344
@router.get(
4445
'/api/dataset',
45-
response_model=List[DatasetResponse],
46+
response_model=HapiGenericResponse[DatasetResponse],
4647
summary='Get information about the sources of the data in HAPI',
4748
include_in_schema=False,
4849
)
4950
@router.get(
5051
'/api/v1/dataset',
51-
response_model=List[DatasetResponse],
52+
response_model=HapiGenericResponse[DatasetResponse],
5253
summary='Get information about the sources of the data in HAPI',
5354
)
5455
async def get_datasets(
@@ -79,13 +80,13 @@ async def get_datasets(
7980

8081
@router.get(
8182
'/api/resource',
82-
response_model=List[ResourceResponse],
83+
response_model=HapiGenericResponse[ResourceResponse],
8384
summary='Get information about the sources of the data in HAPI',
8485
include_in_schema=False,
8586
)
8687
@router.get(
8788
'/api/v1/resource',
88-
response_model=List[ResourceResponse],
89+
response_model=HapiGenericResponse[ResourceResponse],
8990
summary='Get information about the sources of the data in HAPI',
9091
)
9192
async def get_resources(

hdx_hapi/endpoints/get_humanitarian_needs.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import date
2-
from typing import List, Annotated
2+
from typing import Annotated
33
from fastapi import Depends, Query, APIRouter
44
from pydantic import NaiveDatetime
55

@@ -28,6 +28,7 @@
2828
DOC_HAPI_REPLACED_DATE_MAX,
2929
)
3030

31+
from hdx_hapi.endpoints.models.base import HapiGenericResponse
3132
from hdx_hapi.endpoints.models.humanitarian_needs import HumanitarianNeedsResponse
3233
from hdx_hapi.endpoints.util.util import AdminLevel, CommonEndpointParams, OutputFormat, common_endpoint_parameters
3334
from hdx_hapi.services.csv_transform_logic import transform_result_to_csv_stream_if_requested
@@ -41,13 +42,13 @@
4142

4243
@router.get(
4344
'/api/themes/humanitarian_needs',
44-
response_model=List[HumanitarianNeedsResponse],
45+
response_model=HapiGenericResponse[HumanitarianNeedsResponse],
4546
summary='Get humanitarian needs data',
4647
include_in_schema=False,
4748
)
4849
@router.get(
4950
'/api/v1/themes/humanitarian_needs',
50-
response_model=List[HumanitarianNeedsResponse],
51+
response_model=HapiGenericResponse[HumanitarianNeedsResponse],
5152
summary='Get humanitarian needs data',
5253
)
5354
async def get_humanitarian_needs(

hdx_hapi/endpoints/get_humanitarian_response.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Annotated
1+
from typing import Annotated
22
from fastapi import Depends, Query, APIRouter
33

44

@@ -13,6 +13,7 @@
1313
DOC_SEE_ORG_TYPE,
1414
)
1515

16+
from hdx_hapi.endpoints.models.base import HapiGenericResponse
1617
from hdx_hapi.endpoints.models.humanitarian_response import OrgResponse, OrgTypeResponse, SectorResponse
1718
from hdx_hapi.endpoints.util.util import (
1819
CommonEndpointParams,
@@ -33,13 +34,13 @@
3334

3435
@router.get(
3536
'/api/org',
36-
response_model=List[OrgResponse],
37+
response_model=HapiGenericResponse[OrgResponse],
3738
summary='Get the list of organizations represented in the data available in HAPI',
3839
include_in_schema=False,
3940
)
4041
@router.get(
4142
'/api/v1/org',
42-
response_model=List[OrgResponse],
43+
response_model=HapiGenericResponse[OrgResponse],
4344
summary='Get the list of organizations represented in the data available in HAPI',
4445
)
4546
async def get_orgs(
@@ -80,13 +81,13 @@ async def get_orgs(
8081

8182
@router.get(
8283
'/api/org_type',
83-
response_model=List[OrgTypeResponse],
84+
response_model=HapiGenericResponse[OrgTypeResponse],
8485
summary='Get information about how organizations are classified in HAPI',
8586
include_in_schema=False,
8687
)
8788
@router.get(
8889
'/api/v1/org_type',
89-
response_model=List[OrgTypeResponse],
90+
response_model=HapiGenericResponse[OrgTypeResponse],
9091
summary='Get information about how organizations are classified in HAPI',
9192
)
9293
async def get_org_types(
@@ -112,13 +113,13 @@ async def get_org_types(
112113

113114
@router.get(
114115
'/api/sector',
115-
response_model=List[SectorResponse],
116+
response_model=HapiGenericResponse[SectorResponse],
116117
summary='Get information about how humanitarian response activities are classified',
117118
include_in_schema=False,
118119
)
119120
@router.get(
120121
'/api/v1/sector',
121-
response_model=List[SectorResponse],
122+
response_model=HapiGenericResponse[SectorResponse],
122123
summary='Get information about how humanitarian response activities are classified',
123124
)
124125
async def get_sectors(

hdx_hapi/endpoints/get_national_risk.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import date
2-
from typing import List, Annotated
2+
from typing import Annotated
33
from fastapi import Depends, Query, APIRouter
44
from pydantic import NaiveDatetime
55

@@ -19,6 +19,7 @@
1919
DOC_HAPI_REPLACED_DATE_MAX,
2020
)
2121

22+
from hdx_hapi.endpoints.models.base import HapiGenericResponse
2223
from hdx_hapi.endpoints.models.national_risk import NationalRiskResponse
2324
from hdx_hapi.endpoints.util.util import CommonEndpointParams, OutputFormat, common_endpoint_parameters
2425
from hdx_hapi.services.csv_transform_logic import transform_result_to_csv_stream_if_requested
@@ -32,11 +33,15 @@
3233

3334
@router.get(
3435
'/api/themes/national_risk',
35-
response_model=List[NationalRiskResponse],
36+
response_model=HapiGenericResponse[NationalRiskResponse],
3637
summary='Get national risk data',
3738
include_in_schema=False,
3839
)
39-
@router.get('/api/v1/themes/national_risk', response_model=List[NationalRiskResponse], summary='Get national risk data')
40+
@router.get(
41+
'/api/v1/themes/national_risk',
42+
response_model=HapiGenericResponse[NationalRiskResponse],
43+
summary='Get national risk data',
44+
)
4045
async def get_national_risks(
4146
common_parameters: Annotated[CommonEndpointParams, Depends(common_endpoint_parameters)],
4247
db: AsyncSession = Depends(get_db),

hdx_hapi/endpoints/get_operational_presence.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import date
2-
from typing import List, Annotated
2+
from typing import Annotated
33
from fastapi import Depends, Query, APIRouter
44
from pydantic import NaiveDatetime
55

@@ -20,6 +20,7 @@
2020
DOC_HAPI_REPLACED_DATE_MAX,
2121
)
2222

23+
from hdx_hapi.endpoints.models.base import HapiGenericResponse
2324
from hdx_hapi.endpoints.models.operational_presence import OperationalPresenceResponse
2425
from hdx_hapi.endpoints.util.util import AdminLevel, CommonEndpointParams, OutputFormat, common_endpoint_parameters
2526
from hdx_hapi.services.csv_transform_logic import transform_result_to_csv_stream_if_requested
@@ -38,24 +39,24 @@
3839

3940
@router.get(
4041
'/api/themes/3w',
41-
response_model=List[OperationalPresenceResponse],
42+
response_model=HapiGenericResponse[OperationalPresenceResponse],
4243
summary=SUMMARY_TEXT,
4344
include_in_schema=False,
4445
)
4546
@router.get(
4647
'/api/themes/3W',
47-
response_model=List[OperationalPresenceResponse],
48+
response_model=HapiGenericResponse[OperationalPresenceResponse],
4849
summary=SUMMARY_TEXT,
4950
include_in_schema=False,
5051
)
5152
@router.get(
5253
'/api/v1/themes/3w',
53-
response_model=List[OperationalPresenceResponse],
54+
response_model=HapiGenericResponse[OperationalPresenceResponse],
5455
summary=SUMMARY_TEXT,
5556
)
5657
@router.get(
5758
'/api/v1/themes/3W',
58-
response_model=List[OperationalPresenceResponse],
59+
response_model=HapiGenericResponse[OperationalPresenceResponse],
5960
summary=SUMMARY_TEXT,
6061
include_in_schema=False,
6162
)

0 commit comments

Comments
 (0)