Repository navigation
Expand file tree
/
Copy pathDefaultSubmissionClient.ts
More file actions
131 lines (109 loc) · 4.28 KB
/
Copy pathDefaultSubmissionClient.ts
File metadata and controls
131 lines (109 loc) · 4.28 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
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
import { Configuration } from '../configuration/Configuration';
import { IEvent } from '../models/IEvent';
import { IUserDescription } from '../models/IUserDescription';
import { ISubmissionClient } from './ISubmissionClient';
import { SettingsResponse } from './SettingsResponse';
import { SubmissionResponse } from './SubmissionResponse';
import { Utils } from '../Utils';
declare var XDomainRequest: { new (); create(); };
export class DefaultSubmissionClient implements ISubmissionClient {
public submit(events:IEvent[], config:Configuration): Promise<SubmissionResponse> {
var url = `${config.serverUrl}/api/v2/events?access_token=${encodeURIComponent(config.apiKey)}`;
return this.sendRequest('POST', url, Utils.stringify(events)).then(
xhr => { return new SubmissionResponse(xhr.status, this.getResponseMessage(xhr)); },
xhr => { return new SubmissionResponse(xhr.status || 500, this.getResponseMessage(xhr)); }
);
}
public submitDescription(referenceId:string, description:IUserDescription, config:Configuration): Promise<SubmissionResponse> {
var url = `${config.serverUrl}/api/v2/events/by-ref/${encodeURIComponent(referenceId)}/user-description?access_token=${encodeURIComponent(config.apiKey)}`;
return this.sendRequest('POST', url, Utils.stringify(description)).then(
xhr => { return new SubmissionResponse(xhr.status, this.getResponseMessage(xhr)); },
xhr => { return new SubmissionResponse(xhr.status || 500, this.getResponseMessage(xhr)); }
);
}
public getSettings(config:Configuration): Promise<SettingsResponse> {
var url = `${config.serverUrl}/api/v2/projects/config?access_token=${encodeURIComponent(config.apiKey)}`;
return this.sendRequest('GET', url).then(
xhr => {
if (xhr.status !== 200) {
return new SettingsResponse(false, null, -1, null, `Unable to retrieve configuration settings: ${this.getResponseMessage(xhr)}`);
}
var settings;
try {
settings = JSON.parse(xhr.responseText);
} catch (e) {
config.log.error(`An error occurred while parsing the settings response text: "${xhr.responseText}"`);
}
if (!settings || !settings.settings || !settings.version) {
return new SettingsResponse(true, null, -1, null, 'Invalid configuration settings.');
}
return new SettingsResponse(true, settings.settings, settings.version);
},
xhr => {
return new SettingsResponse(false, null, -1, null, this.getResponseMessage(xhr));
}
);
}
private getResponseMessage(xhr:XMLHttpRequest): string {
if (!xhr || (xhr.status >= 200 && xhr.status <= 299)) {
return null;
}
if (xhr.status === 0) {
return 'Unable to connect to server.';
}
if (xhr.responseBody) {
return xhr.responseBody.message;
}
if (xhr.responseText) {
try {
return JSON.parse(xhr.responseText).message;
} catch (e) {
return xhr.responseText;
}
}
return xhr.statusText;
}
private createRequest(method:string, url:string): XMLHttpRequest {
var xhr:any = new XMLHttpRequest();
if ('withCredentials' in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != 'undefined') {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
if (xhr) {
if (method === 'POST' && xhr.setRequestHeader) {
xhr.setRequestHeader('Content-Type', 'application/json');
}
xhr.timeout = 10000;
}
return xhr;
}
private sendRequest(method:string, url:string, data?:string): Promise<any> {
var xhr = this.createRequest(method || 'POST', url);
return new Promise((resolve, reject) => {
if (!xhr) {
return reject({ status: 503, message: 'CORS not supported.' });
}
if ('withCredentials' in xhr) {
xhr.onreadystatechange = () => {
// xhr not ready.
if (xhr.readyState !== 4) {
return;
}
if (xhr.status >= 200 && xhr.status <= 299) {
resolve(xhr);
} else {
reject(xhr);
}
};
}
xhr.ontimeout = () => reject(xhr);
xhr.onerror = () => reject(xhr);
xhr.onload = () => resolve(xhr);
xhr.send(data);
});
}
}