@mindconnect/mindconnect-nodejs
Version:
NodeJS Library for Siemens Insights Hub Connectivity - TypeScript SDK for Insights Hub and Industrial IoT - Command Line Interface - Insights Hub Development Proxy (Siemens Insights Hub was formerly known as MindSphere)
205 lines • 8.68 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IotFileClient = void 0;
const fs = require("fs");
const utils_1 = require("../../utils");
const multipart_uploader_1 = require("../common/multipart-uploader");
const sdk_client_1 = require("../common/sdk-client");
/**
*
* The IoT File API enables storing and retrieving files for entity instances.
*
* * The IoT File Client provides a Upload File Method which will do the most important things for you.
* * It is highly recomended to use the UploadFile method instead of the raw api methods.
*
* @export
* @class IotFileClient
* @extends {SdkClient}
*/
class IotFileClient extends sdk_client_1.SdkClient {
constructor() {
super(...arguments);
this._baseUrl = "/api/iotfile/v3";
this.uploader = new multipart_uploader_1.MultipartUploader(undefined, this);
}
/**
* Create or update a file for the specified entity and path, with the provided content.
* * The most complete function is UploadFile. This is provided for completeness.
*
* @param {string} entityId
* @param {string} filepath
* @param {(string | Buffer)} file
* @param {{ part?: number; ifMatch: number; timestamp?: Date; description?: string; type?: string }} [params]
* @returns {Promise<Headers>}
*
* @memberOf IotFileClient
*/
PutFile(entityId, filepath, file, params) {
return __awaiter(this, void 0, void 0, function* () {
const myBuffer = typeof file === "string" ? fs.readFileSync(file) : file;
const parameters = params || {};
return (yield this.HttpAction({
verb: "PUT",
gateway: this.GetGateway(),
authorization: yield this.GetToken(),
baseUrl: `${this._baseUrl}/files/${entityId}/${filepath}`,
body: myBuffer,
additionalHeaders: parameters,
octetStream: true,
noResponse: true,
returnHeaders: true
}));
});
}
/**
*
* Read a file for the specified entity and path
*
* @param {string} entityId Id to instance of entity
* @param {string} filepath path of the file including filename
* @param {(string | Buffer)} file
* @param {{ ifNoneMatch?: number; range?: string }} [params]
* @param {number | undefined} [params.ifNoneMatch] ETag of the latest version (not supported in this release)
* @param {number | undefined} [params.ifNoneMatch] Part of a file to return in Bytes, eg bytes=200-600
* @returns {Promise<Response>}
*
* @memberOf IotFileClient
*/
GetFile(entityId, filepath, params) {
return __awaiter(this, void 0, void 0, function* () {
const parameters = params || {};
return (yield this.HttpAction({
verb: "GET",
gateway: this.GetGateway(),
authorization: yield this.GetToken(),
baseUrl: `${this._baseUrl}/files/${entityId}/${filepath}`,
additionalHeaders: parameters,
rawResponse: true
}));
});
}
/**
* Search files for the specified entity.
*
* @param {string} entityid
* @param {{ offset?: number; limit?: number; count?: number; order?: string; filter?: string }} [params]
* @returns {Promise<IotFileModels.File[]>}
*
* @memberOf IotFileClient
*/
GetFiles(entityid, params) {
return __awaiter(this, void 0, void 0, function* () {
const parameters = params || {};
const { offset, limit, count, order, filter } = parameters;
return (yield this.HttpAction({
verb: "GET",
gateway: this.GetGateway(),
authorization: yield this.GetToken(),
baseUrl: `${this._baseUrl}/files/${entityid}?${(0, utils_1.toQueryString)({ filter, offset, limit, count, order })}`
}));
});
}
/**
* Delete a file for the specified entity and path
*
* @param {string} entityId
* @param {string} filepath
* @returns
*
* @memberOf IotFileClient
*/
DeleteFile(entityId, filepath) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.HttpAction({
verb: "DELETE",
gateway: this.GetGateway(),
authorization: yield this.GetToken(),
baseUrl: `${this._baseUrl}/files/${entityId}/${filepath}`,
noResponse: true
});
});
}
/**
* Delete a file for the specified entity and path
*
* @param {string} entityId
* @param {string} filepath
* @returns
*
* @memberOf IotFileClient
*/
GetMultipartUploads(entityId, filepath) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.HttpAction({
verb: "GET",
gateway: this.GetGateway(),
authorization: yield this.GetToken(),
baseUrl: `${this._baseUrl}/fileslist/${entityId}/${filepath}`
});
});
}
/**
* Upload file
*
* This method is used to upload the files to the MindSphere.
* It supports standard and multipart upload which can be configured with the [params.chunk] parameter.
*
* * The method will try to abort the multipart upload if an exception occurs.
* * Multipart Upload is done in following steps:
* * start multipart upload
* * upload in parallel [params.parallelUploadChunks] the file parts (retrying [params.retry] times if configured)
* * uploading last chunk.
*
* @param {string} entityId
* @param {string} filepath
* @param {(string | Buffer)} file
* @param {fileUploadOptionalParameters} [params] optional parameters
* @param {(number | undefined)}[params.part] multipart/upload part
* @param {(Date | undefined)} [params.timestamp] File timestamp in mindsphere.
* @param {(string | undefined)} [params.description] Description in mindsphere.
* @param {(string | undefined)} [params.type] Mime type in mindsphere.
* @param {(number | undefined)} [params.chunkSize] chunkSize. It must be bigger than 5 MB. Default 8 MB.
* @param {(number | undefined)} [params.retry] Number of retries
* @param {(Function | undefined)} [params.logFunction] log functgion is called every time a retry happens.
* @param {(Function | undefined)} [params.verboseFunction] verboseLog function.
* @param {(boolean | undefined)} [params.chunk] Set to true to enable multipart uploads
* @param {(number | undefined)} [params.parallelUploads] max paralell uploads for parts (default: 3)
* @param {(number | undefined)} [params.ifMatch] The etag for the upload.
* @returns {Promise<string>} md5 hash of the file
*
* @example await sdk.GetIotFileClient().UploadFile (agent.GetClientId(), "some/mindsphere/path/file.txt", "file.txt");
* @example await sdk.GetIotFileClient().UploadFile (agent.GetClientId(), "some/other/path/10MB.bin", "bigFile.bin",{ chunked:true, retry:5 });
*
* @memberOf IotFileClient
*/
UploadFile(entityId, filepath, file, params) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.uploader.UploadFile(entityId, filepath, file, params);
return result;
});
}
/**
* Abort the multipart upload.
*
* @param {string} entityId
* @param {string} filePath
*
* @memberOf IotFileClient
*/
AbortUpload(entityId, filePath) {
return __awaiter(this, void 0, void 0, function* () {
yield this.uploader.AbortUpload(entityId, filePath);
});
}
}
exports.IotFileClient = IotFileClient;
//# sourceMappingURL=iot-file.js.map