Пример #1
0
  this.dataCache.getFromCache(cacheOptions, cachedData => {
    // data found in the cache, parse into JSON
    // and return to whatever called load()
    if (cachedData) {
      try {
        cachedData = JSON.parse(cachedData.toString())
        return done(null, cachedData)
      } catch (err) {
        log.error(
          'Remote: cache data incomplete, making HTTP request: ' +
            err +
            '(' +
            cacheOptions.endpoint +
            ')'
        )
      }
    }

    debug('load %s', this.endpoint)

    this.getHeaders((err, headers) => {
      err && done(err)

      this.options = Object.assign({}, this.options, headers)

      log.info(
        { module: 'remote' },
        'GET datasource "' +
          this.datasource.schema.datasource.key +
          '": ' +
          decodeURIComponent(this.endpoint)
      )

      const agent = this.options.protocol === 'https' ? https : http

      let request = agent.request(this.options)

      request.on('response', res => {
        // If the token is not valid, we try a second time
        // with a new one.
        if (res.statusCode === 401 && !isRetry) {
          return this.load(requestUrl, done, true)
        }

        this.handleResponse(this.endpoint, res, done)
      })

      request.on('error', err => {
        const message =
          err.toString() + ". Couldn't request data from " + this.endpoint

        err.name = 'GetData'
        err.message = message
        err.remoteIp = this.options.host
        err.remotePort = this.options.port
        return done(err)
      })

      request.end()
    }, isRetry)
  })
Пример #2
0
  setImmediate(() => {
    // Return a 202 Accepted response immediately,
    // along with the datasource response
    if (res.statusCode === 202) {
      return done(null, JSON.parse(data.toString()), res)
    }

    // return 5xx error as the datasource response
    if (res.statusCode && /^5/.exec(res.statusCode)) {
      data = {
        results: [],
        errors: [
          formatError.createWebError('0005', {
            datasource: this.datasource,
            response: res
          })
        ]
      }
    } else if (res.statusCode === 404) {
      data = {
        results: [],
        errors: [
          formatError.createWebError('0004', {
            datasource: this.datasource,
            response: res
          })
        ]
      }
    } else if (res.statusCode && !/200|400/.exec(res.statusCode)) {
      // if the error is anything other than Success or Bad Request, error
      const err = new Error()
      err.message = `Datasource "${this.datasource.name}" failed. ${
        res.statusMessage
      } (${res.statusCode}): ${this.endpoint}`

      if (data) err.message += '\n' + data

      err.remoteIp = this.options.host
      err.remotePort = this.options.port

      log.error(
        { module: 'dadi-api' },
        res.statusMessage + ' (' + res.statusCode + ')' + ': ' + this.endpoint
      )

      // return done(err)
      throw err
    }

    // Cache 200 responses
    if (res.statusCode === 200) {
      log.info(
        { module: 'dadi-api' },
        'GOT datasource "' +
          this.datasource.schema.datasource.key +
          '": ' +
          decodeURIComponent(this.endpoint) +
          ' (HTTP 200, ' +
          require('humanize-plus').fileSize(Buffer.byteLength(data)) +
          ')'
      )

      var cacheOptions = {
        name: this.datasource.name,
        caching: this.schema.datasource.caching,
        endpoint: this.endpoint
      }

      this.dataCache.cacheResponse(cacheOptions, data, written => {
        return done(null, JSON.parse(data.toString()))
      })
    } else {
      if (Buffer.isBuffer(data)) {
        data = data.toString()
      }

      if (typeof data === 'string') {
        data = JSON.parse(data)
      }

      return done(null, data)
    }
  })