This repository has been archived by the owner on Aug 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
client.js
157 lines (131 loc) · 4.11 KB
/
client.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict'
require('dotenv').config();
var https = require('https')
var http = require('http')
var queryString = require('querystring')
// request holds the request to an API Call
var request = {
host: '',
method: '',
path: '',
headers: {},
body: {},
queryParams: {},
test: false, // use this to allow for http calls
port: '' // set the port for http calls
}
var emptyRequest = JSON.parse(JSON.stringify(request))
// response holds the response from an API call, use this as an initializer
// like so: JSON.parse(JSON.stringify(response))
var response = {
'statusCode': '',
'body': {},
'headers': {}
}
// Client allows for quick and easy access any REST or REST-like API.
function Client (globalRequest, maxSockets) {
var emptyResponse = JSON.parse(JSON.stringify(response))
var body = ''
var httpAgent = null
var httpsAgent = null
if (maxSockets) {
httpAgent = new http.Agent({ maxSockets: maxSockets })
httpsAgent = new https.Agent({ maxSockets: maxSockets })
}
// utility function to create an empty request object
this.emptyRequest = function () {
return JSON.parse(JSON.stringify(request))
}
// utility function to detect empty objects
function isEmpty (obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
return false
}
}
return true
}
// add query paramaters to a URL
function buildPath (basePath, queryParams) {
basePath = basePath.concat('?')
var url = basePath.concat(queryString.stringify(queryParams))
return url
}
function buildRequest (globalRequest, endpointRequest) {
var request = JSON.parse(JSON.stringify(globalRequest))
request.host = endpointRequest.host || globalRequest.host
request.method = endpointRequest.method
if (endpointRequest.port) {
request.port = endpointRequest.port
}
// build URL
request.path = !isEmpty(endpointRequest.queryParams)
? buildPath(endpointRequest.path, endpointRequest.queryParams)
: endpointRequest.path
// add headers
if (!isEmpty(endpointRequest.headers)) {
for (var attrname in endpointRequest.headers) {
request.headers[attrname] = endpointRequest.headers[attrname]
}
}
// add the request body's content length
if (!isEmpty(endpointRequest.body)) {
body = JSON.stringify(endpointRequest.body)
request.headers['Content-Length'] = Buffer.byteLength(body)
request.headers['Content-Type'] = 'application/json'
}
// if maxsockets where provided use the apropriate agent
if (maxSockets) {
request.agent = endpointRequest.test === true ? httpAgent : httpsAgent
}
return request
}
// API is the main interface to the API.
this.API = function (endpointRequest, callback) {
var request = buildRequest(globalRequest, endpointRequest)
var requestProtocol = null;
if (endpointRequest.test === true) {
requestProtocol = http
request.port = endpointRequest.port
} else {
requestProtocol = https
}
var httpRequest = requestProtocol.request(request, function (httpResponse) {
var responseBody = ''
// cature the response from the API
httpResponse.on('data', function (chunk) {
responseBody += chunk
})
// after the call is complete, build the response object
httpResponse.on('end', function () {
var response = JSON.parse(JSON.stringify(emptyResponse))
response.statusCode = httpResponse.statusCode
response.body = responseBody
response.headers = httpResponse.headers
callback(response)
})
})
httpRequest.on('error', function (e) {
var response = JSON.parse(JSON.stringify(emptyResponse))
response.statusCode = e.statusCode || 500
response.body = JSON.stringify({
message: e.message,
name: e.name,
stack: e.stack
})
callback(response)
})
// if thre is a request body, sent it
if (!isEmpty(endpointRequest.body)) {
httpRequest.write(body)
}
httpRequest.end()
}
return this
}
module.exports =
{
Client: Client,
request: request,
emptyRequest: emptyRequest
}