-
Notifications
You must be signed in to change notification settings - Fork 85
/
Get.ts
92 lines (79 loc) · 3.87 KB
/
Get.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import { posix } from "path";
import { AbstractSession, ImperativeExpect } from "@zowe/imperative";
import { ZosFilesMessages } from "../../constants/ZosFiles.messages";
import { ZosFilesConstants } from "../../constants/ZosFiles.constants";
import { ZosmfRestClient, ZosmfHeaders, IHeaderContent } from "@zowe/core-for-zowe-sdk";
import { IGetOptions } from "./doc/IGetOptions";
import { ZosFilesUtils } from "../../utils/ZosFilesUtils";
/**
* This class holds helper functions that are used to get the content of data sets or USS files through the z/OSMF APIs
* @export
* @class Get
*/
export class Get {
/**
* Retrieve data sets content
*
* @param {AbstractSession} session - z/OSMF connection info
* @param {string} dataSetName - contains the data set name
* @param {IViewOptions} [options={}] - contains the options to be sent
*
* @returns {Promise<Buffer>} Promise that resolves to the content of the data set
*
* @throws {ImperativeError}
*/
public static async dataSet(session: AbstractSession, dataSetName: string, options: IGetOptions = {}): Promise<Buffer> {
ImperativeExpect.toNotBeNullOrUndefined(dataSetName, ZosFilesMessages.missingDatasetName.message);
ImperativeExpect.toNotBeEqual(dataSetName, "", ZosFilesMessages.missingDatasetName.message);
let endpoint = posix.join(ZosFilesConstants.RESOURCE, ZosFilesConstants.RES_DS_FILES, dataSetName);
const reqHeaders: IHeaderContent[] = ZosFilesUtils.generateHeadersBasedOnOptions(options);
if (options.range) {
reqHeaders.push({"X-IBM-Record-Range": options.range});
}
if (options.volume) {
endpoint = posix.join(ZosFilesConstants.RESOURCE, ZosFilesConstants.RES_DS_FILES, `-(${options.volume})`, dataSetName);
}
const content = await ZosmfRestClient.getExpectBuffer(session, endpoint, reqHeaders);
return content;
}
/**
* Retrieve USS file content
*
* @param {AbstractSession} session - z/OSMF connection info
* @param {string} USSFileName - contains the data set name
* @param {IViewOptions} [options={}] - contains the options to be sent
*
* @returns {Promise<Buffer>} Promise that resolves to the content of the uss file
*
* @throws {ImperativeError}
*/
public static async USSFile(session: AbstractSession, USSFileName: string, options: IGetOptions = {}): Promise<Buffer> {
ImperativeExpect.toNotBeNullOrUndefined(USSFileName, ZosFilesMessages.missingUSSFileName.message);
ImperativeExpect.toNotBeEqual(USSFileName, "", ZosFilesMessages.missingUSSFileName.message);
ImperativeExpect.toNotBeEqual(options.record, true, ZosFilesMessages.unsupportedDataType.message);
USSFileName = posix.normalize(USSFileName);
// Get a proper destination for the file to be downloaded
// If the "file" is not provided, we create a folder structure similar to the uss file structure
if (USSFileName.substr(0, 1) === "/") {
USSFileName = USSFileName.substr(1);
}
const encodedFileName = encodeURIComponent(USSFileName);
const endpoint = posix.join(ZosFilesConstants.RESOURCE, ZosFilesConstants.RES_USS_FILES, encodedFileName);
const reqHeaders: IHeaderContent[] = [ZosmfHeaders.ACCEPT_ENCODING];
if (options.binary === true) {
reqHeaders.unshift(ZosmfHeaders.X_IBM_BINARY);
}
const content = await ZosmfRestClient.getExpectBuffer(session, endpoint, reqHeaders);
return content;
}
}