Skip to content

Commit 0a40161

Browse files
api-clients-generation-pipeline[bot]therveci.datadog-api-spec
authored
Add pagination extension to monitors (#1633)
* Support page param * Regenerate client from commit fee86b40 of spec repo --------- Co-authored-by: Thomas Hervé <[email protected]> Co-authored-by: api-clients-generation-pipeline[bot] <54105614+api-clients-generation-pipeline[bot]@users.noreply.github.com> Co-authored-by: ci.datadog-api-spec <[email protected]>
1 parent f9bda90 commit 0a40161

File tree

13 files changed

+208
-13
lines changed

13 files changed

+208
-13
lines changed

.apigentools-info

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"spec_versions": {
55
"v1": {
66
"apigentools_version": "1.6.5",
7-
"regenerated": "2023-08-30 07:29:22.537461",
8-
"spec_repo_commit": "febdee32"
7+
"regenerated": "2023-08-30 08:42:52.326487",
8+
"spec_repo_commit": "fee86b40"
99
},
1010
"v2": {
1111
"apigentools_version": "1.6.5",
12-
"regenerated": "2023-08-30 07:29:22.557428",
13-
"spec_repo_commit": "febdee32"
12+
"regenerated": "2023-08-30 08:42:52.374431",
13+
"spec_repo_commit": "fee86b40"
1414
}
1515
}
1616
}

.generator/schemas/v1/openapi.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24896,6 +24896,7 @@ paths:
2489624896
name: page_size
2489724897
required: false
2489824898
schema:
24899+
default: 100
2489924900
example: 20
2490024901
format: int32
2490124902
maximum: 1000
@@ -24931,6 +24932,9 @@ paths:
2493124932
summary: Get all monitor details
2493224933
tags:
2493324934
- Monitors
24935+
x-pagination:
24936+
limitParam: page_size
24937+
pageParam: page
2493424938
post:
2493524939
description: 'Create a monitor using the specified options.
2493624940

.generator/src/generator/openapi.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ def get_api_models(operations):
406406
yield name
407407

408408
if "x-pagination" in operation:
409-
name = get_type_at_path(operation, operation["x-pagination"]["resultsPath"])
409+
name = get_type_at_path(operation, operation["x-pagination"].get("resultsPath"))
410410
if name and name not in seen:
411411
seen.add(name)
412412
yield name
@@ -629,6 +629,8 @@ def get_type_at_path(operation, attribute_path):
629629
if content is None:
630630
raise RuntimeError("Default response not found")
631631
content = content["schema"]
632+
if not attribute_path:
633+
return get_type_for_items(content)
632634
for attr in attribute_path.split("."):
633635
content = content["properties"][attr]
634636
return get_type_for_items(content)

.generator/src/generator/templates/api.j2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,19 @@ class {{ classname }}:
206206
set_attribute_from_path(kwargs, "{{ pagination.limitParam|attribute_path }}", local_page_size, endpoint.params_map)
207207
pagination = {
208208
"limit_value": local_page_size,
209+
{%- if pagination.resultsPath %}
209210
"results_path": "{{ pagination.resultsPath|attribute_path }}",
211+
{%- endif %}
210212
{%- if pagination.cursorParam %}
211213
"cursor_param": "{{ pagination.cursorParam|attribute_path }}",
212214
"cursor_path": "{{ pagination.cursorPath }}",
213215
{%- endif %}
214216
{%- if pagination.pageOffsetParam %}
215217
"page_offset_param": "{{ pagination.pageOffsetParam|attribute_path }}",
216218
{%- endif %}
219+
{%- if pagination.pageParam %}
220+
"page_param": "{{ pagination.pageParam|attribute_path }}",
221+
{%- endif %}
217222
"endpoint": endpoint,
218223
"kwargs": kwargs,
219224
}

.generator/src/generator/templates/api_client.j2

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,13 @@ class ApiClient:
346346
host: Optional[str] = None,
347347
check_type: Optional[bool] = None,
348348
):
349+
if "page_param" in pagination:
350+
set_attribute_from_path(
351+
pagination["kwargs"],
352+
pagination["page_param"],
353+
0,
354+
pagination["endpoint"].params_map,
355+
)
349356
params = pagination["endpoint"].gather_params(pagination["kwargs"])
350357
while True:
351358
response = self.call_api(
@@ -365,9 +372,9 @@ class ApiClient:
365372
host=host,
366373
collection_formats=params["collection_format"],
367374
)
368-
for item in get_attribute_from_path(response, pagination["results_path"]):
375+
for item in get_attribute_from_path(response, pagination.get("results_path")):
369376
yield item
370-
if len(get_attribute_from_path(response, pagination["results_path"])) < pagination["limit_value"]:
377+
if len(get_attribute_from_path(response, pagination.get("results_path"))) < pagination["limit_value"]:
371378
break
372379

