Skip to content

Commit

Permalink
Updates to use response.getHeader for accessing response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrudner committed Oct 5, 2020
1 parent fcf6012 commit d5c0a39
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions lib/recurly/Http.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,8 @@ function makeRequest (options, requestBody) {

const submitRequest = () => {
const httpRequest = https.request(options, (httpResponse) => {
// downcase response headers
const responseHeaders = {}
for (const header in httpResponse.headers) {
responseHeaders[header.toLowerCase()] = httpResponse.headers[header]
}

var responseStream
const encoding = responseHeaders['content-encoding']
const encoding = httpResponse.getHeader('content-encoding')

if (encoding === 'gzip') {
responseStream = zlib.createGunzip(ZLIB_OPTIONS)
Expand All @@ -48,7 +42,7 @@ function makeRequest (options, requestBody) {
} else {
responseStream = httpResponse
}
responseStream.setEncoding(responseEncoding(responseHeaders['content-type']))
responseStream.setEncoding(responseEncoding(httpResponse.getHeader('content-type')))

const chunks = []
responseStream.on('data', chunk => chunks.push(chunk))
Expand Down Expand Up @@ -113,35 +107,30 @@ class Response {
if (body && body.length > 0) {
resp.body = body
}
// downcase response headers
const responseHeaders = {}
for (const header in response.headers) {
responseHeaders[header.toLowerCase()] = response.headers[header]
}
resp.status = response.statusCode
resp.requestId = responseHeaders['x-request-id']
resp.rateLimit = parseInt(responseHeaders['x-ratelimit-limit'], 10)
resp.rateLimitRemaining = parseInt(responseHeaders['x-ratelimit-remaining'], 10)
resp.rateLimitReset = new Date(parseInt(responseHeaders['x-ratelimit-reset'], 10) * 1000)
if (responseHeaders['content-type']) {
resp.contentType = responseHeaders['content-type'].split(';')[0]
resp.requestId = response.getHeader('x-request-id')
resp.rateLimit = parseInt(response.getHeader('x-ratelimit-limit'), 10)
resp.rateLimitRemaining = parseInt(response.getHeader('x-ratelimit-remaining'), 10)
resp.rateLimitReset = new Date(parseInt(response.getHeader('x-ratelimit-reset'), 10) * 1000)
if (response.getHeader('content-type')) {
resp.contentType = response.getHeader('content-type').split(';')[0]
}
resp.recordCount = null
resp.apiDeprecated = false
resp.apiSunsetDate = null
const deprecated = responseHeaders['recurly-deprecated'] || ''
const deprecated = response.getHeader('Recurly-Deprecated')
if (deprecated.toUpperCase() === 'TRUE') {
resp.apiDeprecated = true
resp.apiSunsetDate = responseHeaders['recurly-sunset-date']
resp.apiSunsetDate = response.getHeader('Recurly-Sunset-Date')
}

if (responseHeaders['recurly-total-records']) {
resp.recordCount = parseInt(responseHeaders['recurly-total-records'], 10)
if (response.getHeader('recurly-total-records')) {
resp.recordCount = parseInt(response.getHeader('recurly-total-records'), 10)
}
resp.date = responseHeaders['date']
resp.date = response.getHeader('date')
resp.proxyMetadata = {
'server': responseHeaders['server'],
'cf-ray': responseHeaders['cf-ray']
'server': response.getHeader('server'),
'cf-ray': response.getHeader('cf-ray')
}

return resp
Expand Down

0 comments on commit d5c0a39

Please sign in to comment.