xcoobee-sdk
Version:
The XcooBee SDK is a facility to abstract lower level calls and implement standard behaviors. The XcooBee team is providing this to improve the speed of implementation and show the best practices while interacting with XcooBee.
243 lines (223 loc) • 9.15 kB
JavaScript
const UsersApi = require('../api/UsersApi');
const ConversationsApi = require('../api/ConversationsApi');
const ErrorResponse = require('./ErrorResponse');
const SdkUtils = require('./SdkUtils');
const SuccessResponse = require('./SuccessResponse');
/**
* The Users SDK service.
* Instances are not created directly. An {@link Sdk} instance will have a
* reference to an `Users` SDK instance through the {@link Sdk#users users}
* property.
*
* ```js
* const XcooBee = require('xcoobee-sdk');
*
* const sdk = new XcooBee.Sdk(...);
* sdk.users.getUser(...).then(...);
* ```
*
* @param {Config} config
* @param {ApiAccessTokenCache} apiAccessTokenCache
* @param {UsersCache} usersCache
*/
class Users {
/* eslint-disable-next-line valid-jsdoc */
/**
* Constructs an `Users` SDK service instance.
*/
constructor(config, apiAccessTokenCache, usersCache) {
this._ = {
apiAccessTokenCache,
config: config || null,
usersCache,
};
}
/**
* @protected
* @param {Config} config
*/
set config(config) {
this._.config = config;
}
/**
* @protected
*/
_assertValidState() {
if (!this._.config) {
throw TypeError('Illegal State: Default config has not been set yet.');
}
}
/**
* Fetches a page of conversations with the given target cursor.
*
* @async
* @param {string} userId
* @param {Config} [config] - The configuration to use instead of the default.
*
* @returns {Promise<PagingResponse | ErrorResponse>}
* @property {number} code - The response status code.
* @property {Error} [error] - The response error if status is not successful.
* @property {string} [error.message] - The error message.
* @property {string} request_id - The ID of the request generated by the XcooBee
* system.
* @property {Object} [result] - The result of the response if status is successful.
* @property {Note[]} result.data - A page of conversations (aka notes).
* @property {Object} [result.page_info] - The page information.
* @property {boolean} result.page_info.has_next_page - Flag indicating whether there is
* another page of data to may be fetched.
* @property {string} result.page_info.end_cursor - The end cursor.
*
* @throws {XcooBeeError}
*/
async getConversation(userId, config = null) {
this._assertValidState();
const fetchPage = async (apiCfg, params) => {
const { apiKey, apiSecret, apiUrlRoot } = apiCfg;
const { after, limit, targetCursor } = params;
const apiAccessToken = await this._.apiAccessTokenCache.get(apiUrlRoot, apiKey, apiSecret);
const conversationsPage = await ConversationsApi.getConversation(
apiUrlRoot, apiAccessToken, targetCursor, after, limit
);
return conversationsPage;
};
const apiCfg = SdkUtils.resolveApiCfg(config, this._.config);
return SdkUtils.startPaging(fetchPage, apiCfg, { targetCursor: userId });
}
/**
* Fetches a page of the user's conversations.
*
* @async
* @param {Config} [config] - The configuration to use instead of the default.
*
* @returns {Promise<PagingResponse | ErrorResponse>}
* @property {number} code - The response status code.
* @property {Error} [error] - The response error if status is not successful.
* @property {string} [error.message] - The error message.
* @property {string} request_id - The ID of the request generated by the XcooBee
* system.
* @property {Object} [result] - The result of the response if status is successful.
* @property {Note[]} result.data - A page of conversations (aka notes).
* @property {Object} [result.page_info] - The page information.
* @property {boolean} result.page_info.has_next_page - Flag indicating whether there is
* another page of data to may be fetched.
* @property {string} result.page_info.end_cursor - The end cursor.
*
* @throws {XcooBeeError}
*/
async getConversations(config = null) {
this._assertValidState();
const fetchPage = async (apiCfg, params) => {
const { apiKey, apiSecret, apiUrlRoot } = apiCfg;
const { after, limit } = params;
const apiAccessToken = await this._.apiAccessTokenCache.get(apiUrlRoot, apiKey, apiSecret);
const user = await this._.usersCache.get(apiUrlRoot, apiKey, apiSecret);
const userCursor = user.cursor;
const conversationsPage = await ConversationsApi.getConversations(
apiUrlRoot, apiAccessToken, userCursor, after, limit
);
return conversationsPage;
};
const apiCfg = SdkUtils.resolveApiCfg(config, this._.config);
return SdkUtils.startPaging(fetchPage, apiCfg, {});
}
/**
* Fetches the user for the specified authentication credentials.
*
* @async
* @param {Config} [config] - The configuration to use instead of the default.
*
* @returns {Promise<SuccessResponse | ErrorResponse>}
* @property {number} code - The response status code.
* @property {Error} [error] - The response error if status is not successful.
* @property {string} [error.message] - The error message.
* @property {string} request_id - The ID of the request generated by the XcooBee
* system.
* @property {Object} [result] - The result of the response if status is successful.
* @property {Note} result.data - Some information on the user.
* @property {string} result.data.cursor -
* @property {string} result.data.pgp_public_key -
* @property {string} result.data.xcoobee_id -
*
* @throws {XcooBeeError}
*/
async getUser(config = null) {
this._assertValidState();
const apiCfg = SdkUtils.resolveApiCfg(config, this._.config);
const { apiKey, apiSecret, apiUrlRoot } = apiCfg;
try {
const userInfo = await this._.usersCache.get(apiUrlRoot, apiKey, apiSecret);
const response = new SuccessResponse({ data: userInfo });
return response;
} catch (err) {
throw new ErrorResponse(400, err);
}
}
/**
* Fetches the user's pgp public key.
*
* @async
* @param {string} xid - User's xcoobee ID.
* @param {Config} [config] - The configuration to use instead of the default.
*
* @returns {Promise<SuccessResponse | ErrorResponse>}
* @property {number} code - The response status code.
* @property {Error} [error] - The response error if status is not successful.
* @property {string} [error.message] - The error message.
* @property {string} request_id - The ID of the request generated by the XcooBee
* system.
* @property {string} result - User's PGP public key or empty string.
*
* @throws {XcooBeeError}
*/
async getUserPublicKey(xid, config = null) {
this._assertValidState();
const apiCfg = SdkUtils.resolveApiCfg(config, this._.config);
const { apiKey, apiSecret, apiUrlRoot } = apiCfg;
try {
const apiAccessToken = await this._.apiAccessTokenCache.get(apiUrlRoot, apiKey, apiSecret);
const pgpKey = await UsersApi.getUserPublicKey(apiUrlRoot, apiAccessToken, xid);
const response = new SuccessResponse(pgpKey);
return response;
} catch (err) {
throw new ErrorResponse(400, err);
}
}
/**
* Sends a message to a consent destination or a breach destination.
*
* @async
* @param {string} message
* @param {Reference} reference - at least one of following keys should be provided
* @property {string} consentId
* @property {string} ticketId
* @property {string} complaintRef
* @property {string} requestRef - data request reference
* @param {Config} [config] - The configuration to use instead of the default.
*
* @returns {Promise<SuccessResponse | ErrorResponse>}
* @property {number} code - The response status code.
* @property {Error} [error] - The response error if status is not successful.
* @property {string} [error.message] - The error message.
* @property {string} request_id - The ID of the request generated by the XcooBee
* system.
* @property {Object} [result] - The result of the response if status is successful.
* @property {Note} result.data - Some information on the newly created note.
* @property {string} result.data.target_cursor -
*
* @throws {XcooBeeError}
*/
async sendUserMessage(message, reference, config = null) {
this._assertValidState();
const apiCfg = SdkUtils.resolveApiCfg(config, this._.config);
const { apiKey, apiSecret, apiUrlRoot } = apiCfg;
try {
const apiAccessToken = await this._.apiAccessTokenCache.get(apiUrlRoot, apiKey, apiSecret);
const note = await ConversationsApi.sendUserMessage(apiUrlRoot, apiAccessToken, message, reference);
const response = new SuccessResponse({ data: note });
return response;
} catch (err) {
throw new ErrorResponse(400, err);
}
}
}// eo class Users
module.exports = Users;