smartclickhouse/ts/smartclickhouse.classes.httpclient.ts

94 lines
2.9 KiB
TypeScript
Raw Permalink Normal View History

2022-07-28 14:53:07 +00:00
import * as plugins from './smartclickhouse.plugins.js';
export interface IClickhouseHttpClientOptions {
2022-07-28 15:36:08 +00:00
username?: string;
2022-07-28 14:53:07 +00:00
password?: string;
url: string;
}
export class ClickhouseHttpClient {
// STATIC
public static async createAndStart(optionsArg: IClickhouseHttpClientOptions) {
const clickhouseHttpInstance = new ClickhouseHttpClient(optionsArg);
await clickhouseHttpInstance.start();
return clickhouseHttpInstance;
}
// INSTANCE
public options: IClickhouseHttpClientOptions;
public webrequestInstance = new plugins.webrequest.WebRequest({
logging: false
});
public computedProperties: {
connectionUrl: string;
parsedUrl: plugins.smarturl.Smarturl;
} = {
connectionUrl: null,
parsedUrl: null,
};
constructor(optionsArg: IClickhouseHttpClientOptions) {
this.options = optionsArg;
}
public async start() {
this.computedProperties.parsedUrl = plugins.smarturl.Smarturl.createFromUrl(this.options.url);
2022-07-28 15:36:08 +00:00
console.log(this.computedProperties.parsedUrl);
2022-07-28 14:53:07 +00:00
this.computedProperties.connectionUrl = this.computedProperties.parsedUrl.toString();
}
public async ping() {
2022-07-28 15:36:08 +00:00
const ping = await this.webrequestInstance.request(this.computedProperties.connectionUrl.toString(), {
2022-07-28 14:53:07 +00:00
method: 'GET',
timeoutMs: 1000,
});
return ping.status === 200 ? true : false;
}
public async queryPromise(queryArg: string) {
const returnArray = [];
const response = await this.webrequestInstance.request(`${this.computedProperties.connectionUrl}?query=${encodeURIComponent(queryArg)}`, {
2022-07-28 15:36:08 +00:00
method: 'POST',
headers: this.getHeaders(),
2022-07-28 14:53:07 +00:00
});
// console.log('===================');
2022-07-28 15:36:08 +00:00
// console.log(this.computedProperties.connectionUrl);
2022-07-28 14:53:07 +00:00
// console.log(queryArg);
// console.log((await response.clone().text()).split(/\r?\n/))
if (response.headers.get('X-ClickHouse-Format') === 'JSONEachRow') {
const jsonList = await response.text();
const jsonArray = jsonList.split('\n');
for (const jsonArg of jsonArray) {
if (!jsonArg) {
continue;
}
returnArray.push(JSON.parse(jsonArg));
}
} else {
}
return returnArray
}
public async insertPromise(databaseArg: string, tableArg: string, documents: any[]) {
const queryArg = `INSERT INTO ${databaseArg}.${tableArg} FORMAT JSONEachRow`;
const response = await this.webrequestInstance.request(`${this.computedProperties.connectionUrl}?query=${encodeURIComponent(queryArg)}`, {
method: 'POST',
2022-07-28 15:36:08 +00:00
body: documents.map(docArg => JSON.stringify(docArg)).join('\n'),
headers: this.getHeaders()
2022-07-28 14:53:07 +00:00
});
return response;
}
2022-07-28 15:36:08 +00:00
private getHeaders() {
const headers: {[key: string]: string} = {}
if (this.options.username) {
headers['X-ClickHouse-User'] = this.options.username;
}
if (this.options.password) {
headers['X-ClickHouse-Key'] = this.options.password;
}
return headers;
}
2022-07-28 14:53:07 +00:00
}