Skip to content

Commit

Permalink
support to set x-user-agent for request (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
embbnux authored Jun 5, 2020
1 parent 99bef24 commit 1048e12
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 28 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ringcentral-call-control",
"version": "0.2.0",
"version": "0.2.1",
"main": "lib/index.js",
"license": "MIT",
"repository": {
Expand All @@ -20,7 +20,7 @@
"test:showCoverage": "cat ./coverage/lcov.info | coveralls"
},
"devDependencies": {
"@ringcentral/sdk": "^4.1.0",
"@ringcentral/sdk": "^4.2.0",
"@types/jest": "^24.0.18",
"@types/node": "^14.0.1",
"coveralls": "^3.0.6",
Expand Down
79 changes: 64 additions & 15 deletions src/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EventEmitter } from "events";
import { SDK as RingCentralSDK } from '@ringcentral/sdk';

import { formatParty } from './formatParty';
import { USER_AGENT } from './userAgent';

export enum Direction {
inbound = 'Inbound',
Expand Down Expand Up @@ -197,14 +198,16 @@ export class Session extends EventEmitter {
private _eventPartySequences: any;
private _sdk: RingCentralSDK;
private _accountLevel: boolean;
private _userAgent: string;

constructor(rawData: SessionData, sdk: RingCentralSDK, accountLevel: boolean) {
constructor(rawData: SessionData, sdk: RingCentralSDK, accountLevel: boolean, userAgent?: string) {
super();
const { sequence, ...data } = rawData;
this._data = data;
this._eventPartySequences = {};
this._sdk = sdk;
this._accountLevel = !!accountLevel;
this._userAgent = userAgent;

this._updatePartiesSequence(this._data.parties, sequence);

Expand Down Expand Up @@ -324,7 +327,11 @@ export class Session extends EventEmitter {
}

async reload() {
const response = await this._sdk.platform().get(`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}`);
const response = await this._sdk.platform().get(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}`,
null,
this.requestOptions
);
const data = await response.json();
data.extensionId = this.data.extensionId;
data.accountId = this.data.accountId;
Expand All @@ -334,7 +341,9 @@ export class Session extends EventEmitter {

async drop() {
await this._sdk.platform().delete(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}`
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}`,
null,
this.requestOptions
);
}

Expand All @@ -348,7 +357,10 @@ export class Session extends EventEmitter {
async hold() {
const oldParty = this.party;
const response = await this._sdk.platform().post(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${oldParty.id}/hold`
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${oldParty.id}/hold`,
null,
null,
this.requestOptions,
);
const newParty = await response.json();
this.saveNewPartyData(newParty);
Expand All @@ -359,7 +371,10 @@ export class Session extends EventEmitter {
async unhold() {
const oldParty = this.party;
const response = await this._sdk.platform().post(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${oldParty.id}/unhold`
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${oldParty.id}/unhold`,
null,
null,
this.requestOptions,
);
const newParty = await response.json();
this.saveNewPartyData(newParty);
Expand All @@ -369,28 +384,37 @@ export class Session extends EventEmitter {

async toVoicemail() {
await this._sdk.platform().post(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/reject`
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/reject`,
null,
null,
this.requestOptions,
);
}

async ignore(params: IgnoreParams) {
await this._sdk.platform().post(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/ignore`,
params
params,
null,
this.requestOptions,
);
}

async answer(params: AnswerParams) {
await this._sdk.platform().post(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/answer`,
params
params,
null,
this.requestOptions,
);
}

async reply(params: ReplyWithTextParams) {
const response = await this._sdk.platform().post(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/reply`,
params,
null,
this.requestOptions,
);
const rawParty = await response.json();
this.saveNewPartyData(rawParty);
Expand All @@ -401,22 +425,29 @@ export class Session extends EventEmitter {
async forward(params: ForwardParams) {
const response = await this._sdk.platform().post(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/forward`,
params
params,
null,
this.requestOptions,
);
return response.json();
}

async transfer(params: TransferParams) {
const response = await this._sdk.platform().post(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/transfer`,
params
params,
null,
this.requestOptions,
);
return response.json();
}

async park() {
const response = await this._sdk.platform().post(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/park`,
null,
null,
this.requestOptions,
);
return response.json();
}
Expand All @@ -437,7 +468,9 @@ export class Session extends EventEmitter {
async flip(params: FlipParams) {
const response = await this._sdk.platform().post(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/flip`,
params
params,
null,
this.requestOptions,
);
return response.json();
}
Expand All @@ -447,7 +480,8 @@ export class Session extends EventEmitter {
method: 'PATCH',
url: `/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}`,
query: null,
body: params
body: params,
userAgent: this.requestOptions.userAgent,
});
const rawParty = await response.json();
this.saveNewPartyData(rawParty);
Expand All @@ -469,6 +503,9 @@ export class Session extends EventEmitter {
async createRecord() {
const response = await this._sdk.platform().post(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/recordings`,
null,
null,
this.requestOptions,
);
const recording = await response.json();
const recordings = (this.party.recordings || []).filter(r => r.id !== recording.id);
Expand All @@ -484,7 +521,8 @@ export class Session extends EventEmitter {
query: null,
body: {
active: params.active,
}
},
userAgent: this.requestOptions.userAgent,
});
const recording = await response.json();
const recordings = (this.party.recordings || []).filter(r => r.id !== recording.id);
Expand All @@ -506,7 +544,9 @@ export class Session extends EventEmitter {
async supervise(params: SuperviseParams) {
const response = await this._sdk.platform().post(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/supervise`,
params
params,
null,
this.requestOptions,
);
return response.json();
}
Expand All @@ -515,14 +555,23 @@ export class Session extends EventEmitter {
const response = await this._sdk.platform().post(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/bring-in`,
params,
null,
this.requestOptions,
);
return response.json();
}

async removeParty(partyId: string) {
const response = await this._sdk.platform().delete(
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${partyId}`
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${partyId}`,
this.requestOptions,
);
return response.json();
}

get requestOptions() {
return {
userAgent: this._userAgent ? `${this._userAgent} ${USER_AGENT}` : USER_AGENT,
};
}
}
40 changes: 34 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EventEmitter } from 'events';
import { SDK as RingCentralSDK } from '@ringcentral/sdk';
import { Session, SessionData, PartyStatusCode } from './Session';
import { formatParty } from './formatParty';
import { USER_AGENT } from './userAgent';

export interface SessionsMap {
[key: string]: any;
Expand Down Expand Up @@ -76,23 +77,27 @@ export class RingCentralCallControl extends EventEmitter {
private _initializePromise: any;
private _preloadSessions: boolean;
private _preloadDevices: boolean;
private _userAgent: string;

constructor({
sdk,
accountLevel,
preloadSessions = true,
preloadDevices = true,
extensionInfo,
userAgent,
} : {
sdk: RingCentralSDK,
accountLevel?: boolean,
preloadSessions?: boolean,
preloadDevices?: boolean,
extensionInfo?: Extension,
userAgent?: string,
}) {
super();
this._accountLevel = !!accountLevel;
this._sdk = sdk;
this._userAgent = userAgent;
this._sessionsMap = new Map;
this._devices = [];
this._ready = false;
Expand Down Expand Up @@ -173,7 +178,8 @@ export class RingCentralCallControl extends EventEmitter {

private async loadCurrentExtension() {
try {
const response = await this._sdk.platform().get('/restapi/v1.0/account/~/extension/~');
const response =
await this._sdk.platform().get('/restapi/v1.0/account/~/extension/~', null, this.requestOptions);
this._currentExtension = await response.json();
} catch (e) {
console.error('Fetch presence error', e);
Expand All @@ -191,7 +197,7 @@ export class RingCentralCallControl extends EventEmitter {
presenceUrl = '/restapi/v1.0/account/~/presence?detailedTelephonyState=true&sipData=true';
}
try {
const response = await this._sdk.platform().get(presenceUrl);
const response = await this._sdk.platform().get(presenceUrl, null, this.requestOptions);
const data = await response.json();
if (this._accountLevel) {
const presences = data.records;
Expand All @@ -217,7 +223,12 @@ export class RingCentralCallControl extends EventEmitter {
}
try {
await Promise.all(activeCalls.map(async (activeCall) => {
const response = await this._sdk.platform().get(`/restapi/v1.0/account/~/telephony/sessions/${activeCall.telephonySessionId}`);
const response =
await this._sdk.platform().get(
`/restapi/v1.0/account/~/telephony/sessions/${activeCall.telephonySessionId}`,
null,
this.requestOptions,
);
const data = await response.json();
data.extensionId = this.extensionId;
data.accountId = this.accountId;
Expand Down Expand Up @@ -255,7 +266,12 @@ export class RingCentralCallControl extends EventEmitter {

private async loadDevices() {
try {
const response = await this._sdk.platform().get('/restapi/v1.0/account/~/extension/~/device');
const response =
await this._sdk.platform().get(
'/restapi/v1.0/account/~/extension/~/device',
null,
this.requestOptions,
);
const data = await response.json();
this._devices = data.records || [];
} catch (e) {
Expand All @@ -282,7 +298,7 @@ export class RingCentralCallControl extends EventEmitter {
const response = await this._sdk.platform().post('/restapi/v1.0/account/~/telephony/call-out', {
from: { deviceId },
to,
});
}, null, this.requestOptions);
const sessionData = (await response.json()).session;
sessionData.extensionId = this.extensionId;
sessionData.accountId = this.accountId;
Expand All @@ -303,7 +319,13 @@ export class RingCentralCallControl extends EventEmitter {
// Join as HOST with voice by using webphone sdk to call session.voiceCallToken
// Then bring in other telephony session into this conference
public async createConference() {
const response = await this._sdk.platform().post('/restapi/v1.0/account/~/telephony/conference', {});
const response =
await this._sdk.platform().post(
'/restapi/v1.0/account/~/telephony/conference',
{},
null,
this.requestOptions
);
const sessionData = (await response.json()).session;
sessionData.extensionId = this.extensionId;
sessionData.accountId = this.accountId;
Expand Down Expand Up @@ -334,4 +356,10 @@ export class RingCentralCallControl extends EventEmitter {
get ready() {
return this._ready;
}

get requestOptions() {
return {
userAgent: this._userAgent ? `${this._userAgent} ${USER_AGENT}` : USER_AGENT,
};
}
}
1 change: 1 addition & 0 deletions src/userAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const USER_AGENT : string = 'RingCentralCallControl/0.2.1';
16 changes: 16 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RingCentralCallControl } from '../src/index';
import { USER_AGENT } from '../src/userAgent';
import * as mock from './mock/sdk';
import * as extensionInfo from './mock/data/extensionInfo.json';
import * as telephonySessionInboundProceedingMessage from './mock/data/telephonySessionInboundProceedingMessage.json';
Expand Down Expand Up @@ -30,6 +31,10 @@ describe('RingCentral Call Control :: Index', () => {
expect(rcCallControl.ready).toEqual(true);
});

it('should have default userAgent after initialized', () => {
expect(rcCallControl.requestOptions.userAgent).toEqual(USER_AGENT);
});

it('should not initialize duplicate', () => {
rcCallControl.initialize();
expect(rcCallControl._initializePromise).toEqual(null);
Expand Down Expand Up @@ -95,6 +100,17 @@ describe('RingCentral Call Control :: Index', () => {
});
});

describe('Initialize with User Agent', () => {
beforeAll(async () => {
rcCallControl = new RingCentralCallControl({ sdk, userAgent: 'TestAgent' });
await rcCallControl.initialize();
});

it('should be ready after initialized', () => {
expect(rcCallControl.requestOptions.userAgent).toEqual(`TestAgent ${USER_AGENT}`);
});
});

describe('Restore sessions', () => {
it('should restore session as new session', async () => {
const callControl = new RingCentralCallControl({
Expand Down
Loading

0 comments on commit 1048e12

Please sign in to comment.