1 | import urllib from 'urllib';
|
2 | import AgentKeepalive from 'agentkeepalive';
|
3 | import { getUserAgent } from './common/utils/getUserAgent';
|
4 | import initOptions from './common/client/initOptions';
|
5 |
|
6 | const HttpsAgentKeepalive = AgentKeepalive.HttpsAgent;
|
7 | const globalHttpAgent = new AgentKeepalive();
|
8 | const globalHttpsAgent = new HttpsAgentKeepalive();
|
9 |
|
10 | class Client {
|
11 | public options;
|
12 |
|
13 | public urllib;
|
14 |
|
15 | public agent;
|
16 |
|
17 | public httpsAgent;
|
18 |
|
19 | public ctx;
|
20 |
|
21 | public userAgent;
|
22 |
|
23 | public constructor(options, ctx) {
|
24 | if (!(this instanceof Client)) {
|
25 | return new Client(options, ctx);
|
26 | }
|
27 |
|
28 | if (options && options.inited) {
|
29 | this.options = options;
|
30 | } else {
|
31 | this.options = initOptions(options);
|
32 | }
|
33 |
|
34 |
|
35 | if (this.options.urllib) {
|
36 | this.urllib = this.options.urllib;
|
37 | } else {
|
38 | this.urllib = urllib;
|
39 | this.agent = this.options.agent || globalHttpAgent;
|
40 | this.httpsAgent = this.options.httpsAgent || globalHttpsAgent;
|
41 | }
|
42 | this.ctx = ctx;
|
43 | this.userAgent = getUserAgent();
|
44 | }
|
45 | }
|
46 |
|
47 | let client;
|
48 | export const setConfig = (options, ctx) => {
|
49 | client = new Client(options, ctx);
|
50 | };
|
51 |
|
52 | export {
|
53 | client
|
54 | };
|