Skip to content

Commit

Permalink
Inlined JSON and XML parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
PyvesB committed Sep 3, 2018
1 parent 45f9e8b commit a0de9f9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 79 deletions.
29 changes: 0 additions & 29 deletions lib/response-parsers.js

This file was deleted.

44 changes: 0 additions & 44 deletions lib/response-parsers.spec.js

This file was deleted.

14 changes: 11 additions & 3 deletions services/base-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

// See available emoji at http://emoji.muan.co/
const emojic = require('emojic')
const { asJson } = require('../lib/response-parsers')
const BaseService = require('./base')
const trace = require('./trace')
const { InvalidResponse } = require('./errors')

class BaseJsonService extends BaseService {
async _requestJson({ schema, url, options = {}, errorMessages = {} }) {
Expand All @@ -13,12 +13,20 @@ class BaseJsonService extends BaseService {
...{ headers: { Accept: 'application/json' } },
...options,
}
const jsonData = await this._request({
const { buffer } = await this._request({
url,
options: mergedOptions,
errorMessages,
})
const json = await asJson(jsonData)
let json
try {
json = JSON.parse(buffer)
} catch (err) {
throw new InvalidResponse({
prettyMessage: 'unparseable json response',
underlyingError: err,
})
}
logTrace(emojic.dart, 'Response JSON (before validation)', json, {
deep: true,
})
Expand Down
14 changes: 11 additions & 3 deletions services/base-xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

// See available emoji at http://emoji.muan.co/
const emojic = require('emojic')
const { asXml } = require('../lib/response-parsers')
const fastXmlParser = require('fast-xml-parser')
const BaseService = require('./base')
const trace = require('./trace')
const { InvalidResponse } = require('./errors')

class BaseXmlService extends BaseService {
async _requestXml({ schema, url, options = {}, errorMessages = {} }) {
Expand All @@ -13,12 +14,19 @@ class BaseXmlService extends BaseService {
...{ headers: { Accept: 'application/xml, text/xml' } },
...options,
}
const xmlData = await this._request({
const { buffer } = await this._request({
url,
options: mergedOptions,
errorMessages,
})
const xml = await asXml(xmlData)
const validateResult = fastXmlParser.validate(buffer)
if (validateResult !== true) {
throw new InvalidResponse({
prettyMessage: 'unparseable xml response',
underlyingError: validateResult.err,
})
}
const xml = fastXmlParser.parse(buffer)
logTrace(emojic.dart, 'Response XML (before validation)', xml, {
deep: true,
})
Expand Down

0 comments on commit a0de9f9

Please sign in to comment.