Created
November 18, 2020 14:57
-
-
Save shirish87/5bf18923ec48e36beae9a3c27db70b37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
ClientOptions, | |
WritePrecision, | |
WriteOptions, | |
WritePrecisionType, | |
} from '@influxdata/influxdb-client/dist/options'; | |
import TransportImpl from '@influxdata/influxdb-client/dist/impl/node/NodeHttpTransport'; | |
import WriteApiImpl from '@influxdata/influxdb-client/dist/impl/WriteApiImpl'; | |
import QueryApiImpl from '@influxdata/influxdb-client/dist/impl/QueryApiImpl'; | |
import { | |
IllegalArgumentError, | |
QueryApi, | |
SendOptions, | |
Transport, | |
WriteApi, | |
} from '@influxdata/influxdb-client'; | |
import { TransportWrapper } from './transport-wrapper'; | |
/** | |
* InfluxDB 2.0 entry point that configures communication with InfluxDB server | |
* and provide APIs to write and query data. | |
*/ | |
export class InfluxDB { | |
private _options: ClientOptions; | |
readonly transport: Transport; | |
/** | |
* Creates influxdb client options from an options object or url. | |
* @param options - client options | |
*/ | |
constructor(options: ClientOptions | string) { | |
if (typeof options === 'string') { | |
this._options = { url: options }; | |
} else if (options !== null && typeof options === 'object') { | |
this._options = options; | |
} else { | |
throw new IllegalArgumentError('No url or configuration specified!'); | |
} | |
const url = this._options.url; | |
if (typeof url !== 'string') | |
throw new IllegalArgumentError('No url specified!'); | |
if (url.endsWith('/')) this._options.url = url.substring(0, url.length - 1); | |
this.transport = | |
this._options.transport ?? new TransportImpl(this._options); | |
} | |
/** | |
* Creates WriteApi for the supplied organization and bucket. BEWARE that returned instances must be closed | |
* in order to flush the remaining data and close already scheduled retry executions. | |
* | |
* @remarks | |
* Inspect the {@link WriteOptions} to control also advanced options, such retries of failure, retry strategy options, data chunking | |
* and flushing windows. See {@link DEFAULT_WriteOptions} to see the defaults. | |
* | |
* See also {@link https://github.com/influxdata/influxdb-client-js/blob/master/examples/write.js | write.js example}, | |
* {@link https://github.com/influxdata/influxdb-client-js/blob/master/examples/writeAdvanced.js | writeAdvanced.js example}, | |
* and {@link https://github.com/influxdata/influxdb-client-js/blob/master/examples/index.html | browser example}. | |
* | |
* @param org - Specifies the destination organization for writes. Takes either the ID or Name interchangeably. | |
* @param bucket - The destination bucket for writes. | |
* @param token - The authentication token. | |
* @param precision - Timestamp precision for line items. | |
* @param writeOptions - Custom write options. | |
* @param headers - Custom headers. | |
* @returns WriteApi instance | |
*/ | |
newWriteApi( | |
org: string, | |
bucket: string, | |
token?: string, | |
precision: WritePrecisionType = WritePrecision.ns, | |
writeOptions?: Partial<WriteOptions>, | |
headers?: SendOptions['headers'], | |
): WriteApi { | |
return new WriteApiImpl( | |
// eslint-disable-next-line prettier/prettier | |
(token || headers) | |
? new TransportWrapper(this.transport, token, headers) | |
: this.transport, | |
org, | |
bucket, | |
precision, | |
writeOptions ?? this._options.writeOptions, | |
); | |
} | |
/** | |
* Creates QueryApi for the supplied organization . | |
* | |
* @remarks | |
* See also {@link https://github.com/influxdata/influxdb-client-js/blob/master/examples/query.ts | query.ts example}, | |
* {@link https://github.com/influxdata/influxdb-client-js/blob/master/examples/queryWithParams.ts | queryWithParams.ts example}, | |
* {@link https://github.com/influxdata/influxdb-client-js/blob/master/examples/rxjs-query.ts | rxjs-query.ts example}, | |
* and {@link https://github.com/influxdata/influxdb-client-js/blob/master/examples/index.html | browser example}, | |
* | |
* @param org - organization | |
* @param token - The authentication token. | |
* @param headers - Custom headers. | |
* @returns QueryApi instance | |
*/ | |
newQueryApi( | |
org: string, | |
token?: string, | |
headers?: SendOptions['headers'], | |
): QueryApi { | |
return new QueryApiImpl( | |
// eslint-disable-next-line prettier/prettier | |
(token || headers) | |
? new TransportWrapper(this.transport, token, headers) | |
: this.transport, | |
org, | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
ChunkCombiner, | |
CommunicationObserver, | |
SendOptions, | |
Transport, | |
} from '@influxdata/influxdb-client'; | |
export class TransportWrapper implements Transport { | |
/** | |
* Combines response chunks to create a single response object. | |
*/ | |
readonly chunkCombiner: ChunkCombiner; | |
readonly headers?: SendOptions['headers']; | |
/** | |
* Creates a node transport wrapper for the underlying supplied transport. | |
* @param transport - actual transport | |
*/ | |
constructor( | |
private readonly transport: Transport, | |
token: string, | |
headers?: SendOptions['headers'], | |
) { | |
this.chunkCombiner = transport.chunkCombiner; | |
this.headers = headers && { | |
...headers, | |
}; | |
if (token) { | |
this.headers = { | |
...this.headers, | |
authorization: `Token ${token}`, | |
} | |
} | |
} | |
/** | |
* Send data to the server and receive communication events via callbacks. | |
* | |
* @param path - HTTP request path | |
* @param requestBody - HTTP request body | |
* @param options - send options | |
* @param callbacks - communication callbacks to received data in Uint8Array | |
*/ | |
send( | |
path: string, | |
requestBody: string, | |
options: SendOptions, | |
callbacks?: Partial<CommunicationObserver<Uint8Array>>, | |
): void { | |
if (!this.headers) { | |
return this.transport.send(path, requestBody, options, callbacks); | |
} | |
return this.transport.send( | |
path, | |
requestBody, | |
{ | |
...options, | |
...{ | |
headers: { | |
...options.headers, | |
...this.headers, | |
}, | |
}, | |
}, | |
callbacks, | |
); | |
} | |
/** | |
* Sends data to the server and receives decoded result. The type of the result depends on | |
* response's content-type (deserialized json, text). | |
* @param path - HTTP request path | |
* @param requestBody - request body | |
* @param options - send options | |
*/ | |
request(path: string, body: any, options: SendOptions): Promise<any> { | |
return this.transport.request(path, body, options); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment