UNPKG

watson-developer-cloud

Version:

Client library to use the IBM Watson Services and AlchemyAPI

143 lines (142 loc) 4.99 kB
/** * Copyright 2014 IBM Corp. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import request = require('request'); export interface HeaderOptions { 'X-Watson-Learning-Opt-Out'?: boolean; [key: string]: any; } export interface UserOptions { url?: string; version?: string; username?: string; password?: string; api_key?: string; apikey?: string; use_unauthenticated?: boolean; headers?: HeaderOptions; token?: string; iam_access_token?: string; iam_apikey?: string; iam_url?: string; disable_ssl_verification?: boolean; } export interface BaseServiceOptions extends UserOptions { headers: HeaderOptions; url: string; jar?: request.CookieJar; qs: any; rejectUnauthorized?: boolean; } export interface Credentials { username?: string; password?: string; api_key?: string; url?: string; iam_access_token?: string; iam_apikey?: string; iam_url?: string; } export declare class BaseService { static URL: string; name: string; serviceVersion: string; protected _options: BaseServiceOptions; protected serviceDefaults: object; protected tokenManager: any; /** * Internal base class that other services inherit from * @param {UserOptions} options * @param {string} [options.username] - required unless use_unauthenticated is set * @param {string} [options.password] - required unless use_unauthenticated is set * @param {boolean} [options.use_unauthenticated] - skip credential requirement * @param {HeaderOptions} [options.headers] * @param {boolean} [options.headers.X-Watson-Learning-Opt-Out=false] - opt-out of data collection * @param {string} [options.url] - override default service base url * @private * @abstract * @constructor * @throws {Error} * @returns {BaseService} */ constructor(userOptions: UserOptions); /** * Retrieve this service's credentials - useful for passing to the authorization service * * Only returns a URL when token auth is used. * * @returns {Credentials} */ getCredentials(): Credentials; /** * Set an IAM access token to use when authenticating with the service. * The access token should be valid and not yet expired. * * By using this method, you accept responsibility for managing the * access token yourself. You must set a new access token before this * one expires. Failing to do so will result in authentication errors * after this token expires. * * @param {string} iam_access_token - A valid, non-expired IAM access token * @returns {void} */ setAccessToken(iam_access_token: string): void; /** * Guarantee that the next request you make will be IAM authenticated. This * performs any requests necessary to get a valid IAM token so that if your * next request involves a streaming operation, it will not be interrupted. * * @param {Function} callback - callback function to return flow of execution * * @returns {void} */ protected preAuthenticate(callback: any): void; /** * Wrapper around `sendRequest` that determines whether or not IAM tokens * are being used to authenticate the request. If so, the token is * retrieved by the token manager. * * @param {Object} parameters - service request options passed in by user * @param {Function} callback - callback function to pass the response back to * @returns {ReadableStream|undefined} */ protected createRequest(parameters: any, callback: any): any; /** * @private * @param {UserOptions} options * @returns {BaseServiceOptions} */ private initCredentials; /** * Pulls credentials from env properties * * Property checked is uppercase service.name suffixed by _USERNAME and _PASSWORD * * For example, if service.name is speech_to_text, * env properties are SPEECH_TO_TEXT_USERNAME and SPEECH_TO_TEXT_PASSWORD * * @private * @param {string} name - the service snake case name * @returns {Credentials} */ private getCredentialsFromEnvironment; /** * Pulls credentials from VCAP_SERVICES env property that bluemix sets * @param {string} vcap_services_name * @private * @returns {Credentials} */ private getCredentialsFromBluemix; }