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