All files Api.js

96.23% Statements 51/53
83.33% Branches 15/18
100% Functions 15/15
98.08% Lines 51/52
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1011x 1x 1x 1x 1x 1x 1x   1x 1x 1x           40x 40x 40x 40x     43x 2x 1x   1x         6x 6x 6x 6x     3x 2x 2x   3x         3x     3x 3x 3x 1x 1x 1x   1x   2x       2x 1x 1x   2x     1x     1x     1x     2x 1x 1x   2x     1x       1x     2x     1x       1x  
var apiAuth = require('./apiAuth')
var url = require('url')
var path = require('path')
var request = require('request')
var _ = require
const GET = 'GET'
class ApiError extends Error {
  constructor(msg, errorCode, resp) {
    super(msg)
    this.errorCode = errorCode
    this.resp = resp
  }
}
 
class Api {
  constructor(config) {
    this.apiUrl = config.apiUrl
    this.apiKey = config.key
    this.apiSecret = config.secret
    this.proxyUrl = this.buildProxyUrl(config)
  }
  buildProxyUrl(config) {
    if (config.httpsProxy) {
      if (config.proxyUsername) {
        return `https://${config.proxyUsername}:${config.proxyPassword}@${config.httpsProxy}`
      } else {
        return `https://${config.httpsProxy}`
      }
    }
  }
  buildUrl(route, query) {
    var urlInfo = url.parse(this.apiUrl)
    urlInfo.pathname = path.posix.join(urlInfo.pathname, route)
    urlInfo.query = query
    return url.format(urlInfo)
  }
  makeRequest(method, route, query, cb) {
    if (typeof query === 'function') {
      cb = query
      query = null
    }
    var reqOpts = {
      method: method,
      url: this.buildUrl(route, query),
      json: true
    }
    Iif(this.proxyUrl) {
      reqOpts.proxy = this.proxyUrl
    }
    request(apiAuth.signRequest(this.apiKey, this.apiSecret, reqOpts), (err, resp, body) => {
      Iif (err) return cb(err)
      if (resp.statusCode !== 200) {
        var message = body
        Eif (typeof body === 'object') {
          message = JSON.stringify(body, 0, 2)
        }
        return cb(new ApiError(`invalid status code, got ${resp.statusCode}: ${message}`, resp.statusCode, body))
      }
      cb(null, body)
    })
  }
  getDumps(params, cb) {
    if (typeof params === 'function') {
      cb = params
      params = {}
    }
    this.makeRequest(GET, 'account/self/dump', params, cb)
  }
  getLatestFiles(cb) {
    this.makeRequest(GET, 'account/self/file/latest', cb)
  }
  getFilesForDump(dumpId, cb) {
    this.makeRequest(GET, `account/self/file/byDump/${dumpId}`, cb)
  }
  getSync(cb) {
    this.makeRequest(GET, `account/self/file/sync`, cb)
  }
  getFilesForTable(tableName, params, cb) {
    if (typeof params === 'function') {
      cb = params
      params = {}
    }
    this.makeRequest(GET, `account/self/file/byTable/${tableName}`, params, cb)
  }
  getSchemas(cb) {
    this.makeRequest(GET, `schema`, cb)
  }
  // TODO: kill this, keep just in case people are using the API
  getLastestSchema(cb) {
    this.getLatestSchema(cb)
  }
  getLatestSchema(cb) {
    this.makeRequest(GET, 'schema/latest', cb)
  }
  getSchemaVersion(version, cb) {
    this.makeRequest(GET, `schema/${version}`, cb)
  }
}
 
module.exports = Api