headers)
- throws BrowserStackException {
- BrowserStackRequest request = newRequest(method, path);
- if (headers != null && headers.size() > 0) {
- request.headers(headers);
+ 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);
}
- 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);
- }
- }
+ /**
+ * Gets the list of builds.
+ *
+ *
+ * A build is an organizational structure for tests.
+ *
+ *
+ * @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
+ */
+ public List 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);
+ }
- return request;
- }
+ if (limit > 0) {
+ httpRequest.queryString(Constants.Filter.LIMIT, limit);
+ }
- protected BrowserStackRequest signRequest(final HttpRequest request)
- throws BrowserStackException {
- checkAuthState();
+ if (status != null) {
+ httpRequest.queryString(Constants.Filter.FILTER, status.name().toLowerCase());
+ }
- try {
- authentication.intercept(request);
- } catch (IOException e) {
- throw new BrowserStackException(e);
- }
+ if (buildName != null && !buildName.isEmpty()) {
+ httpRequest.queryString(Constants.Filter.BUILD_NAME, buildName);
+ }
- return new BrowserStackRequest(request);
- }
-
- public enum Method {
- GET, POST, PUT, DELETE
- }
-
- public enum Product {
- LIVE, AUTOMATE, SCREENSHOTS
- }
-
- /**
- * Gets the list of builds.
- *
- *
- * A build is an organizational structure for tests.
- *
- *
- * @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
- */
- public List 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);
- }
+ final List buildNodes;
+ try {
+ buildNodes = Arrays.asList(httpRequest.asObject(BuildNode[].class));
+ } catch (BrowserStackException e) {
+ throw e;
+ }
+
+ final List builds = new ArrayList();
+ for (BuildNode buildNode : buildNodes) {
+ if (buildNode != null && buildNode.getBuild() != null) {
+ builds.add(buildNode.getBuild().setClient(this));
+ }
+ }
- if (limit > 0) {
- httpRequest.queryString(Constants.Filter.LIMIT, limit);
+ return builds;
}
- if (status != null) {
- httpRequest.queryString(Constants.Filter.FILTER, status.name().toLowerCase());
+ /**
+ * Gets the list of builds via build status and the count required
+ *
+ *
+ * A build is an organizational structure for tests.
+ *
+ *
+ * @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
+ */
+ public List getBuilds(final BuildStatus status, final int limit)
+ throws BrowserStackException {
+ return getBuilds(status, limit, null);
}
- if (buildName != null && !buildName.isEmpty()) {
- httpRequest.queryString(Constants.Filter.BUILD_NAME, buildName);
+ /**
+ * Gets the list of builds.
+ *
+ *
+ * A build is an organizational structure for tests.
+ *
+ *
+ * @return List of {@link Build} objects.
+ * @throws BrowserStackException
+ */
+ public List getBuilds() throws BrowserStackException {
+ return getBuilds(null, 0);
}
- final List buildNodes;
- try {
- buildNodes = Arrays.asList(httpRequest.asObject(BuildNode[].class));
- } catch (BrowserStackException e) {
- throw e;
+ /**
+ * Gets the list of builds.
+ *
+ *
+ * A build is an organizational structure for tests.
+ *
+ *
+ * @param limit Limit results to the specified count.
+ * @return List of {@link Build} objects.
+ * @throws BrowserStackException
+ */
+ public List getBuilds(final int limit) throws BrowserStackException {
+ return getBuilds(null, limit);
}
- final List builds = new ArrayList();
- for (BuildNode buildNode : buildNodes) {
- if (buildNode != null && buildNode.getBuild() != null) {
- builds.add(buildNode.getBuild().setClient(this));
- }
+ /**
+ * Gets the list of builds.
+ *
+ *
+ * A build is an organizational structure for tests.
+ *
+ *
+ * @param status Include only builds that match the specified build status.
+ * @return List of {@link Build} objects.
+ * @throws BrowserStackException
+ */
+ public List getBuilds(final BuildStatus status) throws BrowserStackException {
+ return getBuilds(status, 0);
}
- return builds;
- }
-
- /**
- * Gets the list of builds via build status and the count required
- *
- *
- * A build is an organizational structure for tests.
- *
- *
- * @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
- */
- public List getBuilds(final BuildStatus status, final int limit)
- throws BrowserStackException {
- return getBuilds(status, limit, null);
- }
-
- /**
- * Gets the list of builds.
- *
- *
- * A build is an organizational structure for tests.
- *
- *
- * @return List of {@link Build} objects.
- * @throws BrowserStackException
- */
- public List getBuilds() throws BrowserStackException {
- return getBuilds(null, 0);
- }
-
- /**
- * Gets the list of builds.
- *
- *
- * A build is an organizational structure for tests.
- *
- *
- * @param limit Limit results to the specified count.
- * @return List of {@link Build} objects.
- * @throws BrowserStackException
- */
- public List getBuilds(final int limit) throws BrowserStackException {
- return getBuilds(null, limit);
- }
-
- /**
- * Gets the list of builds.
- *
- *
- * A build is an organizational structure for tests.
- *
- *
- * @param status Include only builds that match the specified build status.
- * @return List of {@link Build} objects.
- * @throws BrowserStackException
- */
- public List 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
- * @throws BrowserStackException
- */
- 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);
- } catch (BrowserStackException e) {
- throw e;
+ /**
+ * Gets the build identified by the build identifier.
+ *
+ * @param buildId ID that uniquely identifies a build.
+ * @return List of {@link Build} objects.
+ * @throws BuildNotFound
+ * @throws BrowserStackException
+ */
+ 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);
+ } catch (BrowserStackException e) {
+ throw e;
+ }
}
- }
-
- /**
- * 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
- * @throws BrowserStackException
- */
- public Build getBuildByName(@Nonnull final String buildName) throws BuildNotFound, BrowserStackException {
- try {
- final List build = getBuilds(null, 1, buildName);
- if (build.size() == 1) {
- return build.get(0);
- }
- throw new BuildNotFound("Build not found by name: " + buildName);
- } catch (BrowserStackException e) {
- throw e;
+
+ /**
+ * 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
+ * @throws BrowserStackException
+ */
+ public Build getBuildByName(@Nonnull final String buildName) throws BuildNotFound, BrowserStackException {
+ try {
+ final List build = getBuilds(null, 1, buildName);
+ if (build.size() == 1) {
+ return build.get(0);
+ }
+ throw new BuildNotFound("Build not found by name: " + buildName);
+ } catch (BrowserStackException e) {
+ throw e;
+ }
}
- }
-
- /**
- * 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
- */
- public boolean deleteBuild(final String buildId) throws BrowserStackException {
- try {
- ObjectNode result = newRequest(BrowserStackClient.Method.DELETE, "/builds/{buildId}.json")
- .routeParam("buildId", buildId).asJsonObject();
-
- String status = (result != null) ? result.path("status").asText() : null;
- return (status != null && status.equals("ok"));
- } catch (BrowserStackException e) {
- throw e;
+
+ /**
+ * 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
+ */
+ public boolean deleteBuild(final String buildId) throws BrowserStackException {
+ try {
+ ObjectNode result = newRequest(BrowserStackClient.Method.DELETE, "/builds/{buildId}.json")
+ .routeParam("buildId", buildId).asJsonObject();
+
+ String status = (result != null) ? result.path("status").asText() : null;
+ return (status != null && status.equals("ok"));
+ } catch (BrowserStackException e) {
+ throw e;
+ }
}
- }
-
- /**
- * 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
- * @throws BrowserStackException
- */
- public List 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++;
+
+ /**
+ * 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
+ * @throws BrowserStackException
+ */
+ public List 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 sessions = new ArrayList();
+
+ // currReq will act as offset to fetch all* sessions from the build
+ for (int currReq = 0; currReq < totalRequests; currReq++) {
+ final List 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;
}
- final List sessions = new ArrayList();
+ private List getSessionNodes(String buildId, BuildStatus status, int totalLimit, int offset) throws BrowserStackException {
+ BrowserStackRequest httpRequest;
+ try {
+ httpRequest =
+ newRequest(Method.GET, "/builds/{buildId}/sessions.json").routeParam("buildId", buildId);
+ } catch (BrowserStackException e) {
+ throw e;
+ }
- // currReq will act as offset to fetch all* sessions from the build
- for (int currReq = 0; currReq < totalRequests; currReq++) {
- final List sessionNodes = getSessionNodes(buildId, status, totalLimit, currReq * Constants.Filter.MAX_LIMIT);
+ httpRequest.queryString(Constants.Filter.LIMIT, totalLimit);
+ httpRequest.queryString(Constants.Filter.OFFSET, offset);
- for (SessionNode sessionNode : sessionNodes) {
- if (sessionNode != null && sessionNode.getSession() != null) {
- sessions.add(sessionNode.getSession().setClient(this));
+ if (status != null) {
+ httpRequest.queryString(Constants.Filter.FILTER, status);
}
- }
- // break the loop since there are no more sessions left to fetch
- if (sessionNodes.size() < Constants.Filter.MAX_LIMIT) {
- break;
- }
+ final List sessionNodes;
+ try {
+ sessionNodes = Arrays.asList(httpRequest.asObject(SessionNode[].class));
+ } catch (BrowserStackObjectNotFound e) {
+ throw new BuildNotFound("Build not found: " + buildId);
+ } catch (BrowserStackException e) {
+ throw e;
+ }
+ return sessionNodes;
}
- return sessions;
- }
+ /**
+ * 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
+ * @throws BrowserStackException
+ */
+ public List getSessions(final String buildId)
+ throws BuildNotFound, BrowserStackException {
+ return getSessions(buildId, null, 0);
+ }
- private List getSessionNodes(String buildId, BuildStatus status, int totalLimit, int offset) throws BrowserStackException {
- BrowserStackRequest httpRequest;
- try {
- httpRequest =
- newRequest(Method.GET, "/builds/{buildId}/sessions.json").routeParam("buildId", buildId);
- } catch (BrowserStackException e) {
- throw e;
+ /**
+ * 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
+ * @throws BrowserStackException
+ */
+ public List getSessions(final String buildId, final int limit)
+ throws BuildNotFound, BrowserStackException {
+ return getSessions(buildId, null, limit);
}
- httpRequest.queryString(Constants.Filter.LIMIT, totalLimit);
- httpRequest.queryString(Constants.Filter.OFFSET, offset);
+ /**
+ * 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
+ * @throws BrowserStackException
+ */
+ public List getSessions(final String buildId, final BuildStatus status)
+ throws BuildNotFound, BrowserStackException {
+ return getSessions(buildId, status, 0);
+ }
- if (status != null) {
- httpRequest.queryString(Constants.Filter.FILTER, status);
+ /**
+ * Gets the session associated with the specified identifier.
+ *
+ * @param sessionId ID that uniquely identifies a session.
+ * @return {@link Session} objects containing test session information.
+ * @throws SessionNotFound, BrowserStackException
+ */
+ 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);
+ } catch (BrowserStackException e) {
+ throw e;
+ }
}
- final List sessionNodes;
- try {
- sessionNodes = Arrays.asList(httpRequest.asObject(SessionNode[].class));
- } catch (BrowserStackObjectNotFound e) {
- throw new BuildNotFound("Build not found: " + buildId);
- } catch (BrowserStackException e) {
- throw e;
+ public enum Method {
+ GET, POST, PUT, DELETE
}
- 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
- * @throws BrowserStackException
- */
- public List 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
- * @throws BrowserStackException
- */
- public List 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
- * @throws BrowserStackException
- */
- public List getSessions(final String buildId, final BuildStatus status)
- throws BuildNotFound, BrowserStackException {
- return getSessions(buildId, status, 0);
- }
-
- /**
- * Gets the session associated with the specified identifier.
- *
- * @param sessionId ID that uniquely identifies a session.
- * @return {@link Session} objects containing test session information.
- * @throws SessionNotFound, BrowserStackException
- */
- 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);
- } catch (BrowserStackException e) {
- throw e;
+
+ public enum Product {
+ LIVE, AUTOMATE, SCREENSHOTS
}
- }
}
diff --git a/src/main/java/com/browserstack/client/BrowserStackClientInterface.java b/src/main/java/com/browserstack/client/BrowserStackClientInterface.java
index 75534fc..412561a 100644
--- a/src/main/java/com/browserstack/client/BrowserStackClientInterface.java
+++ b/src/main/java/com/browserstack/client/BrowserStackClientInterface.java
@@ -37,4 +37,7 @@ List getSessions(String buildId, BuildStatus status, int limit)
List getSessions(String buildId, BuildStatus status)
throws BuildNotFound, BrowserStackException;
+
+ void setProxy(String proxyHost, int proxyPort, String proxyUsername, String proxyPassword);
+
}