@signiant/media-shuttle-sdk
Version:
The SDK for supporting file transfer to and from Media Shuttle
294 lines (293 loc) • 16.7 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import { signiant_private } from '@signiant/media-shuttle-sdk-base';
import RequestHeaders from './RequestHeaders';
import { isNil, negate as not, pickBy } from 'lodash';
import SDK_VERSION from '../constants/version';
import { v4 as uuid } from 'uuid';
var DOWNLOAD = 'DOWNLOAD';
var PLATFORM = 'PLATFORM';
var SIGNIANT_MOBILE = 'MOBILE';
var MEDIA_SHUTTLE_MEMBER = 'MEDIA_SHUTTLE_MEMBER';
/**
* All messages to the platform API service should be filtered through the PlatformService.
*/
var PlatformService = /** @class */ (function () {
function PlatformService(_a) {
var baseUrl = _a.baseUrl, credentials = _a.credentials;
this._baseUrl = baseUrl;
this._credentials = credentials;
}
/**
* This function creates a transfer in Media Shuttle
* @param {GetTransferConfigOptions} - **files** - An array of file objects. Each file object must
* have a **path** property and a **size** property.
* @returns The response from the API call.
*/
PlatformService.prototype.createTransfer = function (_a) {
var files = _a.files, portalId = _a.portalId, _b = _a.transferDirection, transferDirection = _b === void 0 ? DOWNLOAD : _b, accountId = _a.accountId, destinationPath = _a.destinationPath, serviceId = _a.serviceId, appVersion = _a.appVersion, clientId = _a.clientId;
return __awaiter(this, void 0, void 0, function () {
var body, headers, options;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
body = __assign({ files: files, portalId: portalId, transferDirection: transferDirection }, (destinationPath && { destinationPath: destinationPath }));
return [4 /*yield*/, this.generateHeaders({
accountId: accountId,
serviceId: serviceId,
appVersion: appVersion,
clientId: clientId,
})];
case 1:
headers = _c.sent();
options = {
url: "".concat(this._baseUrl, "/v1/mediaShuttle/transfers"),
body: body,
headers: headers,
};
return [2 /*return*/, signiant_private.request.post(options)];
}
});
});
};
/**
* Fetches the profile information for a given user
* @returns {Promise<ProfileResponse>} List of accounts and service basic information
*/
PlatformService.prototype.getProfile = function () {
return __awaiter(this, void 0, void 0, function () {
var token, options;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._credentials.getAuthToken()];
case 1:
token = _a.sent();
options = {
url: "".concat(this._baseUrl, "/v1/profile"),
headers: this.getBasicHeaders(token),
};
return [2 /*return*/, signiant_private.request.get(options)];
}
});
});
};
/**
* Fetch a list of portals.
*
* @param {GetPortalsRequestOptions} options - GetPortalsRequestOptions
* @param {string} options.cursor - Opaque string used to fetch the next page of results
* @returns {Promise<ListResponse<Portal>>} A promise that resolves to a ListResponse<Portal> object.
*/
PlatformService.prototype.getPortals = function (_a) {
var serviceId = _a.serviceId, accountId = _a.accountId, cursor = _a.cursor, pageSize = _a.pageSize, limitPortalsToMemberRole = _a.limitPortalsToMemberRole;
return __awaiter(this, void 0, void 0, function () {
var url, query, options;
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
url = new URL("".concat(this._baseUrl, "/v1/mediaShuttle/portals"));
query = new URLSearchParams(__assign(__assign(__assign({}, (cursor && { cursor: cursor })), (pageSize && { pageSize: pageSize.toString() })), (limitPortalsToMemberRole && { role: MEDIA_SHUTTLE_MEMBER })));
url.search = query.toString();
_b = {
url: url.toString()
};
return [4 /*yield*/, this.generateHeaders({ serviceId: serviceId, accountId: accountId })];
case 1:
options = (_b.headers = _c.sent(),
_b);
return [2 /*return*/, signiant_private.request.get(options)];
}
});
});
};
/**
* @ignore
* It updates a transfer.
* @param {UpdateTransferOptions} UpdateTransferOptions the required parameters to update a transfer
* @param {string} UpdateTransferOptions.serviceId - **serviceId**: The ID of the Media Shuttle service.
* @param {UpdateTransferOptions} UpdateTransferOptions.accountId - **accountId**: The ID of the Account Containing the MS Service.
* @param {UpdateTransferOptions} UpdateTransferOptions.transferId - **transferId**: The ID of transfer.
* @param {UpdateTransferOptions} UpdateTransferOptions.body - **body**: Update transfer REST request body.
* @returns The update transfer response.
*/
PlatformService.prototype.updateTransfer = function (_a) {
var serviceId = _a.serviceId, body = _a.body, transferId = _a.transferId, accountId = _a.accountId;
return __awaiter(this, void 0, void 0, function () {
var requestId, headers, options, results;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
requestId = uuid();
/**
* We have special debug logging here as our tech stack can be
* very sensitive to the order in which updateTransfer requests are called.
*/
signiant_private.LogManager.debug("Initiating updateTransfer for request [".concat(requestId, "]: ").concat(JSON.stringify(body)));
return [4 /*yield*/, this.generateHeaders({ accountId: accountId, serviceId: serviceId })];
case 1:
headers = _b.sent();
options = {
url: "".concat(this._baseUrl, "/v1/mediaShuttle/transfers/").concat(transferId),
body: body,
headers: headers,
};
return [4 /*yield*/, signiant_private.request.patch(options)];
case 2:
results = _b.sent();
signiant_private.LogManager.debug("UpdateTransfer request [".concat(requestId, "] complete."));
return [2 /*return*/, results];
}
});
});
};
/**
* @ignore
* It gets the contents of a portal folder.
* @param {GetFolderContentRequest} - Request Object | Includes all the required parameters in order to perform te getFolderContent request
* @param {GetFolderContentRequest} GetFolderContentRequest.accountId - **accountId**: The ID of the Account Containing the MS Service.
* @param {GetFolderContentRequest} GetFolderContentRequest.portalId - **portalId**: The ID of portal.
* @param {GetFolderContentRequest} GetFolderContentRequest.serviceId - **serviceId**: The ID of the MS Service.
* @param {GetFolderContentRequest} GetFolderContentRequest.folderId - **folderId**: Folder Identifier.
* @param {GetFolderContentRequest} GetFolderContentRequest.browsePath - **browsePath**: path of the subfolder to look for relative to the provided folderId
* @returns A list of files/folders in the folder.
*/
PlatformService.prototype.getFolderContent = function (_a) {
var accountId = _a.accountId, portalId = _a.portalId, serviceId = _a.serviceId, folderId = _a.folderId, browsePath = _a.browsePath;
return __awaiter(this, void 0, void 0, function () {
var headers, queryParams, options;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.generateHeaders({ accountId: accountId, serviceId: serviceId })];
case 1:
headers = _b.sent();
queryParams = new URLSearchParams(__assign(__assign({}, (folderId && { folderId: folderId })), (browsePath && { browsePath: browsePath })));
options = {
url: "".concat(this._baseUrl, "/v1/mediaShuttle/portals/").concat(portalId, "/files?").concat(queryParams.toString()),
headers: headers,
};
return [2 /*return*/, signiant_private.request.get(options)];
}
});
});
};
/**
* @ignore
* It gets the permissions for a member of a portal.
* @param {PortalBasicRequest} requestObject - Request Object | Includes all the required parameters in order to perform te getPortalMemberPermissions request
* @param {string} requestObject.accountId - **accountId**: The ID of the Account Containing the MS Service.
* @param {string} requestObject.serviceId - **serviceId**: The ID of the MS Service.
* @param {string} requestObject.portalId - **portalId**: The ID of portal.
* @returns The permissions for the portal member.
*/
PlatformService.prototype.getPortalMemberPermissions = function (_a) {
var accountId = _a.accountId, serviceId = _a.serviceId, portalId = _a.portalId;
return __awaiter(this, void 0, void 0, function () {
var headers, options;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.generateHeaders({ accountId: accountId, serviceId: serviceId })];
case 1:
headers = _b.sent();
options = {
url: "".concat(this._baseUrl, "/v1/mediaShuttle/portals/").concat(portalId, "/memberPermissions"),
headers: headers,
};
return [2 /*return*/, signiant_private.request.get(options)];
}
});
});
};
/**
* It takes an object with two properties, `accountId` and `serviceId`, and returns a promise that
* resolves to an object with four properties, `Authorization`, `Date`, `Signiant-Account-Id`, and
* `Signiant-Service-Id`, which are the required headers for most of the Platform requests
* @param {Object} headerOptions
* @param {string} headerOptions.accountId - `accountId` - The account ID of the account you want to use.
* @param {string} headerOptions.serviceId - `serviceId` - The Media Shuttle service ID.
* @param {string} headerOptions.appVersion - `appVersion` - Signiant application version
* @param {string} headerOptions.clientId - `clientId` - Signiant App Machine identifier
* @param {string} headerOptions.apiClientType - `apiClientType` - Indicates the initiator of the API Request
* @returns {Promise<PlatformRequestHeaders} a PlatformRequestHeaders object.
*/
PlatformService.prototype.generateHeaders = function (_a) {
var _b;
var accountId = _a.accountId, serviceId = _a.serviceId, appVersion = _a.appVersion, clientId = _a.clientId, _c = _a.apiClientType, apiClientType = _c === void 0 ? typeof process !== 'undefined' &&
[SIGNIANT_MOBILE, PLATFORM].includes((_b = process === null || process === void 0 ? void 0 : process.env) === null || _b === void 0 ? void 0 : _b.SDK_CONTEXT)
? process.env.SDK_CONTEXT
: 'MS-SDK' : _c;
return __awaiter(this, void 0, void 0, function () {
var token;
var _d;
return __generator(this, function (_e) {
switch (_e.label) {
case 0: return [4 /*yield*/, this._credentials.getAuthToken()];
case 1:
token = _e.sent();
return [2 /*return*/, pickBy(__assign(__assign({}, this.getBasicHeaders(token)), (_d = {}, _d[RequestHeaders.SIGNIANT_ACCOUNT_ID_HEADER] = accountId, _d[RequestHeaders.SIGNIANT_SERVICE_ID_HEADER] = serviceId, _d[RequestHeaders.SIGNIANT_APPVERSION_HEADER] = appVersion, _d[RequestHeaders.SIGNIANT_CLIENTID_HEADER] = clientId, _d[RequestHeaders.SIGNIANT_API_CLIENT] = apiClientType, _d)), not(isNil))];
}
});
});
};
/**
* Returns the basic required headers to make a Platform request
* @param {string} token auth token used for the Authorization header
* @returns {object} basic headers
*/
PlatformService.prototype.getBasicHeaders = function (token) {
var _a;
return _a = {
Authorization: "Bearer ".concat(token)
},
_a['Content-Type'] = 'application/json',
_a[RequestHeaders.SIGNIANT_MEDIA_SHUTTLE_SDK_VERSION_HEADER] = "".concat(SDK_VERSION),
_a;
};
return PlatformService;
}());
export default PlatformService;