forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeSubmissionClient.ts
More file actions
59 lines (51 loc) · 2.19 KB
/
NodeSubmissionClient.ts
File metadata and controls
59 lines (51 loc) · 2.19 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
import { Configuration } from '../configuration/Configuration';
import { IEvent } from '../models/IEvent';
import { IUserDescription } from '../models/IUserDescription';
import { DefaultSubmissionClient } from './DefaultSubmissionClient';
import { SettingsResponse } from './SettingsResponse';
import { SubmissionResponse } from './SubmissionResponse';
import { Utils } from '../Utils';
import http = require('http');
import https = require('https');
import url = require('url');
export class NodeSubmissionClient extends DefaultSubmissionClient {
constructor() {
super();
this.configurationVersionHeader = this.configurationVersionHeader.toLowerCase();
}
public sendRequest(config:Configuration, method:string, path:string, data:string, callback: (status:number, message:string, data?:string, headers?:Object) => void): void {
function complete(response:http.IncomingMessage, responseBody:string, responseHeaders:Object) {
var message:string;
if (response.statusCode === 0) {
message = 'Unable to connect to server.';
} else if (response.statusCode < 200 || response.statusCode > 299) {
message = response.statusMessage || (<any>response).message;
}
callback(response.statusCode || 500, message, responseBody, responseHeaders);
}
var parsedHost = url.parse(config.serverUrl);
var options:https.RequestOptions = {
auth: `client:${config.apiKey}`,
headers: {},
hostname: parsedHost.hostname,
method: method,
port: parsedHost.port && parseInt(parsedHost.port),
path: path
};
if (method === 'POST') {
options.headers = {
'Content-Type': 'application/json',
'Content-Length': data.length
}
}
options.headers['User-Agent'] = config.userAgent;
var request:http.ClientRequest = (parsedHost.protocol === 'https' ? https : http).request(options, (response:http.IncomingMessage) => {
var body = '';
response.setEncoding('utf8');
response.on('data', (chunk) => body += chunk);
response.on('end', () => complete(response, body, response.headers));
});
request.on('error', (error:Error) => callback(500, error.message));
request.end(data);
}
}