forked from Ermlab/nxt-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiBaseHTTP.coffee
More file actions
81 lines (66 loc) · 2.17 KB
/
ApiBaseHTTP.coffee
File metadata and controls
81 lines (66 loc) · 2.17 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
debug = require('debug') 'gitlab:ApiBaseHTTP'
{ApiBase} = require './ApiBase'
querystring = require 'querystring'
rest = require 'rest-js'
class module.exports.ApiBaseHTTP extends ApiBase
handleOptions: =>
super
@options.base_url ?= ''
unless @options.url
throw "`url` is mandatory"
if !@options.token and !@options.access_token
throw "`private_token or access_token` is mandatory"
@options.restOption ||= {defaultFormat: '',crossDomain: true,}
@options.slumber ?= {}
@options.slumber.append_slash ?= false
if @options.auth?
@options.slumber.auth = @options.auth
debug "handleOptions()"
init: =>
super
@slumber = rest @options.url+"/"+@options.base_url+"/",@options.restOption
prepare_opts: (opts) =>
opts.__query ?= {}
if @options.token
opts.__query.private_token = @options.token
if @options.access_token
opts.__query.access_token = @options.access_token
return opts
fn_wrapper: (fn) =>
return (err, 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
authPath:(path,query)=>
opts = @prepare_opts query
if opts.__query.private_token
path+="?private_token="+opts.__query.private_token
if opts.__query.access_token
path+="?access_token="+opts.__query.access_token
return path
get: (path, query={}, fn=null) =>
if 'function' is typeof query
fn = query
query = {}
opts = @prepare_opts query
path = @authPath path,query
@slumber.read path,opts, @fn_wrapper fn
delete: (path, fn=null) =>
opts = @prepare_opts {}
path = @authPath path,{}
@slumber.remove path, opts, @fn_wrapper fn
post: (path, data={}, fn=null) =>
opts = @prepare_opts data
path = @authPath path,data
@slumber.create path, opts, @fn_wrapper fn
put: (path, data={}, fn=null) =>
opts = @prepare_opts data
path = @authPath path,data
@slumber.update path, opts, @fn_wrapper fn
patch: (path, data={}, fn=null) =>
opts = @prepare_opts data
path = @authPath path,data
@slumber.update path, opts, @fn_wrapper fn