|
| 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 | + |
0 commit comments