-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBrowserStackClient.java
More file actions
591 lines (505 loc) · 21.8 KB
/
BrowserStackClient.java
File metadata and controls
591 lines (505 loc) · 21.8 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
package com.browserstack.client;
import com.browserstack.automate.Automate.BuildStatus;
import com.browserstack.automate.exception.BuildNotFound;
import com.browserstack.automate.exception.SessionNotFound;
import com.browserstack.automate.model.Build;
import com.browserstack.automate.model.BuildNode;
import com.browserstack.automate.model.Session;
import com.browserstack.automate.model.SessionNode;
import com.browserstack.client.exception.BrowserStackException;
import com.browserstack.client.exception.BrowserStackObjectNotFound;
import com.browserstack.client.model.BrowserListing;
import com.browserstack.client.util.BrowserStackCache;
import com.browserstack.client.util.Constants;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
import com.google.api.client.util.ObjectParser;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public abstract class BrowserStackClient implements BrowserStackClientInterface {
private static final String BASE_URL = "https://www.browserstack.com";
private static final String CACHE_KEY_PREFIX_BROWSERS = "browsers";
private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
private static final ObjectParser OBJECT_PARSER = new ObjectParser() {
@Override
public <T> T parseAndClose(InputStream inputStream, Charset charset, Class<T> aClass)
throws IOException {
return JSON_MAPPER.readValue(inputStream, aClass);
}
@Override
public <T> T parseAndClose(Reader reader, Class<T> aClass) throws IOException {
return JSON_MAPPER.readValue(reader, aClass);
}
@Override
public Object parseAndClose(InputStream inputStream, Charset charset, Type type)
throws IOException {
throw new IOException("Unsupported operation");
}
@Override
public Object parseAndClose(Reader reader, Type type) throws IOException {
throw new IOException("Unsupported operation");
}
};
private static HttpTransport HTTP_TRANSPORT = new ApacheHttpTransport();
protected final BrowserStackCache<String, Object> cacheMap;
private HttpRequestFactory requestFactory;
private String baseUrl;
private String username;
private String accessKey;
protected BrowserStackClient() {
this.cacheMap = new BrowserStackCache<>();
this.requestFactory = newRequestFactory();
}
public BrowserStackClient(String baseUrl, String username, String accessKey) {
this();
if (baseUrl == null) {
throw new IllegalArgumentException("Invalid baseUrl");
}
if (username == null || username.trim().length() == 0) {
throw new IllegalArgumentException("Invalid username");
}
if (accessKey == null || accessKey.trim().length() == 0) {
throw new IllegalArgumentException("Invalid access key");
}
this.baseUrl = baseUrl;
this.username = username.trim();
this.accessKey = accessKey.trim();
}
static HttpRequestFactory newRequestFactory() {
return HTTP_TRANSPORT.createRequestFactory(httpRequest -> httpRequest.setParser(OBJECT_PARSER));
}
static HttpRequest newRequest(final HttpRequestFactory requestFactory, final Method method, final GenericUrl url) throws BrowserStackException {
if (method == null) {
throw new IllegalArgumentException("Invalid method");
}
final HttpRequest request;
try {
switch (method) {
case GET:
request = requestFactory.buildGetRequest(url);
break;
case POST:
request = requestFactory.buildPostRequest(url, null);
break;
case PUT:
request = requestFactory.buildPutRequest(url, null);
break;
case DELETE:
request = requestFactory.buildDeleteRequest(url);
break;
default:
throw new IllegalArgumentException("Invalid method");
}
} catch (IOException e) {
throw new BrowserStackException(e);
}
return request;
}
/**
* Sets proxy configuration for requests
*
* @param proxyHost Host of the proxy
* @param proxyPort Port of the proxy
* @param proxyUsername Username of proxy
* @param proxyPassword password of the proxy
*/
@Override
public void setProxy(final String proxyHost, final int proxyPort, final String proxyUsername, final String proxyPassword) {
if (proxyHost == null || proxyPort == 0) {
return;
}
final HttpHost proxy = new HttpHost(proxyHost, proxyPort);
HttpClientBuilder clientBuilder = HttpClientBuilder.create().setProxy(proxy);
if (proxyUsername != null && proxyUsername.length() != 0 && proxyPassword != null && proxyPassword.length() != 0) {
final BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
final AuthScope proxyAuthScope = new AuthScope(proxyHost, proxyPort);
UsernamePasswordCredentials proxyAuthentication =
new UsernamePasswordCredentials(proxyUsername, proxyPassword);
basicCredentialsProvider.setCredentials(proxyAuthScope, proxyAuthentication);
clientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider);
}
final HttpClient client = clientBuilder.build();
HTTP_TRANSPORT = new ApacheHttpTransport(client);
this.requestFactory = newRequestFactory();
}
protected String getAccessKey() {
return accessKey;
}
protected synchronized void setAccessKey(final String accessKey) {
this.accessKey = accessKey;
}
private void checkAuthState() {
if (this.accessKey == null || this.username == null) {
throw new IllegalStateException("Missing API credentials (username or access key is null)");
}
}
protected BrowserListing getBrowsersForProduct(Product product) throws BrowserStackException {
return getBrowsersForProduct(product, true);
}
@SuppressWarnings("unchecked")
protected BrowserListing getBrowsersForProduct(Product product, boolean cache)
throws BrowserStackException {
String productName = product.name().toLowerCase();
String cacheKey = (CACHE_KEY_PREFIX_BROWSERS + productName).toLowerCase();
if (cache) {
if (cacheMap.containsKey(cacheKey)) {
BrowserListing browserListing = (BrowserListing) cacheMap.get(cacheKey);
if (browserListing != null) {
return browserListing;
}
}
}
BrowserListing browserListing;
try {
GenericUrl url =
new GenericUrl(BASE_URL + "/list-of-browsers-and-platforms.json?product=" + productName);
HttpResponse response = newRequest(requestFactory, Method.GET, url).execute();
browserListing = response.parseAs(BrowserListing.class);
} catch (IOException e) {
throw new BrowserStackException(e.getMessage(), 400);
}
if (cache) {
cacheMap.put(cacheKey, browserListing);
}
return browserListing;
}
protected BrowserStackRequest newRequest(final Method method, final String path)
throws BrowserStackException {
return newRequest(method, path, true);
}
protected BrowserStackRequest newRequest(final Method method, final String path,
final boolean prependUrl) throws BrowserStackException {
String urlPath = (path == null) ? "" : path;
GenericUrl url = new GenericUrl(prependUrl ? this.baseUrl + urlPath : urlPath);
return signRequest(newRequest(requestFactory, method, url));
}
protected BrowserStackRequest newRequest(final Method method, final String path,
final Map<String, Object> data) throws BrowserStackException {
return newRequest(method, path, data, null);
}
protected BrowserStackRequest newRequest(final Method method, final String path,
final Map<String, Object> data, final Map<String, String> headers)
throws BrowserStackException {
BrowserStackRequest request = newRequest(method, path);
if (headers != null && headers.size() > 0) {
request.headers(headers);
}
if (data != null && data.size() > 0 && request.canContainBody()) {
try {
request.header("Content-Type", "application/json")
.body(JSON_MAPPER.writeValueAsString(data));
} catch (JsonProcessingException e) {
throw new BrowserStackException(e);
}
}
return request;
}
protected BrowserStackRequest signRequest(final HttpRequest request) {
checkAuthState();
final HttpHeaders header = new HttpHeaders();
final String combined = this.username + ":" + this.accessKey;
final String encoded = new String(Base64.encodeBase64(combined.getBytes()));
final String credential = "Basic " + encoded;
header.set("Authorization", credential);
request.setHeaders(header);
return new BrowserStackRequest(request);
}
/**
* Gets the list of builds.
*
* <p>
* A build is an organizational structure for tests.
* </p>
*
* @param status Return only builds that match the specified build status.
* @param limit Limit results to the specified count.
* @param buildName build name to be searched with.
* @return List of {@link Build} objects.
* @throws BrowserStackException Browserstack exception object.
*/
@Override
public List<Build> getBuilds(final BuildStatus status, final int limit, final String buildName)
throws BrowserStackException {
BrowserStackRequest httpRequest;
try {
httpRequest = newRequest(Method.GET, "/builds.json");
} catch (BrowserStackException e) {
throw new BrowserStackException(e);
}
if (limit > 0) {
httpRequest.queryString(Constants.Filter.LIMIT, limit);
}
if (status != null) {
httpRequest.queryString(Constants.Filter.FILTER, status.name().toLowerCase());
}
if (buildName != null && !buildName.isEmpty()) {
httpRequest.queryString(Constants.Filter.BUILD_NAME, buildName);
}
final BuildNode[] buildNodes = httpRequest.asObject(BuildNode[].class);
final List<Build> builds = new ArrayList<>();
for (BuildNode buildNode : buildNodes) {
if (buildNode != null && buildNode.getBuild() != null) {
builds.add(buildNode.getBuild().setClient(this));
}
}
return builds;
}
/**
* Gets the list of builds via build status and the count required
*
* <p>
* A build is an organizational structure for tests.
* </p>
*
* @param status Return only builds that match the specified build status.
* @param limit Limit results to the specified count.
* @return List of {@link Build} objects.
* @throws BrowserStackException Browserstack exception object.
*/
@Override
public List<Build> getBuilds(final BuildStatus status, final int limit)
throws BrowserStackException {
return getBuilds(status, limit, null);
}
/**
* Gets the list of builds.
*
* <p>
* A build is an organizational structure for tests.
* </p>
*
* @return List of {@link Build} objects.
* @throws BrowserStackException Browserstack exception object.
*/
@Override
public List<Build> getBuilds() throws BrowserStackException {
return getBuilds(null, 0);
}
/**
* Gets the list of builds.
*
* <p>
* A build is an organizational structure for tests.
* </p>
*
* @param limit Limit results to the specified count.
* @return List of {@link Build} objects.
* @throws BrowserStackException Browserstack exception object.
*/
@Override
public List<Build> getBuilds(final int limit) throws BrowserStackException {
return getBuilds(null, limit);
}
/**
* Gets the list of builds.
*
* <p>
* A build is an organizational structure for tests.
* </p>
*
* @param status Include only builds that match the specified build status.
* @return List of {@link Build} objects.
* @throws BrowserStackException Browserstack exception object.
*/
@Override
public List<Build> getBuilds(final BuildStatus status) throws BrowserStackException {
return getBuilds(status, 0);
}
/**
* Gets the build identified by the build identifier.
*
* @param buildId ID that uniquely identifies a build.
* @return List of {@link Build} objects.
* @throws BuildNotFound could not find build with given id
* @throws BrowserStackException Browserstack exception object.
*/
@Override
public Build getBuild(final String buildId) throws BuildNotFound, BrowserStackException {
try {
BuildNode buildNode = newRequest(Method.GET, "/builds/{buildId}.json")
.routeParam("buildId", buildId).asObject(BuildNode.class);
if (buildNode == null) {
throw new BuildNotFound("Build not found: " + buildId);
}
return buildNode.getBuild().setClient(this);
} catch (BrowserStackObjectNotFound e) {
throw new BuildNotFound("Build not found: " + buildId);
}
}
/**
* Gets the build identified using the build name.
*
* @param buildName Name of the build which will be used for searching
* @return {@link Build} object
* @throws BuildNotFound could not find build with given id
* @throws BrowserStackException Browserstack exception object.
*/
@Override
public Build getBuildByName(@Nonnull final String buildName) throws BuildNotFound, BrowserStackException {
final List<Build> build = getBuilds(null, 1, buildName);
if (build.size() == 1) {
return build.get(0);
}
throw new BuildNotFound("Build not found by name: " + buildName);
}
/**
* Delete the build identified by the build identifier.
*
* @param buildId ID that uniquely identifies a build.
* @return true or false based on successful deletion of the build.
* @throws BrowserStackException Browserstack exception object.
*/
@Override
public boolean deleteBuild(final String buildId) throws BrowserStackException {
ObjectNode result = newRequest(Method.DELETE, "/builds/{buildId}.json")
.routeParam("buildId", buildId).asJsonObject();
String status = (result != null) ? result.path("status").asText() : null;
return (status != null && status.equals("ok"));
}
/**
* Retrieves the list of sessions existing under a specific build.
* If no limit is specified, all the sessions will be fetched from that build
*
* @param buildId ID that uniquely identifies a build.
* @param status Include only builds that match the specified build status.
* @param limit Limit results to the specified count.
* @return List of {@link Session} objects containing test session information.
* @throws BuildNotFound could not find build with given id
* @throws BrowserStackException Browserstack exception object.
*/
@Override
public List<Session> getSessions(final String buildId, final BuildStatus status, final int limit)
throws BuildNotFound, BrowserStackException {
// validation of the limit field. Default will be set to 1000 if 0 is provided
final int totalLimit =
(limit <= 0 || limit > Constants.Filter.MAX_SESSIONS)
? Constants.Filter.MAX_SESSIONS
: limit;
int totalRequests = totalLimit / Constants.Filter.MAX_LIMIT;
// An extra request to fetch the remainder sessions
if ((totalLimit % Constants.Filter.MAX_LIMIT) > 0) {
totalRequests++;
}
final List<Session> sessions = new ArrayList<>();
// currReq will act as offset to fetch all* sessions from the build
for (int currReq = 0; currReq < totalRequests; currReq++) {
final List<SessionNode> sessionNodes = getSessionNodes(buildId, status, totalLimit, currReq * Constants.Filter.MAX_LIMIT);
for (SessionNode sessionNode : sessionNodes) {
if (sessionNode != null && sessionNode.getSession() != null) {
sessions.add(sessionNode.getSession().setClient(this));
}
}
// break the loop since there are no more sessions left to fetch
if (sessionNodes.size() < Constants.Filter.MAX_LIMIT) {
break;
}
}
return sessions;
}
private List<SessionNode> getSessionNodes(String buildId, BuildStatus status, int totalLimit, int offset) throws BrowserStackException {
BrowserStackRequest httpRequest = newRequest(Method.GET, "/builds/{buildId}/sessions.json").routeParam(
"buildId", buildId);
httpRequest.queryString(Constants.Filter.LIMIT, totalLimit);
httpRequest.queryString(Constants.Filter.OFFSET, offset);
if (status != null) {
httpRequest.queryString(Constants.Filter.FILTER, status);
}
final List<SessionNode> sessionNodes;
try {
sessionNodes = Arrays.asList(httpRequest.asObject(SessionNode[].class));
} catch (BrowserStackObjectNotFound e) {
throw new BuildNotFound("Build not found: " + buildId);
}
return sessionNodes;
}
/**
* Retrieves the list of sessions existing under a specific build.
*
* @param buildId ID that uniquely identifies a build.
* @return List of {@link Session} objects containing test session information.
* @throws BuildNotFound could not find build with given id
* @throws BrowserStackException Browserstack exception object.
*/
@Override
public List<Session> getSessions(final String buildId)
throws BuildNotFound, BrowserStackException {
return getSessions(buildId, null, 0);
}
/**
* Retrieves the list of sessions existing under a specific build.
*
* @param buildId ID that uniquely identifies a build.
* @param limit Limit results to the specified count.
* @return List of {@link Session} objects containing test session information.
* @throws BuildNotFound could not find build with given id
* @throws BrowserStackException Browserstack exception object.
*/
@Override
public List<Session> getSessions(final String buildId, final int limit)
throws BuildNotFound, BrowserStackException {
return getSessions(buildId, null, limit);
}
/**
* Retrieves the list of sessions existing under a specific build.
*
* @param buildId ID that uniquely identifies a build.
* @param status Include only builds that match the specified build status.
* @return List of {@link Session} objects containing test session information.
* @throws BuildNotFound could not find build with given id
* @throws BrowserStackException Browserstack exception object.
*/
@Override
public List<Session> getSessions(final String buildId, final BuildStatus status)
throws BuildNotFound, BrowserStackException {
return getSessions(buildId, status, 0);
}
/**
*
* @param sessionId ID that uniquely identifies a session.
* @return {@link Session} objects containing test session information.
* @throws SessionNotFound could not find session with given id
* @throws BrowserStackException Browserstack exception object.
*/
@Override
public Session getSession(String sessionId) throws SessionNotFound, BrowserStackException {
try {
SessionNode sessionNode = newRequest(Method.GET, "/sessions/{sessionId}.json")
.routeParam("sessionId", sessionId).asObject(SessionNode.class);
if (sessionNode.getSession() == null) {
throw new SessionNotFound("Session not found: " + sessionId);
}
return sessionNode.getSession().setClient(this);
} catch (BrowserStackObjectNotFound e) {
throw new SessionNotFound("Session not found: " + sessionId);
}
}
public enum Method {
GET, POST, PUT, DELETE
}
public enum Product {
LIVE, AUTOMATE, SCREENSHOTS
}
}