UNPKG

@atlaskit/profilecard

Version:

A React component to display a card with user information.

191 lines 6.35 kB
import { agentRequestAnalytics } from '../util/analytics'; import { getPageTime } from '../util/performance'; import CachingClient from './CachingClient'; import { getErrorAttributes } from './errorUtils'; const createHeaders = (product, cloudId, isBodyJson) => { const headers = new Headers({ 'X-Product': product, 'X-Experience-Id': 'profile-card', 'X-Cloudid': cloudId || '' }); if (isBodyJson) { headers.set('Content-Type', 'application/json'); } return headers; }; export default class RovoAgentCardClient extends CachingClient { constructor(options) { super(options); this.options = options; } makeRequest(id, cloudId) { const product = this.options.productIdentifier || 'rovo'; const headers = createHeaders(product, this.options.cloudId); if (id.type === 'identity') { return fetch(new Request(`/gateway/api/assist/agents/v1/accountid/${id.value}`, { method: 'GET', credentials: 'include', mode: 'cors', headers })).then(response => response.json()); } return fetch(new Request(`/gateway/api/assist/agents/v1/${id.value}`, { method: 'GET', credentials: 'include', mode: 'cors', headers })).then(response => response.json()); } getProfile(id, analytics) { if (!id.value) { return Promise.reject(new Error('Id is missing')); } if (!this.options.cloudId) { return Promise.reject(new Error('cloudId is missing')); } const cache = this.getCachedProfile(id.value); if (cache) { return Promise.resolve(cache); } return new Promise((resolve, reject) => { const startTime = getPageTime(); if (analytics) { analytics(agentRequestAnalytics('triggered')); } this.makeRequest(id, this.options.cloudId || '').then(data => { if (this.cache) { this.setCachedProfile(id.value, data); } if (analytics) { analytics(agentRequestAnalytics('succeeded', 'request', { duration: getPageTime() - startTime, gateway: true })); } resolve(data); }).catch(error => { if (analytics) { analytics(agentRequestAnalytics('failed', 'request', { duration: getPageTime() - startTime, ...getErrorAttributes(error), gateway: true })); } reject(error); }); }); } deleteAgent(agentId, analytics) { if (!this.options.cloudId) { return Promise.reject(new Error('cloudId is missing')); } return new Promise((resolve, reject) => { const startTime = getPageTime(); const product = this.options.productIdentifier || 'rovo'; if (analytics) { analytics(agentRequestAnalytics('triggered')); } const headers = createHeaders(product, this.options.cloudId); fetch(new Request(`/gateway/api/assist/agents/v1/${agentId}`, { method: 'DELETE', credentials: 'include', mode: 'cors', headers })).then(() => { if (analytics) { analytics(agentRequestAnalytics('succeeded', 'deleteAgent', { duration: getPageTime() - startTime, gateway: true })); } resolve(); }).catch(error => { if (analytics) { analytics(agentRequestAnalytics('failed', 'deleteAgent', { duration: getPageTime() - startTime, ...getErrorAttributes(error), gateway: true })); } reject(error); }); }); } setFavouriteAgent(agentId, isFavourite, analytics) { if (!this.options.cloudId) { return Promise.reject(new Error('cloudId is missing')); } return new Promise(async (resolve, reject) => { const startTime = getPageTime(); const product = this.options.productIdentifier || 'rovo'; const actionSubjectId = isFavourite ? 'favourite' : 'unfavourite'; const requestMethod = isFavourite ? 'POST' : 'DELETE'; if (analytics) { analytics(agentRequestAnalytics('triggered', 'actionSubjectId')); } const headers = createHeaders(product, this.options.cloudId); await fetch(new Request(`/gateway/api/assist/agents/v1/${agentId}/favourite`, { method: requestMethod, credentials: 'include', mode: 'cors', headers })).then(() => { if (analytics) { analytics(agentRequestAnalytics('succeeded', actionSubjectId, { duration: getPageTime() - startTime, gateway: true })); } resolve(); }).catch(error => { if (analytics) { analytics(agentRequestAnalytics('failed', actionSubjectId, { duration: getPageTime() - startTime, ...getErrorAttributes(error), gateway: true })); } reject(error); }); }); } getPermissions(id, fireAnalytics) { if (!this.options.cloudId) { return Promise.reject(new Error('cloudId is missing')); } return new Promise((resolve, reject) => { const startTime = getPageTime(); const product = this.options.productIdentifier || 'rovo'; if (fireAnalytics) { fireAnalytics(agentRequestAnalytics('triggered')); } const headers = createHeaders(product, this.options.cloudId, true); fetch(new Request(`/gateway/api/assist/api/rovo/v2/permissions/agents/${id}`, { method: 'POST', credentials: 'include', mode: 'cors', headers, body: JSON.stringify({ permission_ids: ['AGENT_CREATE', 'AGENT_UPDATE', 'AGENT_DELETE', 'AGENT_DEACTIVATE', 'AGENT_READ'] }) })).then(response => response.json()).then(data => { if (fireAnalytics) { fireAnalytics(agentRequestAnalytics('succeeded', 'getAgentPermissions', { duration: getPageTime() - startTime, gateway: true })); } resolve(data); }).catch(error => { if (fireAnalytics) { fireAnalytics(agentRequestAnalytics('failed', 'getAgentPermissions', { duration: getPageTime() - startTime, ...getErrorAttributes(error), gateway: true })); } reject(error); }); }); } }