UNPKG

@adobe/pdftools-extract-node-sdk

Version:

The Document Services PDF Tools Extract Node.js SDK provides APIs for extracting elements and renditions from PDF

120 lines (109 loc) 3.72 kB
/* * Copyright 2019 Adobe * All Rights Reserved. * * NOTICE: Adobe permits you to use, modify, and distribute this file in * accordance with the terms of the Adobe license agreement accompanying * it. If you have received this file from a source other than Adobe, * then your use, modification, or distribution of it requires the prior * written permission of Adobe. */ const fs = require('fs'), InternalClientConfig = require('./internal/internal-client-config'); /** * Encapsulates the API request configurations */ class ClientConfig { /** * @hideconstructor */ constructor() {} static get Builder() { /** * * Builds a {@link ClientConfig} instance. */ class ClientConfigBuilder { /** * @hideconstructor */ constructor() {} /** * Sets the connect timeout. It should be greater than zero. * @param {!Number} connectTimeout - Determines the timeout in milliseconds until a connection is * established in the API calls. Default value is 10000 milliseconds * @returns {ClientConfigBuilder} This Builder instance to add any additional parameters. */ withConnectTimeout(connectTimeout) { this.connectTimeout = connectTimeout; return this; } /** * Sets the read timeout. It should be greater than zero. * @param {!Number} readTimeout - Defines the read timeout in milliseconds, which is the timeout for * waiting for data or, put differently, a maximum period inactivity between two consecutive data packets. * Default value is 20000 milliseconds * @returns {ClientConfigBuilder} This Builder instance to add any additional parameters. */ withReadTimeout(readTimeout) { this.readTimeout = readTimeout; return this; } /** * Sets the connect timeout and read timeout using the JSON client config file path. All the keys in the * JSON structure are optional. * <p> * JSON structure: * <pre> * { * "connectTimeout": "4000", * "readTimeout": "20000" * } * </pre> * @param {!String} clientConfigFilePath - JSON client config file path * @returns {ClientConfigBuilder} This Builder instance to add any additional parameters. */ fromFile(clientConfigFilePath){ const clientConfig = JSON.parse(fs.readFileSync(clientConfigFilePath)); const v2ServicesConfig = clientConfig.v2Services; const cpfServicesConfig = clientConfig.cpfServices; if(cpfServicesConfig){ const cpfServicesOpsCreateUri = cpfServicesConfig.cpfOpsCreateUri; if (cpfServicesOpsCreateUri) { this.cpfOpsCreateUri = cpfServicesOpsCreateUri; } } if (v2ServicesConfig) { const v2ServicesPredictUri = v2ServicesConfig.v2PredictUri; if (v2ServicesPredictUri) { this.v2ServicesPredictUri = v2ServicesPredictUri; } } this.connectTimeout = parseInt(clientConfig.connectTimeout, 10); this.readTimeout = parseInt(clientConfig.readTimeout, 10); return this; } /** * Returns a new {@link ClientConfig} instance built from the current state of this builder. * @returns {ClientConfig} A ClientConfig instance. */ build() { let internalClientConfig = new InternalClientConfig(this.connectTimeout, this.readTimeout, this.v2ServicesPredictUri, this.cpfOpsCreateUri); Object.freeze(internalClientConfig); return internalClientConfig; } } return ClientConfigBuilder; } /** * Creates a new {@link ClientConfig} builder. * @memberOf ClientConfig * @function * @returns {ClientConfigBuilder} A ClientConfigBuilder instance. */ static clientConfigBuilder() { return new ClientConfig.Builder(); } } Object.freeze(ClientConfig); module.exports = ClientConfig;