Skip to content

Commit 2fc205a

Browse files
authored
feat(Usage Reports): Update query param for getting reports snapshot list (#224)
Add new query parameters to support pagination of API: `/v1/billing-reports-snapshots` Signed-off-by: manu.k.m <[email protected]>
1 parent ba3e1a3 commit 2fc205a

File tree

2 files changed

+193
-3
lines changed

2 files changed

+193
-3
lines changed

ibm_platform_services/usage_reports_v4.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,8 @@ def get_reports_snapshot(
826826
*,
827827
date_from: int = None,
828828
date_to: int = None,
829+
limit: int = None,
830+
start: str = None,
829831
**kwargs,
830832
) -> DetailedResponse:
831833
"""
@@ -842,6 +844,10 @@ def get_reports_snapshot(
842844
billing report snapshot is requested.
843845
:param int date_to: (optional) Timestamp in milliseconds for which billing
844846
report snapshot is requested.
847+
:param int limit: (optional) Number of usage records returned. The default
848+
value is 30. Maximum value is 200.
849+
:param str start: (optional) The offset from which the records must be
850+
fetched. Offset information is included in the response.
845851
:param dict headers: A `dict` containing the request headers
846852
:return: A `DetailedResponse` containing the result, headers and HTTP status code.
847853
:rtype: DetailedResponse with `dict` result representing a `SnapshotList` object
@@ -864,6 +870,8 @@ def get_reports_snapshot(
864870
'month': month,
865871
'date_from': date_from,
866872
'date_to': date_to,
873+
'_limit': limit,
874+
'_start': start,
867875
}
868876

869877
if 'headers' in kwargs:
@@ -3151,26 +3159,32 @@ class SnapshotListNext:
31513159
Reference to the next page of the search query if any.
31523160
31533161
:attr str href: (optional)
3162+
:attr str offset: (optional)
31543163
"""
31553164

31563165
def __init__(
31573166
self,
31583167
*,
31593168
href: str = None,
3169+
offset: str = None,
31603170
) -> None:
31613171
"""
31623172
Initialize a SnapshotListNext object.
31633173
31643174
:param str href: (optional)
3175+
:param str offset: (optional)
31653176
"""
31663177
self.href = href
3178+
self.offset = offset
31673179

31683180
@classmethod
31693181
def from_dict(cls, _dict: Dict) -> 'SnapshotListNext':
31703182
"""Initialize a SnapshotListNext object from a json dictionary."""
31713183
args = {}
31723184
if 'href' in _dict:
31733185
args['href'] = _dict.get('href')
3186+
if 'offset' in _dict:
3187+
args['offset'] = _dict.get('offset')
31743188
return cls(**args)
31753189

31763190
@classmethod
@@ -3183,6 +3197,8 @@ def to_dict(self) -> Dict:
31833197
_dict = {}
31843198
if hasattr(self, 'href') and self.href is not None:
31853199
_dict['href'] = self.href
3200+
if hasattr(self, 'offset') and self.offset is not None:
3201+
_dict['offset'] = self.offset
31863202
return _dict
31873203

31883204
def _to_dict(self):
@@ -4696,3 +4712,88 @@ def get_all(self) -> List[dict]:
46964712
next_page = self.get_next()
46974713
results.extend(next_page)
46984714
return results
4715+
4716+
4717+
class GetReportsSnapshotPager:
4718+
"""
4719+
GetReportsSnapshotPager can be used to simplify the use of the "get_reports_snapshot" method.
4720+
"""
4721+
4722+
def __init__(
4723+
self,
4724+
*,
4725+
client: UsageReportsV4,
4726+
account_id: str,
4727+
month: str,
4728+
date_from: int = None,
4729+
date_to: int = None,
4730+
limit: int = None,
4731+
) -> None:
4732+
"""
4733+
Initialize a GetReportsSnapshotPager object.
4734+
:param str account_id: Account ID for which the billing report snapshot is
4735+
requested.
4736+
:param str month: The month for which billing report snapshot is requested.
4737+
Format is yyyy-mm.
4738+
:param int date_from: (optional) Timestamp in milliseconds for which
4739+
billing report snapshot is requested.
4740+
:param int date_to: (optional) Timestamp in milliseconds for which billing
4741+
report snapshot is requested.
4742+
:param int limit: (optional) Number of usage records returned. The default
4743+
value is 30. Maximum value is 200.
4744+
"""
4745+
self._has_next = True
4746+
self._client = client
4747+
self._page_context = {'next': None}
4748+
self._account_id = account_id
4749+
self._month = month
4750+
self._date_from = date_from
4751+
self._date_to = date_to
4752+
self._limit = limit
4753+
4754+
def has_next(self) -> bool:
4755+
"""
4756+
Returns true if there are potentially more results to be retrieved.
4757+
"""
4758+
return self._has_next
4759+
4760+
def get_next(self) -> List[dict]:
4761+
"""
4762+
Returns the next page of results.
4763+
:return: A List[dict], where each element is a dict that represents an instance of SnapshotListSnapshotsItem.
4764+
:rtype: List[dict]
4765+
"""
4766+
if not self.has_next():
4767+
raise StopIteration(message='No more results available')
4768+
4769+
result = self._client.get_reports_snapshot(
4770+
account_id=self._account_id,
4771+
month=self._month,
4772+
date_from=self._date_from,
4773+
date_to=self._date_to,
4774+
limit=self._limit,
4775+
start=self._page_context.get('next'),
4776+
).get_result()
4777+
4778+
next = None
4779+
next_page_link = result.get('next')
4780+
if next_page_link is not None:
4781+
next = get_query_param(next_page_link.get('href'), '_start')
4782+
self._page_context['next'] = next
4783+
if next is None:
4784+
self._has_next = False
4785+
4786+
return result.get('snapshots')
4787+
4788+
def get_all(self) -> List[dict]:
4789+
"""
4790+
Returns all results by invoking get_next() repeatedly
4791+
until all pages of results have been retrieved.
4792+
:return: A List[dict], where each element is a dict that represents an instance of SnapshotListSnapshotsItem.
4793+
:rtype: List[dict]
4794+
"""
4795+
results = []
4796+
while self.has_next():
4797+
next_page = self.get_next()
4798+
results.extend(next_page)
4799+
return results

0 commit comments

Comments
 (0)