forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpClient.ts
More file actions
64 lines (57 loc) · 2.48 KB
/
httpClient.ts
File metadata and controls
64 lines (57 loc) · 2.48 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import { parse, ParseError } from 'jsonc-parser';
import * as requestTypes from 'request';
import { IHttpClient } from '../../common/types';
import { IServiceContainer } from '../../ioc/types';
import { IWorkspaceService } from '../application/types';
import { traceError } from '../logger';
@injectable()
export class HttpClient implements IHttpClient {
public readonly requestOptions: requestTypes.CoreOptions;
constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer) {
const workspaceService = serviceContainer.get<IWorkspaceService>(IWorkspaceService);
this.requestOptions = { proxy: workspaceService.getConfiguration('http').get('proxy', '') };
}
public async downloadFile(uri: string): Promise<requestTypes.Request> {
// tslint:disable-next-line:no-any
const request = ((await import('request')) as any) as typeof requestTypes;
return request(uri, this.requestOptions);
}
public async getJSON<T>(uri: string, strict: boolean = true): Promise<T> {
const body = await this.getBody(uri);
return this.parseBodyToJSON(body, strict);
}
public async parseBodyToJSON<T>(body: string, strict: boolean): Promise<T> {
if (strict) {
return JSON.parse(body);
} else {
// tslint:disable-next-line: prefer-const
let errors: ParseError[] = [];
const content = parse(body, errors, { allowTrailingComma: true, disallowComments: false }) as T;
if (errors.length > 0) {
traceError('JSONC parser returned ParseError codes', errors);
}
return content;
}
}
public async getBody(uri: string): Promise<string> {
// tslint:disable-next-line:no-require-imports
const request = require('request') as typeof requestTypes;
return new Promise<string>((resolve, reject) => {
request(uri, this.requestOptions, (ex, response, body) => {
if (ex) {
return reject(ex);
}
if (response.statusCode !== 200) {
return reject(
new Error(`Failed with status ${response.statusCode}, ${response.statusMessage}, Uri ${uri}`)
);
}
resolve(body);
});
});
}
}