camstreamerlib
Version:
Helper library for CamStreamer ACAP applications.
53 lines (52 loc) • 1.81 kB
JavaScript
import { addParametersToPath } from '../internal/utils';
import { HttpRequestSender } from './HttpRequestSender';
export class DefaultClient {
tls;
ip;
port;
user;
pass;
httpRequestSender;
constructor(opt = {}) {
this.tls = opt.tls ?? false;
this.ip = opt.ip ?? '127.0.0.1';
this.port = opt.port ?? (this.tls ? 443 : 80);
this.user = opt.user ?? '';
this.pass = opt.pass ?? '';
let agentOptions;
if (opt.tlsInsecure !== undefined || opt.keepAlive !== undefined) {
agentOptions = {
rejectUnaurhorized: !opt.tlsInsecure,
keepAlive: opt.keepAlive,
};
}
this.httpRequestSender = new HttpRequestSender(agentOptions);
}
get url() {
return `${this.tls ? 'https' : 'http'}://${this.user}:${this.pass}@${this.ip}:${this.port}`;
}
get = (...params) => {
const [path, parameters, headers] = params;
const options = this.getBaseConnectionParams('GET', path, parameters ?? {});
options.headers = headers;
return this.httpRequestSender.sendRequest(options);
};
post = (...params) => {
const [path, data, parameters, headers] = params;
const options = this.getBaseConnectionParams('POST', path, parameters ?? {});
options.headers = headers;
return this.httpRequestSender.sendRequest(options, data);
};
getBaseConnectionParams(method, path, params) {
const pathName = addParametersToPath(path, params);
return {
method: method,
protocol: this.tls ? 'https:' : 'http:',
host: this.ip,
port: this.port,
path: pathName,
user: this.user,
pass: this.pass,
};
}
}