channelape-sdk
Version:
A client for interacting with ChannelApe's API
107 lines • 4.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Resource_1 = require("../../../model/Resource");
const Version_1 = require("../../../model/Version");
const q = require("q");
const GenerateApiError_1 = require("../../../utils/GenerateApiError");
const EXPECTED_GET_STATUS = 200;
const EXPECTED_POST_STATUS = 201;
class ApiAccountsService {
constructor(client) {
this.client = client;
}
/**
* Do Not Use. This is for internal use only
*
*/
getById(apiAccountId) {
const deferred = q.defer();
const requestUrl = `/${Version_1.default.V1}${Resource_1.default.BUSINESSES}${Resource_1.default.API_ACCOUNTS}`;
this.client.get(requestUrl, {
params: {
apiAccountId
}
}, (error, response, body) => {
this.mapApiAccountPromise(requestUrl, deferred, error, response, body, EXPECTED_GET_STATUS);
});
return deferred.promise;
}
get(businessId, apiAccountId) {
if (typeof (apiAccountId) === 'undefined') {
const deferred = q.defer();
const requestUrl = `/${Version_1.default.V1}${Resource_1.default.BUSINESSES}/${businessId}${Resource_1.default.API_ACCOUNTS}`;
this.client.get(requestUrl, {}, (error, response, body) => {
if (apiAccountId) {
this.mapApiAccountPromise(requestUrl, deferred, error, response, body, EXPECTED_GET_STATUS);
}
else {
this.mapApiAccountPromises(requestUrl, deferred, error, response, body, EXPECTED_GET_STATUS);
}
});
return deferred.promise;
}
const deferred = q.defer();
const requestUrl = `/${Version_1.default.V1}${Resource_1.default.BUSINESSES}/${businessId}${Resource_1.default.API_ACCOUNTS}/${apiAccountId}`;
this.client.get(requestUrl, {}, (error, response, body) => {
this.mapApiAccountPromise(requestUrl, deferred, error, response, body, EXPECTED_GET_STATUS);
});
return deferred.promise;
}
delete(businessId, apiAccountId) {
const deferred = q.defer();
const requestUrl = `/${Version_1.default.V1}${Resource_1.default.BUSINESSES}/${businessId}${Resource_1.default.API_ACCOUNTS}/${apiAccountId}`;
this.client.delete(requestUrl, {}, (error, response, body) => {
this.mapApiAccountPromise(requestUrl, deferred, error, response, body, EXPECTED_GET_STATUS);
});
return deferred.promise;
}
create(name, businessId) {
const deferred = q.defer();
const requestUrl = `/${Version_1.default.V1}${Resource_1.default.BUSINESSES}/${businessId}${Resource_1.default.API_ACCOUNTS}`;
const options = {
data: {
name
}
};
this.client.post(requestUrl, options, (error, response, body) => {
this.mapApiAccountPromise(requestUrl, deferred, error, response, body, EXPECTED_POST_STATUS);
});
return deferred.promise;
}
mapApiAccountPromise(requestUrl, deferred, error, response, body, expectedStatusCode) {
if (error) {
deferred.reject(error);
}
else if (response.status === expectedStatusCode) {
const apiAccount = this.formatApiAccount(body);
deferred.resolve(apiAccount);
}
else {
const channelApeErrorResponse = (0, GenerateApiError_1.default)(requestUrl, response, body, EXPECTED_GET_STATUS);
deferred.reject(channelApeErrorResponse);
}
}
mapApiAccountPromises(requestUrl, deferred, error, response, body, expectedStatusCode) {
if (error) {
deferred.reject(error);
}
else if (response.status === expectedStatusCode) {
const apiAccounts = this.formatApiAccounts(body.apiAccounts);
deferred.resolve(apiAccounts);
}
else {
const channelApeErrorResponse = (0, GenerateApiError_1.default)(requestUrl, response, body, EXPECTED_GET_STATUS);
deferred.reject(channelApeErrorResponse);
}
}
formatApiAccounts(apiAccounts) {
return apiAccounts.map((apiAccount) => this.formatApiAccount(apiAccount));
}
formatApiAccount(apiAccount) {
apiAccount.creationTime = apiAccount.creationTime ? new Date(apiAccount.creationTime) : undefined;
apiAccount.lastAccessedTime = apiAccount.lastAccessedTime ? new Date(apiAccount.lastAccessedTime) : undefined;
return apiAccount;
}
}
exports.default = ApiAccountsService;
//# sourceMappingURL=ApiAccountsService.js.map