Completed
Pull Request — master (#99)
by
unknown
01:16
created

RestClient.get   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
c 0
b 0
f 0
nc 4
dl 0
loc 23
rs 8.7972
nop 4

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 3 1
1
var _ = require('lodash');
2
var Request = require('./request');
3
var q = require('q');
4
5
/**
6
 * Intermediate class to create HTTP requests
7
 *
8
 *
9
 * @param options       object{
10
 *                          host: '',
11
 *                          endpoint: '', // base url for .request
12
 *                          apiKey: 'API_KEY',
13
 *                          apiSecret: 'API_SECRET'
14
 *                      }
15
 * @constructor
16
 * @constructor
17
 */
18
var RestClient = function(options) {
19
    var self = this;
20
21
    self.apiKey = options.apiKey;
22
    self.apiSecret = options.apiSecret;
23
    self.https = options.https;
24
    self.host = options.host;
25
    self.port = options.port;
26
    self.endpoint = options.endpoint;
27
28
    self.throttleRequestsTimeout = 1000;
29
    self.throttleRequests = self.throttleRequestsTimeout > 0;
30
    self.nextRequest = null;
31
32
    self.defaultParams = {};
33
34
    if (self.apiKey) {
35
        self.defaultParams['api_key'] = self.apiKey;
36
    }
37
38
    self.defaultHeaders = _.defaults({}, {
39
        'X-SDK-Version': 'blocktrail-sdk-nodejs/' + require('./pkginfo').VERSION
40
    }, options.defaultHeaders);
41
};
42
43
RestClient.prototype.throttle = function() {
44
    var self = this;
45
    var deferred = q.defer();
46
47
    if (this.throttleRequests) {
48
        if (this.nextRequest) {
49
            // chain onto the previous delay
50
            this.nextRequest = this.nextRequest.then(function() {
51
                deferred.resolve();
52
53
                return q.delay(self.throttleRequestsTimeout);
54
            });
55
        } else {
56
            // first time we just resolve and setup the delay for the next request
57
            this.nextRequest = q.delay(self.throttleRequestsTimeout);
58
            deferred.resolve();
59
        }
60
    } else {
61
        deferred.resolve();
62
    }
63
64
    return deferred.promise;
65
};
66
67
RestClient.prototype.create_request = function(options) {
68
    var self = this;
69
70
    options = _.defaults({}, options, {
71
        https: self.https,
72
        host: self.host,
73
        port: self.port,
74
        endpoint: self.endpoint,
75
        apiKey: self.apiKey,
76
        apiSecret: self.apiSecret,
77
        params: _.defaults({}, self.defaultParams),
78
        headers: _.defaults({}, self.defaultHeaders)
79
    });
80
81
    return new Request(options);
82
};
83
84
RestClient.prototype.post = function(path, params, data, fn, requireAuth) {
85
    var self = this;
86
87
    requireAuth = typeof requireAuth === "undefined" ? true : requireAuth;
88
89
    var options = {};
90
    if (requireAuth) {
91
        options['auth'] = 'http-signature';
92
    }
93
94
    return self.throttle().then(function() {
95
        return self.create_request(options).request('POST', path, params, data, fn);
96
    });
97
};
98
99
RestClient.prototype.put = function(path, params, data, fn, requireAuth) {
100
    var self = this;
101
102
    requireAuth = typeof requireAuth === "undefined" ? true : requireAuth;
103
104
    var options = {};
105
    if (requireAuth) {
106
        options['auth'] = 'http-signature';
107
    }
108
109
    return self.throttle().then(function() {
110
        return self.create_request(options).request('PUT', path, params, data, fn);
111
    });
112
};
113
114
RestClient.prototype.get = function(path, params, doHttpSignature, fn) {
115
    var self = this;
116
117
    if (typeof doHttpSignature === "function") {
118
        fn = doHttpSignature;
119
        doHttpSignature = false;
120
    }
121
122
    var options = {};
123
124
    if (doHttpSignature) {
125
        options['auth'] = 'http-signature';
126
    }
127
128
    // @TODO: only for during development
129
    if (typeof fn !== "undefined") {
130
        throw new Error("we should be using callbackify!");
131
    }
132
133
    return self.throttle().then(function() {
134
        return self.create_request(options).request('GET', path, params, null, fn);
135
    });
136
};
137
138
RestClient.prototype.delete = function(path, params, data, fn, requireAuth) {
139
    var self = this;
140
141
    requireAuth = typeof requireAuth === "undefined" ? true : requireAuth;
142
143
    var options = {};
144
    if (requireAuth) {
145
        options['auth'] = 'http-signature';
146
    }
147
148
    return self.throttle().then(function() {
149
        return self.create_request(options).request('DELETE', path, params, data, fn);
150
    });
151
};
152
153
module.exports = function(options) {
154
    return new RestClient(options);
155
};
156