373380
params = self._update_paginated_params(pagination, response)
@@ -381,6 +388,13 @@ class ApiClient:
381388
+ pagination["limit_value"],
382389
pagination["endpoint"].params_map,
383390
)
391+
elif "page_param" in pagination:
392+
set_attribute_from_path(
393+
pagination["kwargs"],
394+
pagination["page_param"],
395+
get_attribute_from_path(pagination["kwargs"], pagination["page_param"], 0) + 1,
396+
pagination["endpoint"].params_map,
397+
)
384398
else:
385399
set_attribute_from_path(
386400
pagination["kwargs"],
@@ -635,9 +649,9 @@ class AsyncApiClient(ApiClient):
635649
host=host,
636650
collection_formats=params["collection_format"],
637651
)
638-
for item in get_attribute_from_path(response, pagination["results_path"]):
652+
for item in get_attribute_from_path(response, pagination.get("results_path")):
639653
yield item
640-
if len(get_attribute_from_path(response, pagination["results_path"])) < pagination["limit_value"]:
654+
if len(get_attribute_from_path(response, pagination.get("results_path"))) < pagination["limit_value"]:
641655
break
642656

643657
params = self._update_paginated_params(pagination, response)

.generator/src/generator/templates/model_utils.j2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,8 @@ class UnparsedObject(ModelNormal):
16371637

