Skip to content

Commit 2674f27

Browse files
authored
fix: operations api licenses limit and offset parameters (#1540)
1 parent 4d45d21 commit 2674f27

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

docs/OperationsAPI.yaml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,21 @@ paths:
277277
- "licenses"
278278
operationId: getLicenses
279279
parameters:
280-
- $ref: "#/components/parameters/limit_query_param_licenses_endpoint"
281-
- $ref: "#/components/parameters/offset"
280+
- name: offset
281+
in: query
282+
description: Number of items to skip for pagination. Defined as string rather than integer due to https://github.com/OpenAPITools/openapi-generator/issues/21905
283+
required: false
284+
schema:
285+
type: string
286+
default: "0"
287+
- name: limit
288+
in: query
289+
description: Maximum number of items to return. Defined as string rather than integer due to https://github.com/OpenAPITools/openapi-generator/issues/21905
290+
required: false
291+
schema:
292+
type: string
293+
default: "50"
294+
example: "100"
282295
- $ref: "#/components/parameters/search_text_query_param_license"
283296
- $ref: "#/components/parameters/license_is_spdx_query_param"
284297
security:

functions-python/operations_api/src/feeds_operations/impl/licenses_api_impl.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class LicensesApiImpl(BaseLicensesApi):
4040
"""
4141

4242
@with_db_session
43-
async def get_license(
43+
def handle_get_license(
4444
self,
4545
id: StrictStr,
4646
db_session: Session = None,
@@ -59,11 +59,21 @@ async def get_license(
5959
# Build Pydantic model from ORM object attributes
6060
return LicenseWithRulesImpl.from_orm(license_orm)
6161

62+
async def get_license(
63+
self,
64+
id: StrictStr,
65+
) -> LicenseWithRules:
66+
"""Get the specified license from the Mobility Database.
67+
68+
Raises 404 if the license is not found.
69+
"""
70+
return self.handle_get_license(id)
71+
6272
@with_db_session
63-
async def get_licenses(
73+
def handle_get_licenses(
6474
self,
65-
limit: int,
66-
offset: int,
75+
offset: str = "0",
76+
limit: str = "100",
6777
search_query: Optional[StrictStr] = None,
6878
is_spdx: Optional[bool] = None,
6979
db_session: Session = None,
@@ -83,6 +93,15 @@ async def get_licenses(
8393
is_spdx,
8494
)
8595

96+
try:
97+
limit_int = int(limit)
98+
offset_int = int(offset)
99+
except (TypeError, ValueError):
100+
logging.error(
101+
"Invalid pagination parameters: limit=%s offset=%s", limit, offset
102+
)
103+
raise HTTPException(status_code=400, detail="Invalid limit or offset")
104+
86105
query = db_session.query(OrmLicense)
87106

88107
# Text search by name or id
@@ -101,9 +120,24 @@ async def get_licenses(
101120
if is_spdx is not None:
102121
query = query.filter(OrmLicense.is_spdx == is_spdx)
103122

104-
query = query.order_by(OrmLicense.id).offset(offset).limit(limit)
123+
query = query.order_by(OrmLicense.id).offset(offset_int).limit(limit_int)
105124
items: List[OrmLicense] = query.all()
106125

107126
logging.info("Fetched %d licenses", len(items))
108127

109128
return [LicenseBaseImpl.from_orm(item) for item in items]
129+
130+
async def get_licenses(
131+
self,
132+
offset: str = "0",
133+
limit: str = "100",
134+
search_query: Optional[StrictStr] = None,
135+
is_spdx: Optional[bool] = None,
136+
) -> List[LicenseBase]:
137+
"""Get the list of licenses from the Mobility Database.
138+
139+
Supports pagination via `limit` and `offset`, optional
140+
case-insensitive text search on license name / id, and
141+
optional filtering by SPDX status.
142+
"""
143+
return self.handle_get_licenses(offset, limit, search_query, is_spdx)

0 commit comments

Comments
 (0)