Skip to content

Commit

Permalink
added failOnNoData flag to Get SDK method
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Twydell <[email protected]>
  • Loading branch information
AndrewTwydell committed Jan 10, 2025
1 parent 9d46311 commit 06e2c06
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/sdk/src/methods/cache/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { IResultCacheParms } from "../../doc/IResultCacheParms";
import { CicsCmciRestClient } from "../../rest";
import { Utils } from "../../utils";

export async function getCache(session: AbstractSession, parms: ICacheParms): Promise<ICMCIApiResponse> {
export async function getCache(session: AbstractSession, parms: ICacheParms, failOnNoData: boolean = true): Promise<ICMCIApiResponse> {
ImperativeExpect.toBeDefinedAndNonBlank(parms.cacheToken, "CICS Result Cache Token", "CICS Result Cache Token is required");
Logger.getAppLogger().debug("Attempting to get cache with the following parameters:\n%s", JSON.stringify(parms));

Expand All @@ -28,5 +28,5 @@ export async function getCache(session: AbstractSession, parms: ICacheParms): Pr
};
const cmciResource = Utils.getCacheUri(parms.cacheToken, options);

return CicsCmciRestClient.getExpectParsedXml(session, cmciResource, []);
return CicsCmciRestClient.getExpectParsedXml(session, cmciResource, [], failOnNoData);
}
4 changes: 2 additions & 2 deletions packages/sdk/src/methods/get/Get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Utils } from "../../utils";
* @throws {ImperativeError} CICS region name not defined or blank
* @throws {ImperativeError} CicsCmciRestClient request fails
*/
export async function getResource(session: AbstractSession, parms: IResourceParms): Promise<ICMCIApiResponse> {
export async function getResource(session: AbstractSession, parms: IResourceParms, failOnNoData: boolean = true): Promise<ICMCIApiResponse> {
ImperativeExpect.toBeDefinedAndNonBlank(parms.name, "CICS Resource name", "CICS resource name is required");

Logger.getAppLogger().debug("Attempting to get resource(s) with the following parameters:\n%s", JSON.stringify(parms));
Expand All @@ -39,5 +39,5 @@ export async function getResource(session: AbstractSession, parms: IResourceParm

const cmciResource = Utils.getResourceUri(parms.name, options);

return CicsCmciRestClient.getExpectParsedXml(session, cmciResource, []);
return CicsCmciRestClient.getExpectParsedXml(session, cmciResource, [], failOnNoData);
}
36 changes: 24 additions & 12 deletions packages/sdk/src/rest/CicsCmciRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ import { CicsCmciMessages } from "../constants/CicsCmci.messages";
export class CicsCmciRestClient extends RestClient {
/**
* If the API request is successful, this value should be in
* api_response2 in the resultsummary object in the response
* api_response1 in the resultsummary object in the response
*/
public static readonly CMCI_SUCCESS_RESPONSE_1 = "1024";

/**
* If the API request returns NODATA, this value should be in
* api_response1 in the resultsummary object in the response
*/
public static readonly CMCI_NODATA_RESPONSE_1 = "1027";

/**
* If the API request is successful, this value should be in
* api_response2 in the resultsummary object in the response
Expand Down Expand Up @@ -56,10 +62,10 @@ export class CicsCmciRestClient extends RestClient {
* @throws {ImperativeError} verifyResponseCodes fails
*/
public static async getExpectParsedXml(session: AbstractSession,
resource: string, reqHeaders: any[] = []): Promise<ICMCIApiResponse> {
resource: string, reqHeaders: any[] = [], failOnNoData: boolean = true): Promise<ICMCIApiResponse> {
const data = await CicsCmciRestClient.getExpectString(session, resource, reqHeaders);
const apiResponse = CicsCmciRestClient.parseStringSync(data);
return CicsCmciRestClient.verifyResponseCodes(apiResponse);
return CicsCmciRestClient.verifyResponseCodes(apiResponse, failOnNoData);
}

/**
Expand Down Expand Up @@ -180,17 +186,23 @@ export class CicsCmciRestClient extends RestClient {
* @returns {ICMCIApiResponse} - the response if it was correct
* @throws {ImperativeError} request did not get the expected codes
*/
private static verifyResponseCodes(apiResponse: ICMCIApiResponse): ICMCIApiResponse {
if (apiResponse.response != null && apiResponse.response.resultsummary != null
&& apiResponse.response.resultsummary.api_response1 === CicsCmciRestClient.CMCI_SUCCESS_RESPONSE_1
&& apiResponse.response.resultsummary.api_response2 === CicsCmciRestClient.CMCI_SUCCESS_RESPONSE_2) {
// expected return code and reason code specify
private static verifyResponseCodes(apiResponse: ICMCIApiResponse, failOnNoData: boolean = true): ICMCIApiResponse {

const okResponse1Codes = [
CicsCmciRestClient.CMCI_SUCCESS_RESPONSE_1
];
if (!failOnNoData) {
okResponse1Codes.push(CicsCmciRestClient.CMCI_NODATA_RESPONSE_1);
}

if (okResponse1Codes.includes(apiResponse.response?.resultsummary?.api_response1)) {
return apiResponse;
} else {
throw new ImperativeError({
msg: CicsCmciMessages.cmciRequestFailed.message + "\n" + TextUtils.prettyJson(apiResponse),
});
}

throw new ImperativeError({
msg: CicsCmciMessages.cmciRequestFailed.message + "\n" + TextUtils.prettyJson(apiResponse),
});

}

/**
Expand Down

0 comments on commit 06e2c06

Please sign in to comment.