16381638
def get_attribute_from_path(obj, path, default=None):
16391639
"""Return an attribute at `path` from the passed object."""
1640+
if not path:
1641+
return obj
16401642
for elt in path.split("."):
16411643
try:
16421644
obj = obj[elt]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Get all monitor details returns "OK" response with pagination
3+
"""
4+
5+
from datadog_api_client import ApiClient, Configuration
6+
from datadog_api_client.v1.api.monitors_api import MonitorsApi
7+
8+
configuration = Configuration()
9+
with ApiClient(configuration) as api_client:
10+
api_instance = MonitorsApi(api_client)
11+
items = api_instance.list_monitors_with_pagination(
12+
page_size=2,
13+
)
14+
for item in items:
15+
print(item)

src/datadog_api_client/api_client.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,13 @@ def call_api_paginated(
346346
host: Optional[str] = None,
347347
check_type: Optional[bool] = None,
348348
):
349+
if "page_param" in pagination:
350+
set_attribute_from_path(
351+
pagination["kwargs"],
352+
pagination["page_param"],
353+
0,
354+
pagination["endpoint"].params_map,
355+
)
349356
params = pagination["endpoint"].gather_params(pagination["kwargs"])
350357
while True:
351358
response = self.call_api(
@@ -365,9 +372,9 @@ def call_api_paginated(
365372
host=host,
366373
collection_formats=params["collection_format"],
367374
)
368-
for item in get_attribute_from_path(response, pagination["results_path"]):
375+
for item in get_attribute_from_path(response, pagination.get("results_path")):
369376
yield item
370-
if len(get_attribute_from_path(response, pagination["results_path"])) < pagination["limit_value"]:
377+
if len(get_attribute_from_path(response, pagination.get("results_path"))) < pagination["limit_value"]:
371378
break
372379

373380
params = self._update_paginated_params(pagination, response)
@@ -381,6 +388,13 @@ def _update_paginated_params(self, pagination, response):
381388
+ pagination["limit_value"],
382389
pagination["endpoint"].params_map,
383390
)
391+
elif "page_param" in pagination:
392+
set_attribute_from_path(
393+
pagination["kwargs"],
394+
pagination["page_param"],
395+
get_attribute_from_path(pagination["kwargs"], pagination["page_param"], 0) + 1,
396+
pagination["endpoint"].params_map,
397+
)
384398
else:
385399
set_attribute_from_path(
386400
pagination["kwargs"],
@@ -633,9 +647,9 @@ async def call_api_paginated(
633647
host=host,
634648
collection_formats=params["collection_format"],
635649
)
636-
for item in get_attribute_from_path(response, pagination["results_path"]):
650+
for item in get_attribute_from_path(response, pagination.get("results_path")):
637651
yield item
638-
if len(get_attribute_from_path(response, pagination["results_path"])) < pagination["limit_value"]:
652+
if len(get_attribute_from_path(response, pagination.get("results_path"))) < pagination["limit_value"]:
639653
break
640654

641655
params = self._update_paginated_params(pagination, response)

src/datadog_api_client/model_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,8 @@ def __init__(self, **kwargs):
16391639

16401640
def get_attribute_from_path(obj, path, default=None):
16411641
"""Return an attribute at `path` from the passed object."""
1642+
if not path:
1643+
return obj
16421644
for elt in path.split("."):
16431645
try:
16441646
obj = obj[elt]

src/datadog_api_client/v1/api/monitors_api.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
# Copyright 2019-Present Datadog, Inc.
44
from __future__ import annotations
55

6+
import collections
67
from typing import Any, Dict, List, Union
78

89
from datadog_api_client.api_client import ApiClient, Endpoint as _Endpoint
910
from datadog_api_client.configuration import Configuration
1011
from datadog_api_client.model_utils import (
12+
set_attribute_from_path,
13+
get_attribute_from_path,
1114
UnsetType,
1215
unset,
1316
)
@@ -693,6 +696,81 @@ def list_monitors(
693696

694697
return self._list_monitors_endpoint.call_with_http_info(**kwargs)
695698

699+
def list_monitors_with_pagination(
700+
self,
701+
*,
702+
group_states: Union[str, UnsetType] = unset,
703+
name: Union[str, UnsetType] = unset,
704+
tags: Union[str, UnsetType] = unset,
705+
monitor_tags: Union[str, UnsetType] = unset,
706+
with_downtimes: Union[bool, UnsetType] = unset,
707+
id_offset: Union[int, UnsetType] = unset,
708+
page: Union[int, UnsetType] = unset,
709+
page_size: Union[int, UnsetType] = unset,
710+
) -> collections.abc.Iterable[Monitor]:
711+
"""Get all monitor details.
712+
713+
Provide a paginated version of :meth:`list_monitors`, returning all items.
714+
715+
:param group_states: When specified, shows additional information about the group states.
716+
Choose one or more from ``all`` , ``alert`` , ``warn`` , and ``no data``.
717+
:type group_states: str, optional
718+
:param name: A string to filter monitors by name.
719+
:type name: str, optional
720+
:param tags: A comma separated list indicating what tags, if any, should be used to filter the list of monitors by scope.
721+
For example, ``host:host0``.
722+
:type tags: str, optional
723+
:param monitor_tags: A comma separated list indicating what service and/or custom tags, if any, should be used to filter the list of monitors.
724+
Tags created in the Datadog UI automatically have the service key prepended. For example, ``service:my-app``.
725+
:type monitor_tags: str, optional
726+
:param with_downtimes: If this argument is set to true, then the returned data includes all current active downtimes for each monitor.
727+
:type with_downtimes: bool, optional
728+
:param id_offset: Use this parameter for paginating through large sets of monitors. Start with a value of zero, make a request, set the value to the last ID of result set, and then repeat until the response is empty.
729+
:type id_offset: int, optional
730+
:param page: The page to start paginating from. If this argument is not specified, the request returns all monitors without pagination.
731+
:type page: int, optional
732+
:param page_size: The number of monitors to return per page. If the page argument is not specified, the default behavior returns all monitors without a ``page_size`` limit. However, if page is specified and ``page_size`` is not, the argument defaults to 100.
733+
:type page_size: int, optional
734+
735+
:return: A generator of paginated results.
736+
:rtype: collections.abc.Iterable[Monitor]
737+
"""
738+
kwargs: Dict[str, Any] = {}
739+
if group_states is not unset:
740+
kwargs["group_states"] = group_states
741+
742+
if name is not unset:
743+
kwargs["name"] = name
744+
745+
if tags is not unset:
746+
kwargs["tags"] = tags
747+
748+
if monitor_tags is not unset:
749+
kwargs["monitor_tags"] = monitor_tags
750+
751+
if with_downtimes is not unset:
752+
kwargs["with_downtimes"] = with_downtimes
753+
754+
if id_offset is not unset:
755+
kwargs["id_offset"] = id_offset
756+
757+
if page is not unset:
758+
kwargs["page"] = page
759+
760+
if page_size is not unset:
761+
kwargs["page_size"] = page_size
762+
763+
local_page_size = get_attribute_from_path(kwargs, "page_size", 100)
764+
endpoint = self._list_monitors_endpoint
765+
set_attribute_from_path(kwargs, "page_size", local_page_size, endpoint.params_map)
766+
pagination = {
767+
"limit_value": local_page_size,
768+
"page_param": "page",
769+
"endpoint": endpoint,
770+
"kwargs": kwargs,
771+
}
772+
return endpoint.call_with_http_info_paginated(pagination)
773+
696774
def search_monitor_groups(
697775
self,
698776
*,

0 commit comments

Comments
 (0)