/
mablApiClient.ts
240 lines (219 loc) · 6.32 KB
/
mablApiClient.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import retry from 'async-retry';
import {Application} from './entities/Application';
import {Deployment, DeploymentProperties} from './entities/Deployment';
import {ExecutionResult} from './entities/ExecutionResult';
import axios, {AxiosInstance, AxiosRequestConfig, AxiosResponse} from 'axios';
import {Environment} from './entities/Environment';
import {USER_AGENT} from './constants';
const GET_REQUEST_TIMEOUT_MILLIS = 600_000;
const POST_REQUEST_TIMEOUT_MILLIS = 900_000;
export class MablApiClient {
private readonly httpClient: AxiosInstance;
private readonly baseUrl: string =
process.env.MABL_REST_API_URL ?? 'https://api.mabl.com';
constructor(apiKey: string) {
const config: AxiosRequestConfig = {
headers: {
'User-Agent': USER_AGENT,
Accept: 'application/json',
'Content-Type': 'application/json',
},
auth: {
username: 'key',
password: apiKey,
},
};
this.httpClient = axios.create(config);
}
/**
* Throw helpful error messages, if possible, otherwise throw generic error
* @param response error response
*/
static throwHumanizedError(response: AxiosResponse): never {
switch (response.status) {
case 401:
throw new Error(
`Unauthorized API error, are you sure you passed the correct API key? Is the key "enabled"?`,
);
case 403:
throw new Error(
`Forbidden API error, are you sure you used a "CI/CD Integration" type API key? Ensure this key is for the same workspace you're testing.`,
);
case 404:
throw new Error(
`Not Found API error, please ensure any environment or application IDs in your Action config are correct.`,
);
default:
throw new Error(`[${response.status} - ${response.statusText}]`);
}
}
async makeGetRequest<T>(url: string): Promise<T> {
return retry(
async () => {
const response = await this.httpClient.get<T>(url, {
timeout: GET_REQUEST_TIMEOUT_MILLIS,
});
if ((response.status ?? 400) >= 400) {
MablApiClient.throwHumanizedError(response);
}
return response.data;
},
{
retries: 3,
},
);
}
async makePostRequest<T>(url: string, requestBody: object): Promise<T> {
return retry(
async () => {
const response = await this.httpClient.post(
url,
JSON.stringify(requestBody),
{timeout: POST_REQUEST_TIMEOUT_MILLIS},
);
if ((response.status ?? 400) >= 400) {
throw new Error(`[${response.status} - ${response.statusText}]`);
}
return response.data;
},
{
retries: 3,
},
);
}
async getApplication(id: string): Promise<Application> {
try {
return await this.makeGetRequest<Application>(
`${this.baseUrl}/applications/${id}`,
);
} catch (error) {
throw new Error(
`failed to get mabl application (${id}) from the API ${error}`,
);
}
}
async getEnvironment(id: string): Promise<Environment> {
try {
return await this.makeGetRequest<Environment>(
`${this.baseUrl}/environments/${id}`,
);
} catch (error) {
throw new Error(
`failed to get mabl environment (${id}) from the API ${error}`,
);
}
}
async getExecutionResults(eventId: string): Promise<ExecutionResult> {
try {
return await this.makeGetRequest<ExecutionResult>(
`${this.baseUrl}/execution/result/event/${eventId}`,
);
} catch (error) {
throw new Error(
`failed to get mabl execution results for event ${eventId} from the API ${error}`,
);
}
}
async postDeploymentEvent(
browserTypes: string[],
planLabels: string[],
httpHeaders: string[],
rebaselineImages: boolean,
setStaticBaseline: boolean,
eventTime: number,
properties: DeploymentProperties,
applicationId?: string,
environmentId?: string,
uri?: string,
revision?: string,
mablBranch?: string,
): Promise<Deployment> {
try {
const requestBody: any = this.buildRequestBody(
browserTypes,
planLabels,
httpHeaders,
rebaselineImages,
setStaticBaseline,
eventTime,
properties,
applicationId,
environmentId,
uri,
revision,
mablBranch,
);
return await this.makePostRequest<Deployment>(
`${this.baseUrl}/events/deployment/`,
requestBody,
);
} catch (e) {
throw new Error(`failed to create deployment through mabl API ${e}`);
}
}
buildRequestBody(
browserTypes: string[],
planLabels: string[],
httpHeaders: string[],
rebaselineImages: boolean,
setStaticBaseline: boolean,
eventTime: number,
properties: DeploymentProperties,
applicationId?: string,
environmentId?: string,
uri?: string,
revision?: string,
mablBranch?: string,
): any {
const requestBody: any = {};
if (environmentId) {
requestBody.environment_id = environmentId;
}
if (applicationId) {
requestBody.application_id = applicationId;
}
if (mablBranch) {
requestBody.source_control_tag = mablBranch;
}
const planOverrides: any = {};
if (browserTypes.length) {
planOverrides.browser_types = browserTypes;
}
if (uri) {
planOverrides.uri = uri;
}
if (httpHeaders.length) {
planOverrides.http_headers = httpHeaders.map((header) => {
const parts = header.split(':', 2); // allow for colon in the header value
return {
name: parts[0],
value: parts[1],
log_header_value: false,
};
});
planOverrides.http_headers_required = true;
}
requestBody.plan_overrides = planOverrides;
if (planLabels.length) {
requestBody.plan_labels = planLabels;
}
if (revision) {
requestBody.revision = revision;
}
if (eventTime) {
requestBody.event_time = eventTime;
}
if (properties) {
requestBody.properties = properties;
}
const actions: any = {};
if (rebaselineImages) {
actions.rebaseline_images = rebaselineImages;
}
if (setStaticBaseline) {
actions.set_static_baseline = setStaticBaseline;
}
requestBody.actions = actions;
return requestBody;
}
}