This repository was archived by the owner on Dec 26, 2019. It is now read-only.
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
74 lines (58 loc) · 1.92 KB
/
ApiBaseHTTP.coffee
File metadata and controls
74 lines (58 loc) · 1.92 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
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 or @options.oauth_token
throw "`private_token` or `oauth_token` is mandatory"
@options.slumber ?= {}
@options.slumber.append_slash ?= false
@options.url = @options.url.replace(/\/api\/v3/, '')
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 ?= {}
if @options.token
opts.headers = { 'PRIVATE-TOKEN': @options.token }
else
opts.headers = { 'Authorization': 'Bearer ' + @options.oauth_token }
return opts
fn_wrapper: (fn) =>
return (err, response, ret) =>
arity = fn.length
switch arity
when 1 then fn ret
when 2
if typeof response.body == 'object'
fn err, ret || response.body.message
else
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 = @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