1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", { value: true });
|
8 | const http = require("http");
|
9 | const lodash_1 = require("lodash");
|
10 | const request = require("request");
|
11 | const url = require("url");
|
12 | const logger = require("./logger");
|
13 | class RestApiClient {
|
14 | constructor(options) {
|
15 | this.DEFAULT_CONFIG = {
|
16 | protocol: 'https',
|
17 | host: 'localhost',
|
18 | port: '443',
|
19 | user: 'user',
|
20 | password: 'password',
|
21 | base: '',
|
22 | strictSSL: true,
|
23 | };
|
24 | options = lodash_1.merge(this.DEFAULT_CONFIG, options);
|
25 | this.options = options;
|
26 | }
|
27 | encode() {
|
28 | return encodeURIComponent.apply(this, arguments);
|
29 | }
|
30 | url(pathname, query) {
|
31 | const options = this.options;
|
32 | const uri = url.format({
|
33 | query,
|
34 | hostname: options.host,
|
35 | pathname: options.base + pathname,
|
36 | port: options.port,
|
37 | protocol: options.protocol,
|
38 | });
|
39 | return decodeURIComponent(uri);
|
40 | }
|
41 | authorize(p) {
|
42 | const options = this.options;
|
43 | if (p.oauth) {
|
44 | p.oauth = options.oauth;
|
45 | return;
|
46 | }
|
47 | if (typeof options.user === 'string') {
|
48 | p.auth = {
|
49 | user: options.user,
|
50 | pass: options.password,
|
51 | };
|
52 | }
|
53 | }
|
54 | request(method, path, params) {
|
55 | if (typeof path === 'object') {
|
56 | let args = Array.from(path);
|
57 | args.unshift(method);
|
58 | return this.request.apply(this, args);
|
59 | }
|
60 | let options = this.options;
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | const cookie = options.cookie
|
66 | ? {
|
67 | headers: {
|
68 | Cookie: options.cookie,
|
69 | },
|
70 | }
|
71 | : {};
|
72 | let p = Object.assign({}, cookie, { method, strictSSL: options.strictSSL, uri: this.url(path), json: true, followAllRedirects: true });
|
73 | if (params) {
|
74 | p = lodash_1.merge(p, params);
|
75 | }
|
76 | this.authorize(p);
|
77 | let id = Math.floor(Math.random() * 10000000);
|
78 | let begin = new Date().getTime();
|
79 | return new Promise((resolve, reject) => {
|
80 | logger.debug(`New request #${id} started at ${begin}:\n${method} ${p.uri}`);
|
81 | logger.insane(p);
|
82 | request(p, (error, response) => {
|
83 | let end = new Date().getTime();
|
84 | logger.debug(`End of request #${id} at ${end} (${end -
|
85 | begin}ms) with status code: ${response && response.statusCode}`);
|
86 | if (response) {
|
87 | logger.insane('Response headers:');
|
88 | logger.insane(response.headers);
|
89 | logger.debug('Response body');
|
90 | logger.debug(response.body);
|
91 | }
|
92 | if (error) {
|
93 | reject(error);
|
94 | return;
|
95 | }
|
96 | if (response.statusCode < 200 || response.statusCode > 399) {
|
97 | reject({
|
98 | response,
|
99 | error: `${response.statusCode} ${http.STATUS_CODES[response.statusCode]}`,
|
100 | code: response.statusCode,
|
101 | msg: http.STATUS_CODES[response.statusCode],
|
102 | });
|
103 | return;
|
104 | }
|
105 | resolve(response);
|
106 | });
|
107 | });
|
108 | }
|
109 | get() {
|
110 | return this.request('GET', arguments);
|
111 | }
|
112 | post() {
|
113 | return this.request('POST', arguments);
|
114 | }
|
115 | put() {
|
116 | return this.request('PUT', arguments);
|
117 | }
|
118 | delete() {
|
119 | return this.request('DELETE', arguments);
|
120 | }
|
121 | }
|
122 | module.exports = RestApiClient;
|
123 |
|
\ | No newline at end of file |