|
1 | 1 | var http = require('http')
|
2 | 2 | var qs = require('querystring')
|
3 | 3 | var getFilesStream = require('./get-files-stream')
|
4 |
| -var config = require('./config') |
5 | 4 |
|
6 |
| -exports = module.exports = requestAPI |
| 5 | +exports = module.exports = function getRequestAPI (config) { |
| 6 | + return requestAPI |
7 | 7 |
|
8 |
| -function requestAPI (path, args, opts, files, buffer, cb) { |
9 |
| - var query, stream, contentType |
10 |
| - contentType = 'application/json' |
| 8 | + function requestAPI (path, args, opts, files, buffer, cb) { |
| 9 | + var query, stream, contentType |
| 10 | + contentType = 'application/json' |
11 | 11 |
|
12 |
| - if (Array.isArray(path)) path = path.join('/') |
| 12 | + if (Array.isArray(path)) path = path.join('/') |
13 | 13 |
|
14 |
| - opts = opts || {} |
| 14 | + opts = opts || {} |
15 | 15 |
|
16 |
| - if (args && !Array.isArray(args)) args = [args] |
17 |
| - if (args) opts.arg = args |
| 16 | + if (args && !Array.isArray(args)) args = [args] |
| 17 | + if (args) opts.arg = args |
18 | 18 |
|
19 |
| - if (files) { |
20 |
| - stream = getFilesStream(files, opts) |
21 |
| - if (!stream.boundary) { |
22 |
| - throw new Error('no boundary in multipart stream') |
| 19 | + if (files) { |
| 20 | + stream = getFilesStream(files, opts) |
| 21 | + if (!stream.boundary) { |
| 22 | + throw new Error('no boundary in multipart stream') |
| 23 | + } |
| 24 | + contentType = 'multipart/form-data; boundary=' + stream.boundary |
23 | 25 | }
|
24 |
| - contentType = 'multipart/form-data; boundary=' + stream.boundary |
25 |
| - } |
26 |
| - |
27 |
| - if (typeof buffer === 'function') { |
28 |
| - cb = buffer |
29 |
| - buffer = false |
30 |
| - } |
31 | 26 |
|
32 |
| - // this option is only used internally, not passed to daemon |
33 |
| - delete opts.followSymlinks |
34 |
| - |
35 |
| - opts['stream-channels'] = true |
36 |
| - query = qs.stringify(opts) |
37 |
| - |
38 |
| - var reqo = { |
39 |
| - method: files ? 'POST' : 'GET', |
40 |
| - host: config.host, |
41 |
| - port: config.port, |
42 |
| - path: config['api-path'] + path + '?' + query, |
43 |
| - headers: { |
44 |
| - 'User-Agent': config['user-agent'], |
45 |
| - 'Content-Type': contentType |
46 |
| - }, |
47 |
| - withCredentials: false |
48 |
| - } |
| 27 | + if (typeof buffer === 'function') { |
| 28 | + cb = buffer |
| 29 | + buffer = false |
| 30 | + } |
49 | 31 |
|
50 |
| - var req = http.request(reqo, function (res) { |
51 |
| - var data = '' |
52 |
| - var objects = [] |
53 |
| - var stream = !!res.headers && !!res.headers['x-stream-output'] |
54 |
| - var chunkedObjects = !!res.headers && !!res.headers['x-chunked-output'] |
| 32 | + // this option is only used internally, not passed to daemon |
| 33 | + delete opts.followSymlinks |
| 34 | + |
| 35 | + opts['stream-channels'] = true |
| 36 | + query = qs.stringify(opts) |
| 37 | + |
| 38 | + var reqo = { |
| 39 | + method: files ? 'POST' : 'GET', |
| 40 | + host: config.host, |
| 41 | + port: config.port, |
| 42 | + path: config['api-path'] + path + '?' + query, |
| 43 | + headers: { |
| 44 | + 'User-Agent': config['user-agent'], |
| 45 | + 'Content-Type': contentType |
| 46 | + }, |
| 47 | + withCredentials: false |
| 48 | + } |
55 | 49 |
|
56 |
| - if (stream && !buffer) return cb(null, res) |
57 |
| - if (chunkedObjects && buffer) return cb(null, res) |
| 50 | + var req = http.request(reqo, function (res) { |
| 51 | + var data = '' |
| 52 | + var objects = [] |
| 53 | + var stream = !!res.headers && !!res.headers['x-stream-output'] |
| 54 | + var chunkedObjects = !!res.headers && !!res.headers['x-chunked-output'] |
58 | 55 |
|
59 |
| - res.on('data', function (chunk) { |
60 |
| - if (!chunkedObjects) { |
61 |
| - data += chunk |
62 |
| - return data |
63 |
| - } |
| 56 | + if (stream && !buffer) return cb(null, res) |
| 57 | + if (chunkedObjects && buffer) return cb(null, res) |
64 | 58 |
|
65 |
| - try { |
66 |
| - var obj = JSON.parse(chunk.toString()) |
67 |
| - objects.push(obj) |
68 |
| - } catch (e) { |
69 |
| - chunkedObjects = false |
70 |
| - data += chunk |
71 |
| - } |
72 |
| - }) |
73 |
| - res.on('end', function () { |
74 |
| - var parsed |
| 59 | + res.on('data', function (chunk) { |
| 60 | + if (!chunkedObjects) { |
| 61 | + data += chunk |
| 62 | + return data |
| 63 | + } |
75 | 64 |
|
76 |
| - if (!chunkedObjects) { |
77 | 65 | try {
|
78 |
| - parsed = JSON.parse(data) |
79 |
| - data = parsed |
80 |
| - } catch (e) {} |
81 |
| - } else { |
82 |
| - data = objects |
83 |
| - } |
84 |
| - |
85 |
| - if (res.statusCode >= 400 || !res.statusCode) { |
86 |
| - if (!data) data = new Error() |
87 |
| - return cb(data, null) |
88 |
| - } |
89 |
| - return cb(null, data) |
| 66 | + var obj = JSON.parse(chunk.toString()) |
| 67 | + objects.push(obj) |
| 68 | + } catch (e) { |
| 69 | + chunkedObjects = false |
| 70 | + data += chunk |
| 71 | + } |
| 72 | + }) |
| 73 | + res.on('end', function () { |
| 74 | + var parsed |
| 75 | + |
| 76 | + if (!chunkedObjects) { |
| 77 | + try { |
| 78 | + parsed = JSON.parse(data) |
| 79 | + data = parsed |
| 80 | + } catch (e) {} |
| 81 | + } else { |
| 82 | + data = objects |
| 83 | + } |
| 84 | + |
| 85 | + if (res.statusCode >= 400 || !res.statusCode) { |
| 86 | + if (!data) data = new Error() |
| 87 | + return cb(data, null) |
| 88 | + } |
| 89 | + return cb(null, data) |
| 90 | + }) |
| 91 | + res.on('error', function (err) { |
| 92 | + return cb(err, null) |
| 93 | + }) |
90 | 94 | })
|
91 |
| - res.on('error', function (err) { |
| 95 | + |
| 96 | + req.on('error', function (err) { |
92 | 97 | return cb(err, null)
|
93 | 98 | })
|
94 |
| - }) |
95 | 99 |
|
96 |
| - req.on('error', function (err) { |
97 |
| - return cb(err, null) |
98 |
| - }) |
| 100 | + if (stream) { |
| 101 | + stream.pipe(req) |
| 102 | + } else { |
| 103 | + req.end() |
| 104 | + } |
99 | 105 |
|
100 |
| - if (stream) { |
101 |
| - stream.pipe(req) |
102 |
| - } else { |
103 |
| - req.end() |
| 106 | + return req |
104 | 107 | }
|
105 |
| - |
106 |
| - return req |
107 | 108 | }
|
0 commit comments