const axios = require('axios');
const crypto = require('crypto');
class UnicyFalconApiClient {
constructor(apiKey, hmacSecret, baseUrl, timeout = 30000) {
this.apiKey = apiKey;
this.hmacSecret = hmacSecret;
this.baseUrl = baseUrl.replace(/\/$/, '');
this.timeout = timeout;
}
sign(method, uri, body, timestamp) {
const payload = `${method.toUpperCase()}|${uri}|${body}|${timestamp}`;
return crypto.createHmac('sha256', this.hmacSecret).update(payload).digest('base64');
}
async request(method, endpoint, data = null, params = null) {
const url = `${this.baseUrl}/${endpoint.replace(/^\//, '')}`;
const timestamp = Math.floor(Date.now() / 1000);
const body = data ? JSON.stringify(data) : '';
const basePath = new URL(this.baseUrl).pathname || '';
const uri = `${basePath}/${endpoint.replace(/^\//, '')}`;
const signature = this.sign(method, uri, body, timestamp);
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-API-Key': this.apiKey,
'X-Timestamp': timestamp.toString(),
'X-Signature': signature
};
const response = await axios({
method: method.toUpperCase(),
url,
headers,
data: data || undefined,
params: params || undefined,
timeout: this.timeout
});
return response.data;
}
get(endpoint, params = null) { return this.request('GET', endpoint, null, params); }
post(endpoint, data) { return this.request('POST', endpoint, data); }
put(endpoint, data) { return this.request('PUT', endpoint, data); }
delete(endpoint) { return this.request('DELETE', endpoint); }
}