forked from moul/node-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiBaseHTTP.coffee
More file actions
67 lines (52 loc) · 1.61 KB
/
ApiBaseHTTP.coffee
File metadata and controls
67 lines (52 loc) · 1.61 KB
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
debug = require('debug') 'gitlab:ApiBaseHTTP'
{ApiBase} = require './ApiBase'
querystring = require 'querystring'
slumber = require 'slumber'
class module.exports.ApiBaseHTTP extends ApiBase
handleOptions: =>
super
@options.base_url ?= ''
unless @options.url
throw "`url` is mandatory"
unless @options.token
throw "`private_token` is mandatory"
@options.slumber ?= {}
@options.slumber.append_slash ?= false
if @options.auth?
@options.slumber.auth = @options.auth
debug "handleOptions()"
init: =>
super
api = slumber.API @options.url, @options.slumber
@slumber = api(@options.base_url)
prepare_opts: (opts) =>
opts.__query ?= {}
opts.__query.private_token = @options.token
return opts
fn_wrapper: (fn) =>
return (err, response, ret) =>
if err
debug 'an error has occured', err
arity = fn.length
switch arity
when 1 then fn ret
when 2 then fn err, ret
when 3 then fn err, response, ret
get: (path, query={}, fn=null) =>
if 'function' is typeof query
fn = query
query = {}
opts = @prepare_opts query
@slumber(path).get opts, @fn_wrapper fn
delete: (path, fn=null) =>
opts = @prepare_opts {}
@slumber(path).delete opts, @fn_wrapper fn
post: (path, data={}, fn=null) =>
opts = @prepare_opts data
@slumber(path).post opts, @fn_wrapper fn
put: (path, data={}, fn=null) =>
opts = @prepare_opts data
@slumber(path).put opts, @fn_wrapper fn
patch: (path, data={}, fn=null) =>
opts = @prepare_opts data
@slumber(path).patch opts, @fn_wrapper fn