camstreamerlib
Version:
Helper library for CamStreamer ACAP applications.
49 lines (48 loc) • 1.67 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(params) {
const { path, parameters, headers, timeout } = params;
const options = this.getBaseConnectionParams('GET', path, parameters, headers, timeout);
return this.httpRequestSender.sendRequest(options);
}
post(params) {
const { path, data, parameters, headers, timeout } = params;
const options = this.getBaseConnectionParams('POST', path, parameters, headers, timeout);
return this.httpRequestSender.sendRequest(options, data);
}
getBaseConnectionParams(method, path, params, headers, timeout) {
return {
method: method,
protocol: this.tls ? 'https:' : 'http:',
host: this.ip,
port: this.port,
path: addParametersToPath(path, params),
user: this.user,
pass: this.pass,
headers,
timeout,
};
}
}