Skip to content

Commit 87c2d75

Browse files
committed
feat: add support for application list pagination
Application list results are now paginated. Add properties and helper classes for accessing application list pages. Signed-off-by: Subin Shekhar <[email protected]>
1 parent f62e585 commit 87c2d75

File tree

6 files changed

+239
-20
lines changed

6 files changed

+239
-20
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,4 @@ secrets.tar
8383

8484
# SDK generator
8585
.openapi-generator*
86+
.npmrc

examples/ibm-analytics-engine-api.v3.test.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
/* eslint-disable no-console */
21+
/* eslint-disable no-await-in-loop */
2122

2223
const IbmAnalyticsEngineApiV3 = require('../dist/ibm-analytics-engine-api/v3');
2324
// eslint-disable-next-line node/no-unpublished-require
@@ -389,12 +390,20 @@ describe('IbmAnalyticsEngineApiV3', () => {
389390
const params = {
390391
instanceId: 'e64c907a-e82f-46fd-addc-ccfafbd28b09',
391392
state: ['accepted', 'running', 'finished', 'failed'],
393+
limit: 10,
392394
};
393395

394-
let res;
396+
const allResults = [];
395397
try {
396-
res = await ibmAnalyticsEngineApiService.listApplications(params);
397-
console.log(JSON.stringify(res.result, null, 2));
398+
const pager = new IbmAnalyticsEngineApiV3.ApplicationsPager(
399+
ibmAnalyticsEngineApiService,
400+
params
401+
);
402+
while (pager.hasNext()) {
403+
const nextPage = await pager.getNext();
404+
allResults.push(...nextPage);
405+
}
406+
console.log(JSON.stringify(allResults, null, 2));
398407
} catch (err) {
399408
console.warn(err);
400409
}

ibm-analytics-engine-api/v3.ts

+116-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
* IBM OpenAPI SDK Code Generator Version: 3.66.0-d6c2d7e0-20230215-221247
1919
*/
2020

21+
/* eslint-disable max-classes-per-file */
22+
/* eslint-disable no-await-in-loop */
23+
2124
import * as extend from 'extend';
2225
import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
2326
import {
@@ -713,6 +716,8 @@ class IbmAnalyticsEngineApiV3 extends BaseService {
713716
* @param {string} params.instanceId - The identifier of the Analytics Engine instance associated with the Spark
714717
* application(s).
715718
* @param {string[]} [params.state] - List of Spark application states that will be used to filter the response.
719+
* @param {number} [params.limit] - Number of application entries to be included in the response.
720+
* @param {string} [params.start] - Token used to fetch the next or the previous page of the applications list.
716721
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
717722
* @returns {Promise<IbmAnalyticsEngineApiV3.Response<IbmAnalyticsEngineApiV3.ApplicationCollection>>}
718723
*/
@@ -721,14 +726,16 @@ class IbmAnalyticsEngineApiV3 extends BaseService {
721726
): Promise<IbmAnalyticsEngineApiV3.Response<IbmAnalyticsEngineApiV3.ApplicationCollection>> {
722727
const _params = { ...params };
723728
const _requiredParams = ['instanceId'];
724-
const _validParams = ['instanceId', 'state', 'headers'];
729+
const _validParams = ['instanceId', 'state', 'limit', 'start', 'headers'];
725730
const _validationErrors = validateParams(_params, _requiredParams, _validParams);
726731
if (_validationErrors) {
727732
return Promise.reject(_validationErrors);
728733
}
729734

730735
const query = {
731736
'state': _params.state,
737+
'limit': _params.limit,
738+
'start': _params.start,
732739
};
733740

734741
const path = {
@@ -1560,6 +1567,10 @@ namespace IbmAnalyticsEngineApiV3 {
15601567
instanceId: string;
15611568
/** List of Spark application states that will be used to filter the response. */
15621569
state?: ListApplicationsConstants.State[] | string[];
1570+
/** Number of application entries to be included in the response. */
1571+
limit?: number;
1572+
/** Token used to fetch the next or the previous page of the applications list. */
1573+
start?: string;
15631574
headers?: OutgoingHttpHeaders;
15641575
}
15651576

@@ -1709,10 +1720,18 @@ namespace IbmAnalyticsEngineApiV3 {
17091720
auto_termination_time?: string;
17101721
}
17111722

1712-
/** An array of application details. */
1723+
/** A paginated collection of applications. */
17131724
export interface ApplicationCollection {
17141725
/** List of applications. */
1715-
applications?: Application[];
1726+
applications: Application[];
1727+
/** A reference to a page in a paginated collection. */
1728+
first?: PageLink;
1729+
/** A reference to a page in a paginated collection. */
1730+
next?: PageLink;
1731+
/** A reference to a page in a paginated collection. */
1732+
previous?: PageLink;
1733+
/** The maximum number of results in this page of the collection. */
1734+
limit: number;
17161735
}
17171736

17181737
/** Application details. */
@@ -1963,6 +1982,14 @@ namespace IbmAnalyticsEngineApiV3 {
19631982
type?: string;
19641983
}
19651984

1985+
/** A reference to a page in a paginated collection. */
1986+
export interface PageLink {
1987+
/** A url which returns a specific page of a collection. */
1988+
href: string;
1989+
/** A token which loads a specific page of a collection when it is provided the url of the collection. */
1990+
start?: string;
1991+
}
1992+
19661993
/** Resource consumption limits for the instance. */
19671994
export interface ResourceConsumptionLimitsResponse {
19681995
/** Maximum number of virtual processor cores that be used in the instance. */
@@ -1992,6 +2019,92 @@ namespace IbmAnalyticsEngineApiV3 {
19922019
/** Time when the Spark history server will be stopped automatically. */
19932020
auto_termination_time?: string;
19942021
}
2022+
2023+
/*************************
2024+
* pager classes
2025+
************************/
2026+
2027+
/**
2028+
* ApplicationsPager can be used to simplify the use of listApplications().
2029+
*/
2030+
export class ApplicationsPager {
2031+
protected _hasNext: boolean;
2032+
2033+
protected pageContext: any;
2034+
2035+
protected client: IbmAnalyticsEngineApiV3;
2036+
2037+
protected params: IbmAnalyticsEngineApiV3.ListApplicationsParams;
2038+
2039+
/**
2040+
* Construct a ApplicationsPager object.
2041+
*
2042+
* @param {IbmAnalyticsEngineApiV3} client - The service client instance used to invoke listApplications()
2043+
* @param {Object} params - The parameters to be passed to listApplications()
2044+
* @constructor
2045+
* @returns {ApplicationsPager}
2046+
*/
2047+
constructor(
2048+
client: IbmAnalyticsEngineApiV3,
2049+
params: IbmAnalyticsEngineApiV3.ListApplicationsParams
2050+
) {
2051+
if (params && params.start) {
2052+
throw new Error(`the params.start field should not be set`);
2053+
}
2054+
2055+
this._hasNext = true;
2056+
this.pageContext = { next: undefined };
2057+
this.client = client;
2058+
this.params = JSON.parse(JSON.stringify(params || {}));
2059+
}
2060+
2061+
/**
2062+
* Returns true if there are potentially more results to be retrieved by invoking getNext().
2063+
* @returns {boolean}
2064+
*/
2065+
public hasNext(): boolean {
2066+
return this._hasNext;
2067+
}
2068+
2069+
/**
2070+
* Returns the next page of results by invoking listApplications().
2071+
* @returns {Promise<IbmAnalyticsEngineApiV3.Application[]>}
2072+
*/
2073+
public async getNext(): Promise<IbmAnalyticsEngineApiV3.Application[]> {
2074+
if (!this.hasNext()) {
2075+
throw new Error('No more results available');
2076+
}
2077+
2078+
if (this.pageContext.next) {
2079+
this.params.start = this.pageContext.next;
2080+
}
2081+
const response = await this.client.listApplications(this.params);
2082+
const { result } = response;
2083+
2084+
let next = null;
2085+
if (result && result.next) {
2086+
next = result.next.start;
2087+
}
2088+
this.pageContext.next = next;
2089+
if (!this.pageContext.next) {
2090+
this._hasNext = false;
2091+
}
2092+
return result.applications;
2093+
}
2094+
2095+
/**
2096+
* Returns all results by invoking listApplications() repeatedly until all pages of results have been retrieved.
2097+
* @returns {Promise<IbmAnalyticsEngineApiV3.Application[]>}
2098+
*/
2099+
public async getAll(): Promise<IbmAnalyticsEngineApiV3.Application[]> {
2100+
const results: Application[] = [];
2101+
while (this.hasNext()) {
2102+
const nextPage = await this.getNext();
2103+
results.push(...nextPage);
2104+
}
2105+
return results;
2106+
}
2107+
}
19952108
}
19962109

19972110
export = IbmAnalyticsEngineApiV3;

package-lock.json

+12-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/integration/ibm-analytics-engine-api.v3.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
/* eslint-disable no-console */
18+
/* eslint-disable no-await-in-loop */
1819

1920
const { readExternalSources } = require('ibm-cloud-sdk-core');
2021
const IbmAnalyticsEngineApiV3 = require('../../dist/ibm-analytics-engine-api/v3');
@@ -217,14 +218,38 @@ describe('IbmAnalyticsEngineApiV3_integration', () => {
217218
const params = {
218219
instanceId: instanceGuid,
219220
state: ['accepted', 'running', 'finished', 'failed'],
221+
limit: 10,
220222
};
221223

222224
const res = await ibmAnalyticsEngineApiService.listApplications(params);
223225
expect(res).toBeDefined();
224226
expect(res.status).toBe(200);
225227
expect(res.result).toBeDefined();
226228
});
229+
test('listApplications() via ApplicationsPager', async () => {
230+
const params = {
231+
instanceId: instanceGuid,
232+
state: ['accepted', 'running', 'finished', 'failed'],
233+
limit: 1,
234+
};
227235

236+
const allResults = [];
237+
238+
// Test getNext().
239+
let pager = new IbmAnalyticsEngineApiV3.ApplicationsPager(ibmAnalyticsEngineApiService, params);
240+
while (pager.hasNext()) {
241+
const nextPage = await pager.getNext();
242+
expect(nextPage).not.toBeNull();
243+
allResults.push(...nextPage);
244+
}
245+
246+
// Test getAll().
247+
pager = new IbmAnalyticsEngineApiV3.ApplicationsPager(ibmAnalyticsEngineApiService, params);
248+
const allItems = await pager.getAll();
249+
expect(allItems).not.toBeNull();
250+
expect(allItems).toHaveLength(allResults.length);
251+
console.log(`Retrieved a total of ${allResults.length} items(s) with pagination.`);
252+
});
228253
test('getApplication()', async () => {
229254
const params = {
230255
instanceId: instanceGuid,

0 commit comments

Comments
 (0)