vatapi
Version:
vatapi.com API Nodejs library.
63 lines (48 loc) • 1.57 kB
text/coffeescript
debug = require('debug') 'vatapi:ApiBaseHTTP'
{ApiBase} = require './ApiBase'
querystring = require 'querystring'
slumber = require 'slumber'
class module.exports.ApiBaseHTTP extends ApiBase
handleOptions: =>
super
.base_url ?= ''
unless .key
throw "`key` is mandatory"
.slumber ?= {}
.slumber.append_slash ?= false
if .auth?
.slumber.auth = .auth
debug "handleOptions()"
init: =>
super
api = slumber.API 'https://vatapi.com', .slumber
= api(.base_url)
prepare_opts: (opts) =>
opts.__query ?= {}
opts.headers = { 'Apikey': .key }
return opts
fn_wrapper: (fn) =>
return (err, response, ret) =>
arity = fn.length
switch arity
when 1 then fn ret
when 2 then fn err, ret || JSON.parse(response.body).message
when 3 then fn err, response, ret
get: (path, query={}, fn=null) =>
if 'function' is typeof query
fn = query
query = {}
opts = query
(path).get opts, fn
delete: (path, fn=null) =>
opts = {}
(path).delete opts, fn
post: (path, data={}, fn=null) =>
opts = data
(path).post opts, fn
put: (path, data={}, fn=null) =>
opts = data
(path).put opts, fn
patch: (path, data={}, fn=null) =>
opts = data
(path).patch opts, fn