Skip to content

Commit 34d7595

Browse files
committed
Added wrapper for app automate apis
1 parent d3bde0f commit 34d7595

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.browserstack.appautomate;
2+
3+
import java.io.FileNotFoundException;
4+
5+
import com.browserstack.automate.exception.AppAutomateException;
6+
import com.browserstack.automate.exception.InvalidFileExtensionException;
7+
import com.browserstack.automate.exception.SessionNotFound;
8+
import com.browserstack.automate.model.Session;
9+
10+
/**
11+
* @author Hitesh Raghuvanshi
12+
*/
13+
public interface AppAutomate {
14+
15+
public Session getSession(String sessionId) throws SessionNotFound, AppAutomateException;
16+
17+
public String uploadApp(String filePath)
18+
throws AppAutomateException, FileNotFoundException, InvalidFileExtensionException;
19+
20+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.browserstack.appautomate;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
6+
import com.browserstack.automate.exception.AppAutomateException;
7+
import com.browserstack.automate.exception.InvalidFileExtensionException;
8+
import com.browserstack.automate.exception.SessionNotFound;
9+
import com.browserstack.automate.model.Session;
10+
import com.browserstack.automate.model.SessionNode;
11+
import com.browserstack.client.BrowserStackClient;
12+
import com.browserstack.client.exception.BrowserStackException;
13+
import com.browserstack.client.exception.BrowserStackObjectNotFound;
14+
import com.fasterxml.jackson.databind.node.ObjectNode;
15+
import com.google.api.client.http.FileContent;
16+
import com.google.api.client.http.HttpHeaders;
17+
import com.google.api.client.http.HttpMediaType;
18+
import com.google.api.client.http.MultipartContent;
19+
20+
/**
21+
* @author Hitesh Raghuvanshi
22+
*/
23+
public class AppAutomateClient extends BrowserStackClient implements AppAutomate {
24+
25+
private static final String BASE_URL = "https://api-cloud.browserstack.com/app-automate";
26+
27+
public AppAutomateClient(String username, String accessKey) {
28+
super(BASE_URL, username, accessKey);
29+
}
30+
31+
public Session getSession(String sessionId) throws SessionNotFound, AppAutomateException {
32+
try {
33+
SessionNode sessionNode = newRequest(Method.GET, "/sessions/{sessionId}.json")
34+
.routeParam("sessionId", sessionId).asObject(SessionNode.class);
35+
36+
if (sessionNode.getSession() == null) {
37+
throw new SessionNotFound("Session not found: " + sessionId);
38+
}
39+
40+
return sessionNode.getSession().setClient(this);
41+
} catch (BrowserStackObjectNotFound e) {
42+
throw new SessionNotFound("Session not found: " + sessionId);
43+
} catch (BrowserStackException e) {
44+
throw new AppAutomateException(e);
45+
}
46+
}
47+
48+
public String uploadApp(String filePath)
49+
throws AppAutomateException, FileNotFoundException, InvalidFileExtensionException {
50+
try {
51+
File file = new File(filePath);
52+
53+
if (!file.exists()) {
54+
throw new FileNotFoundException("File not found at " + filePath);
55+
}
56+
57+
if (!filePath.endsWith(".apk") && !filePath.endsWith(".ipa")) {
58+
throw new InvalidFileExtensionException("File extension should be only .apk or .ipa.");
59+
}
60+
61+
MultipartContent content = new MultipartContent()
62+
.setMediaType(new HttpMediaType("multipart/form-data").setParameter("boundary", "__END_OF_PART__"));
63+
64+
FileContent fileContent = new FileContent("multipart/form-data", file);
65+
66+
MultipartContent.Part part = new MultipartContent.Part(fileContent);
67+
part.setHeaders(new HttpHeaders().set("Content-Disposition",
68+
String.format("form-data; name=\"file\"; filename=\"%s\"", file.getName())));
69+
content.addPart(part);
70+
71+
ObjectNode response = newRequest(Method.POST, "/upload").body(content).asJsonObject();
72+
73+
String appId = (response != null) ? response.path("app_url").asText() : null;
74+
75+
return appId;
76+
} catch (BrowserStackException e) {
77+
throw new AppAutomateException(e);
78+
}
79+
}
80+
81+
}
82+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.browserstack.automate.exception;
2+
3+
import com.browserstack.client.exception.BrowserStackException;
4+
5+
/**
6+
* @author Hitesh Raghuvanshi
7+
*/
8+
public class AppAutomateException extends BrowserStackException {
9+
10+
public AppAutomateException(BrowserStackException e) {
11+
super(e.getMessage(), e.getStatusCode());
12+
}
13+
14+
public AppAutomateException(String message, int statusCode) {
15+
super(message, statusCode);
16+
}
17+
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.browserstack.automate.exception;
2+
3+
/**
4+
* @author Hitesh Raghuvanshi
5+
*/
6+
public class InvalidFileExtensionException extends Exception {
7+
8+
public InvalidFileExtensionException(String message) {
9+
super(message);
10+
}
11+
12+
}
13+

0 commit comments

Comments
 (0)