redis-cloud-api-sdk
Version:
This is a client for the Redislabs Cloud API
131 lines (130 loc) • 4.86 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = void 0;
const axios_1 = require("axios");
class Client {
/**
* Initializing the API base of the SDK
* @param parameters The parameters of the SDK
*/
constructor(parameters) {
/**
* The protocol of the API. Default: 'https'
*/
this.protocol = 'https';
/**
* The domain of the API. Default: 'api.redislabs.com'
*/
this.domain = 'api.redislabs.com';
/**
* The version of the API. Default: 'v1'
*/
this.version = 'v1';
/**
* If to report debug logs when performing requests. Default: false
*/
this.debug = false;
if (parameters.protocol !== undefined)
this.protocol = parameters.protocol;
if (parameters.domain !== undefined)
this.domain = parameters.domain;
if (parameters.version !== undefined)
this.version = parameters.version;
if (parameters.debug !== undefined)
this.debug = parameters.debug;
this.accessKey = parameters.accessKey;
this.secretKey = parameters.secretKey;
this.httpClient = axios_1.default.create({
baseURL: `${this.protocol}://${this.domain}/${this.version}`,
responseType: 'json',
headers: {
'Content-Type': 'application/json',
'x-api-key': this.accessKey,
'x-api-secret-key': this.secretKey
}
});
}
/**
* Performing a "GET" request
* @param url The URL of the request
* @returns An Axios Response
*/
get(url) {
return __awaiter(this, void 0, void 0, function* () {
this.log('debug', `Performing GET request for url '${url}'`);
const response = yield this.httpClient.get(url);
this.log('debug', JSON.stringify(response.data));
return response;
});
}
/**
* Performing a "POST" request
* @param url The URL of the request
* @param body The body of the request
* @returns An Axios Response
*/
post(url, body) {
return __awaiter(this, void 0, void 0, function* () {
this.log('debug', `Performing POST request for url '${url}'`);
const response = body ? yield this.httpClient.post(url, body) : yield this.httpClient.post(url);
this.log('debug', JSON.stringify(response.data));
return response;
});
}
/**
* Performing a "PUT" request
* @param url The URL of the request
* @param body The body of the request
* @returns An Axios Response
*/
put(url, body) {
return __awaiter(this, void 0, void 0, function* () {
this.log('debug', `Performing PUT request for url '${url}'`);
const response = body ? yield this.httpClient.put(url, body) : yield this.httpClient.put(url);
this.log('debug', JSON.stringify(response.data));
return response;
});
}
/**
* Performing a "DELETE" request
* @param url The URL of the request
* @returns An Axios Response
*/
delete(url) {
return __awaiter(this, void 0, void 0, function* () {
this.log('debug', `Performing DELETE request for url '${url}'`);
const response = yield this.httpClient.delete(url);
this.log('debug', JSON.stringify(response.data));
return response;
});
}
/**
* Log messages depending on log levels
* @param level The log level
* @param message The message
*/
log(level, message) {
if (level === 'debug' && this.debug === true) {
console.debug(`[Cloud API SDK] ${message}`);
}
}
/**
* Freezing the code for a number of seconds
* @param seconds seconds to freeze the code
*/
sleep(seconds) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
});
}
}
exports.Client = Client;