-
Notifications
You must be signed in to change notification settings - Fork 72.8k
Api3 output renderers #5425
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
Merged
Merged
Api3 output renderers #5425
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
378f94c
APIv3: adding framework for various output renderers
PetrOndrusek ff421d1
APIv3: adding xml output renderer
PetrOndrusek 6e01580
APIv3: adding csv output renderer
PetrOndrusek 99f66b3
APIv3: documenting supported output renderers
PetrOndrusek 23f1c52
APIv3: testing output renderers
PetrOndrusek 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
This file contains hidden or 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 hidden or 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,88 @@ | ||
| # APIv3: Output formats | ||
|
|
||
| ### Choosing output format | ||
| In APIv3, the standard content type is JSON for both HTTP request and HTTP response. | ||
| However, in HTTP response, the response content type can be changed to XML or CSV | ||
| for READ, SEARCH, and HISTORY operations. | ||
|
|
||
| The response content type can be requested in one of the following ways: | ||
| - add a file type extension to the URL, eg. | ||
| `/api/v3/entries.csv?...` | ||
| or `/api/v3/treatments/95e1a6e3-1146-5d6a-a3f1-41567cae0895.xml?...` | ||
| - set `Accept` HTTP request header to `text/csv` or `application/xml` | ||
|
|
||
| The server replies with `406 Not Acceptable` HTTP status in case of not supported content type. | ||
|
|
||
|
|
||
| ### JSON | ||
|
|
||
| Default content type is JSON, output can look like this: | ||
|
|
||
| ``` | ||
| [ | ||
| { | ||
| "type":"sgv", | ||
| "sgv":"171", | ||
| "dateString":"2014-07-19T02:44:15.000-07:00", | ||
| "date":1405763055000, | ||
| "device":"dexcom", | ||
| "direction":"Flat", | ||
| "identifier":"5c5a2404e0196f4d3d9a718a", | ||
| "srvModified":1405763055000, | ||
| "srvCreated":1405763055000 | ||
| }, | ||
| { | ||
| "type":"sgv", | ||
| "sgv":"176", | ||
| "dateString":"2014-07-19T03:09:15.000-07:00", | ||
| "date":1405764555000, | ||
| "device":"dexcom", | ||
| "direction":"Flat", | ||
| "identifier":"5c5a2404e0196f4d3d9a7187", | ||
| "srvModified":1405764555000, | ||
| "srvCreated":1405764555000 | ||
| } | ||
| ] | ||
| ``` | ||
|
|
||
| ### XML | ||
|
|
||
| Sample output: | ||
|
|
||
| ``` | ||
| <?xml version='1.0' encoding='utf-8'?> | ||
| <items> | ||
| <item> | ||
| <type>sgv</type> | ||
| <sgv>171</sgv> | ||
| <dateString>2014-07-19T02:44:15.000-07:00</dateString> | ||
| <date>1405763055000</date> | ||
| <device>dexcom</device> | ||
| <direction>Flat</direction> | ||
| <identifier>5c5a2404e0196f4d3d9a718a</identifier> | ||
| <srvModified>1405763055000</srvModified> | ||
| <srvCreated>1405763055000</srvCreated> | ||
| </item> | ||
| <item> | ||
| <type>sgv</type> | ||
| <sgv>176</sgv> | ||
| <dateString>2014-07-19T03:09:15.000-07:00</dateString> | ||
| <date>1405764555000</date> | ||
| <device>dexcom</device> | ||
| <direction>Flat</direction> | ||
| <identifier>5c5a2404e0196f4d3d9a7187</identifier> | ||
| <srvModified>1405764555000</srvModified> | ||
| <srvCreated>1405764555000</srvCreated> | ||
| </item> | ||
| </items> | ||
| ``` | ||
|
|
||
| ### CSV | ||
|
|
||
| Sample output: | ||
|
|
||
| ``` | ||
| type,sgv,dateString,date,device,direction,identifier,srvModified,srvCreated | ||
| sgv,171,2014-07-19T02:44:15.000-07:00,1405763055000,dexcom,Flat,5c5a2404e0196f4d3d9a718a,1405763055000,1405763055000 | ||
| sgv,176,2014-07-19T03:09:15.000-07:00,1405764555000,dexcom,Flat,5c5a2404e0196f4d3d9a7187,1405764555000,1405764555000 | ||
| ``` |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,99 @@ | ||
| 'use strict'; | ||
|
|
||
| const apiConst = require('../const.json') | ||
| , mime = require('mime') | ||
| , url = require('url') | ||
| , opTools = require('./operationTools') | ||
| , EasyXml = require('easyxml') | ||
| , csvStringify = require('csv-stringify') | ||
| ; | ||
|
|
||
|
|
||
| /** | ||
| * Middleware that converts url's extension to Accept HTTP request header | ||
| * @param {Object} req | ||
| * @param {Object} res | ||
| * @param {Function} next | ||
| */ | ||
| function extension2accept (req, res, next) { | ||
|
|
||
| const pathSplit = req.path.split('.'); | ||
|
|
||
| if (pathSplit.length < 2) | ||
| return next(); | ||
|
|
||
| const pathBase = pathSplit[0] | ||
| , extension = pathSplit.slice(1).join('.'); | ||
|
|
||
| if (!extension) | ||
| return next(); | ||
|
|
||
| const mimeType = mime.getType(extension); | ||
| if (!mimeType) | ||
| return opTools.sendJSONStatus(res, apiConst.HTTP.NOT_ACCEPTABLE, apiConst.MSG.HTTP_406_UNSUPPORTED_FORMAT); | ||
|
|
||
| req.extToAccept = { | ||
| url: req.url, | ||
| accept: req.headers.accept | ||
| }; | ||
|
|
||
| req.headers.accept = mimeType; | ||
| const parsed = url.parse(req.url); | ||
| parsed.pathname = pathBase; | ||
| req.url = url.format(parsed); | ||
|
|
||
| next(); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Sends data to output using the client's desired format | ||
| * @param {Object} res | ||
| * @param {any} data | ||
| */ | ||
| function render (res, data) { | ||
| res.format({ | ||
| 'json': () => res.send(data), | ||
| 'csv': () => renderCsv(res, data), | ||
| 'xml': () => renderXml(res, data), | ||
| 'default': () => | ||
| opTools.sendJSONStatus(res, apiConst.HTTP.NOT_ACCEPTABLE, apiConst.MSG.HTTP_406_UNSUPPORTED_FORMAT) | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Format data to output as .csv | ||
| * @param {Object} res | ||
| * @param {any} data | ||
| */ | ||
| function renderCsv (res, data) { | ||
| const csvSource = Array.isArray(data) ? data : [data]; | ||
| csvStringify(csvSource, { | ||
| header: true | ||
| }, | ||
| function csvStringified (err, output) { | ||
| res.send(output); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Format data to output as .xml | ||
| * @param {Object} res | ||
| * @param {any} data | ||
| */ | ||
| function renderXml (res, data) { | ||
| const serializer = new EasyXml({ | ||
| rootElement: 'item', | ||
| dateFormat: 'ISO', | ||
| manifest: true | ||
| }); | ||
| res.send(serializer.render(data)); | ||
| } | ||
|
|
||
|
|
||
| module.exports = { | ||
| extension2accept, | ||
| render | ||
| }; |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.
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.
This is a breaking change which will result in a 404 for users who have bookmarked this page or scripts that programmatically request this route.
Do we need to change this? If so, what do you think about supporting both
/swagger.yamland/swagger?Uh oh!
There was an error while loading. Please reload this page.
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.
You are right that it looks like
/swagger.yamlURL has changed, but in fact it works both/swagger.yaml,/swagger.Actually
/swagger.xxxreturns JSON everytime, no matter which file extension you use.Only READ, SEARCH, HISTORY operations (GETs for some collection) evaluate the
Accept/extension -renderer.render(res, doc);does that.