Skip to content
Snippets Groups Projects
Verified Commit 86c2b0bc authored by Erwan Rouchet's avatar Erwan Rouchet
Browse files

Detect x-paginated in the API schema

parent 866a883d
Branches
Tags
1 merge request!207Detect x-paginated in the API schema
Pipeline #29131 passed
......@@ -116,6 +116,10 @@ class ArkindexClient(apistar.Client):
operation = _find_operation(schema, link_info.link.name)
link_info.link.deprecated = operation.get("deprecated", False)
# Detect paginated links
if "x-paginated" in operation:
link_info.link._paginated = operation["x-paginated"]
# Remove domains from each endpoint; allows APIStar to properly handle our base URL
# https://github.com/encode/apistar/issues/657
original_url = urlsplit(link_info.link.url)
......@@ -176,14 +180,18 @@ class ArkindexClient(apistar.Client):
# Add the Referer header to allow Django CSRF to function
self.transport.headers.setdefault("Referer", self.document.url)
def paginate(self, *args, **kwargs):
def paginate(self, operation_id, *args, **kwargs):
"""
Perform a usual request as done by APIStar, but handle paginated endpoints.
:return: An iterator for a paginated endpoint.
:rtype: arkindex.pagination.ResponsePaginator
:rtype: Union[arkindex.pagination.ResponsePaginator, dict, list]
"""
return ResponsePaginator(self, *args, **kwargs)
link = self.lookup_operation(operation_id)
# If there was no x-paginated, trust the caller and assume the endpoint is paginated
if getattr(link, "_paginated", True):
return ResponsePaginator(self, operation_id, *args, **kwargs)
return self.request(operation_id, *args, **kwargs)
def login(self, email, password):
"""
......
......@@ -223,6 +223,137 @@
}
}
}
},
"/api/v1/element/{id}/metadata/": {
"get": {
"operationId": "ListElementMetaData",
"description": "List all metadata linked to an element.",
"parameters": [
{
"in": "path",
"name": "id",
"schema": {
"type": "string",
"format": "uuid"
},
"required": true
}
],
"tags": [
"elements"
],
"security": [
{
"cookieAuth": []
},
{
"tokenAuth": []
},
{
"agentAuth": []
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"description": "Allow editing MetaData",
"properties": {
"id": {
"type": "string",
"format": "uuid",
"readOnly": true
},
"type": {
"enum": [
"text",
"markdown",
"date",
"location",
"reference",
"numeric"
],
"type": "string"
},
"name": {
"type": "string",
"maxLength": 250
},
"value": {
"type": "string"
},
"dates": {
"type": "array",
"items": {
"type": "object",
"description": "Serialize a list of interpreted dates linked to a metadata",
"properties": {
"type": {
"enum": [
"exact",
"lower",
"upper",
"unknown"
],
"type": "string"
},
"year": {
"type": "integer",
"minimum": 0
},
"month": {
"type": "integer",
"maximum": 12,
"minimum": 1,
"nullable": true
},
"day": {
"type": "integer",
"maximum": 31,
"minimum": 1,
"nullable": true
}
},
"required": [
"day",
"month",
"type",
"year"
]
},
"readOnly": true
},
"entity": {
"type": "string",
"format": "uuid",
"nullable": true
},
"worker_version": {
"type": "string",
"format": "uuid",
"nullable": true
}
},
"required": [
"dates",
"id",
"name",
"type",
"value"
]
}
}
}
},
"description": ""
}
},
"x-paginated": false
}
}
},
"components": {
......
......@@ -67,6 +67,7 @@ def test_schema_url():
assert [link.name for link in cli.document.walk_links()] == [
"UploadDataFile",
"elements:ListElements",
"elements:ListElementMetaData",
"imports:ListProcessElements",
]
......@@ -77,5 +78,6 @@ def test_local_path():
assert [link.name for link in cli.document.walk_links()] == [
"UploadDataFile",
"elements:ListElements",
"elements:ListElementMetaData",
"imports:ListProcessElements",
]
......@@ -214,3 +214,16 @@ def test_cursor_pagination_zero_results(mock_schema):
assert len(paginator) == 0
assert list(paginator) == []
assert len(responses.calls) == 2
def test_paginate_x_paginated(mock_schema):
responses.add(
responses.GET,
"https://dummy.test/api/v1/element/element_id/metadata/",
match_querystring=True,
json=["a", "b"],
)
cli = ArkindexClient("t0k3n", base_url="https://dummy.test")
assert cli.paginate("ListElementMetaData", id="element_id") == ["a", "b"]
assert len(responses.calls) == 2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment