Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

breaking: Remove parameter key translating from OpenAPI client #58

Merged
merged 5 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/services/open-api-client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { logger } = require('./messaging/logging');
const { pascalCase } = require('./naming-conventions');
const { doesObjectHaveProperty, translateKeys } = require('./javascript-utilities');
const { doesObjectHaveProperty } = require('./javascript-utilities');
const JsonSchemaConverter = require('./api-schema/json-converter');

class OpenApiClient {
Expand Down Expand Up @@ -31,9 +30,6 @@ class OpenApiClient {
throw new Error(`Operation not found: ${opts.domain}.${opts.path}.${opts.method}`);
}

// Normalize the request data keys to pascal-case (i.e., upper camel-case).
opts.data = translateKeys(opts.data, pascalCase);

const isPost = (opts.method.toLowerCase() === 'post');
const params = this.getParams(opts, operation);

Expand Down Expand Up @@ -71,11 +67,10 @@ class OpenApiClient {
getParams(opts, operation) {
const params = {};
operation.parameters.forEach(parameter => {
const replaceSymbolName = parameter.name.replace('>', 'After').replace('<', 'Before');
// Build the actual request params from the spec's query parameters. This
// effectively drops all params that are not in the spec.
if (parameter.in === 'query' && doesObjectHaveProperty(opts.data, replaceSymbolName)) {
let value = opts.data[replaceSymbolName];
if (parameter.in === 'query' && doesObjectHaveProperty(opts.data, parameter.name)) {
let value = opts.data[parameter.name];
if (parameter.schema.type === 'boolean') {
value = value.toString();
}
Expand All @@ -92,8 +87,8 @@ class OpenApiClient {
return opts.path.replace(/{(.+?)}/g, (fullMatch, pathNode) => {
let value = '';

if (doesObjectHaveProperty(opts.data, pathNode)) {
value = opts.data[pathNode];
if (doesObjectHaveProperty(opts.pathParams, pathNode)) {
value = opts.pathParams[pathNode];
}

logger.debug(`pathNode=${pathNode}, value=${value}`);
Expand Down
6 changes: 4 additions & 2 deletions src/services/twilio-api/twilio-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class TwilioApiClient {
* @param {string} [opts.password] - The password used for auth
* @param {object} [opts.headers] - The request headers
* @param {object} [opts.data] - The request data
* @param {object} [opts.pathParams] - The request path parameter values
* @param {int} [opts.timeout] - The request timeout in milliseconds
* @param {boolean} [opts.allowRedirects] - Should the client follow redirects
*/
Expand All @@ -129,6 +130,7 @@ class TwilioApiClient {
opts.region = opts.region || this.region;
opts.headers = opts.headers || {};
opts.data = opts.data || {};
opts.pathParams = opts.pathParams || {};

opts.headers['User-Agent'] = `twilio-api-client/${pkg.version} (node.js ${process.version})`;
opts.headers['Accept-Charset'] = 'utf-8';
Expand All @@ -142,8 +144,8 @@ class TwilioApiClient {
}

if (!opts.uri) {
if (opts.path.includes(ACCOUNT_SID_FLAG) && !doesObjectHaveProperty(opts.data, ACCOUNT_SID_FLAG)) {
opts.data[ACCOUNT_SID_FLAG] = this.accountSid;
if (opts.path.includes(ACCOUNT_SID_FLAG) && !doesObjectHaveProperty(opts.pathParams, ACCOUNT_SID_FLAG)) {
opts.pathParams[ACCOUNT_SID_FLAG] = this.accountSid;
}
}

Expand Down
53 changes: 7 additions & 46 deletions test/services/twilio-api/twilio-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,52 +37,12 @@ describe('services', () => {
const response = await client.list({
domain: 'api',
path: '/2010-04-01/Accounts/{AccountSid}/Calls.json',
data: { startTime: callStartTime }
data: { StartTime: callStartTime }
});

expect(response).to.eql([{ sid: callSid }, { sid: callSid }]);
});

test
.nock('https://api.twilio.com', api => {
/* eslint-disable camelcase */
api.get(`/2010-04-01/Accounts/${accountSid}/Calls.json?StartTime%3E=${callStartTime}`).reply(200, {
calls: [{
sid: callSid
}]
});
/* eslint-enable camelcase */
})
.it('test greater than inequality conversion', async () => {
const response = await client.list({
domain: 'api',
path: '/2010-04-01/Accounts/{AccountSid}/Calls.json',
data: { startTimeAfter: callStartTime } // CLI is startTimeAfter
});

expect(response).to.eql([{ sid: callSid }]);
});

test
.nock('https://api.twilio.com', api => {
/* eslint-disable camelcase */
api.get(`/2010-04-01/Accounts/${accountSid}/Calls.json?StartTime%3C=${callStartTime}`).reply(200, {
calls: [{
sid: callSid
}]
});
/* eslint-enable camelcase */
})
.it('test less than inequality conversion', async () => {
const response = await client.list({
domain: 'api',
path: '/2010-04-01/Accounts/{AccountSid}/Calls.json',
data: { startTimeBefore: callStartTime } // CLI is startTimeBefore
});

expect(response).to.eql([{ sid: callSid }]);
});

test
.nock('https://studio.twilio.com', api => {
/* eslint-disable camelcase */
Expand Down Expand Up @@ -123,7 +83,7 @@ describe('services', () => {
const response = await client.create({
domain: 'api',
path: '/2010-04-01/Accounts/{AccountSid}/Calls.json',
data: { to: '123', from: '456', junk: 'disregard' }
data: { To: '123', From: '456', Junk: 'disregard' }
});

expect(response).to.eql({ status: 'ringing' });
Expand All @@ -140,7 +100,7 @@ describe('services', () => {
const response = await client.fetch({
domain: 'api',
path: '/2010-04-01/Accounts/{AccountSid}/Calls/{Sid}.json',
data: { sid: callSid }
pathParams: { Sid: callSid }
});

expect(response).to.eql({ status: 'in-progress' });
Expand All @@ -156,7 +116,8 @@ describe('services', () => {
const response = await client.update({
domain: 'api',
path: '/2010-04-01/Accounts/{AccountSid}/Calls/{Sid}.json',
data: { sid: callSid, status: 'canceled' }
pathParams: { Sid: callSid },
data: { Status: 'canceled' }
});

expect(response).to.eql({ status: 'canceled' });
Expand All @@ -171,7 +132,7 @@ describe('services', () => {
const response = await client.remove({
domain: 'api',
path: '/2010-04-01/Accounts/{AccountSid}/Calls/{Sid}.json',
data: { sid: callSid }
pathParams: { Sid: callSid }
});

expect(response).to.be.true;
Expand Down Expand Up @@ -236,7 +197,7 @@ describe('services', () => {
const response = await client.create({
domain: 'api',
path: '/2010-04-01/Accounts/{AccountSid}/Addresses.json',
data: { emergencyEnabled: true }
data: { EmergencyEnabled: true }
});

expect(response).to.eql({ verified: 'true' });
Expand Down