-
Notifications
You must be signed in to change notification settings - Fork 26
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
Add a TwilioAPI client based on the Twilio OpenAPI spec #27
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
const { logger } = require('../messaging/logging'); | ||
|
||
const SCHEMA_TYPE_TO_CONVERT_FUNC_MAP = { | ||
array: 'convertArray', | ||
boolean: 'convertBoolean', | ||
integer: 'convertInteger', | ||
number: 'convertNumber', | ||
object: 'convertObject', | ||
string: 'convertString' | ||
}; | ||
|
||
/** | ||
* A JSON Schema conversion orchestrator. It accepts a JSON schema and value | ||
* and converts any fields that require ... conversion. | ||
*/ | ||
class JsonSchemaConverter { | ||
constructor() { | ||
this.logger = logger; | ||
} | ||
|
||
convertSchema(schema, value) { | ||
if (schema) { | ||
const convertFunc = SCHEMA_TYPE_TO_CONVERT_FUNC_MAP[schema.type]; | ||
|
||
if (convertFunc) { | ||
value = this[convertFunc](schema, value); | ||
} else { | ||
this.logger.debug(`No conversion function for "${schema.type}" schema type`); | ||
} | ||
} | ||
|
||
return value; | ||
} | ||
|
||
convertArray(schema, value) { | ||
if (!value) { | ||
if (!schema.nullable) { | ||
this.logger.debug('Null array found when nullable not allowed by schema: ' + JSON.stringify(schema)); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
// Recurse into the value using the schema's items schema. | ||
return value.map(item => this.convertSchema(schema.items, item)); | ||
} | ||
|
||
convertObject(schema, value) { | ||
const converted = {}; | ||
|
||
let properties = schema.properties; | ||
|
||
// If the schema has no properties, it is a free-form object with arbitrary | ||
// property/value pairs. We'll map the object's keys to null-schemas so | ||
// they'll be processed as-is (i.e., no type so just use the value). | ||
if (!properties) { | ||
const nameList = Object | ||
.keys(value) | ||
.map(name => ({ [name]: null })); | ||
|
||
properties = Object.assign({}, ...nameList); | ||
} | ||
|
||
// Convert each object property and store it in the converted object, if a | ||
// value was provided. | ||
Object.entries(properties).forEach(([name, propSchema]) => { | ||
const { propName, propValue } = this.convertObjectProperty(propSchema, name, value[name]); | ||
|
||
if (propValue !== undefined) { | ||
converted[propName] = propValue; | ||
} | ||
}); | ||
|
||
return converted; | ||
} | ||
|
||
convertObjectProperty(propSchema, propName, propValue) { | ||
if (propValue !== undefined) { | ||
propValue = this.convertSchema(propSchema, propValue); | ||
} | ||
|
||
return { propName, propValue }; | ||
} | ||
|
||
convertBoolean(schema, value) { | ||
return value; | ||
} | ||
|
||
convertInteger(schema, value) { | ||
return value; | ||
} | ||
|
||
convertNumber(schema, value) { | ||
return value; | ||
} | ||
|
||
convertString(schema, value) { | ||
return value; | ||
} | ||
} | ||
|
||
module.exports = JsonSchemaConverter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const JsonSchemaConverter = require('./json-converter'); | ||
const { camelCase } = require('../naming-conventions'); | ||
|
||
const STRING_FORMAT_TO_CONVERT_FUNC_MAP = { | ||
'date-time': 'convertDateTime', | ||
'date-time-rfc-2822': 'convertDateTime', | ||
uri: 'convertUri' | ||
}; | ||
|
||
/** | ||
* A Twilio extension of the JSON Schema converter. We do additional date-time | ||
* conversion and also camelCase object property names (keys). | ||
*/ | ||
class TwilioSchemaConverter extends JsonSchemaConverter { | ||
convertObjectProperty(propSchema, propName, propValue) { | ||
// Convert the property *and* camelCase the key to make it more JSON-ic. | ||
if (propValue !== undefined) { | ||
propValue = this.convertSchema(propSchema, propValue); | ||
} | ||
|
||
return { propName: camelCase(propName), propValue }; | ||
} | ||
|
||
convertString(schema, value) { | ||
if (schema.format) { | ||
const validateFunc = STRING_FORMAT_TO_CONVERT_FUNC_MAP[schema.format]; | ||
|
||
if (validateFunc) { | ||
value = this[validateFunc](schema, value); | ||
} else { | ||
this.logger.debug(`No conversion function for "${schema.format}" schema format`); | ||
} | ||
} | ||
|
||
return value; | ||
} | ||
|
||
convertDateTime(schema, value) { | ||
// The date constructor accepts both ISO 8601 and RFC 2822 date-time formats. | ||
const dateValue = new Date(value); | ||
|
||
if (isNaN(dateValue)) { | ||
this.logger.debug(`Date-Time value "${value}" is not properly formatted for "${schema.format}" schema format`); | ||
return value; | ||
} | ||
|
||
return dateValue; | ||
} | ||
|
||
convertUri(schema, value) { | ||
// We don't currently do any URI conversion. This just keeps from logging non-helpful debug. | ||
return value; | ||
} | ||
} | ||
|
||
module.exports = TwilioSchemaConverter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these just place holders?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, in case anyone wants to extend this and do some conversions.