protected String putBodyString(String path) throws Exception { log.info("[PUT] {}", (origin + path)); return Unirest.put(origin + path).asString().getBody(); }
protected String deleteBodyString(String path) throws Exception { log.info("[DELETE] {}", (origin + path)); return Unirest.delete(origin + path).asString().getBody(); }
protected String postBodyString(String path) throws Exception { log.info("[POST] {}", (origin + path)); return Unirest.post(origin + path).asString().getBody(); }
protected void logout() throws IOException { try { Unirest.post(serverUrl + "v1/logout").asString(); this.authToken = ""; this.userId = ""; } catch (UnirestException e) { throw new IOException(e); } }
@Override public Map<String, String> validateSettings(Map<String, String> settings) { Map<String, String> errors = new HashMap<>(); String url = settings.get(SETTING_URL); if (!url.endsWith("/")) { url += "/"; } if (!url.startsWith("http://") && !url.startsWith("https://")) { url = "http://" + url; } try { HttpResponse<String> response = Unirest.post(url + "api.php").header("cache-control", "no-cache").asString(); gson.fromJson(response.getBody(), PiHoleStats.class); } catch (Exception e) { errors.put("Unavailable", "Unable to reach PiHole admin at the following address: " + url + "api.php"); } return errors; }
/** * Send keys to desktop * @param keys */ @Override public void sendKeysWithKeyboard(List<Integer> keyCodes) { if (nodeUrl == null) { throw new ScenarioException("You cannot use keyboard before driver has been created and corresponding node instanciated"); } String keyCodeString = String.join(",", keyCodes .stream() .map(k -> Integer.toString(k)) .collect(Collectors.toList())); logger.info("sending keys: " + keyCodes); try { Unirest.post(String.format("%s%s", nodeUrl, NODE_TASK_SERVLET)) .queryString("action", "sendKeys") .queryString("keycodes", keyCodeString).asString(); } catch (UnirestException e) { logger.warn(String.format("Could send keys: %s", e.getMessage())); } }
/** * Get public stats for the pihole installation * * @return * @throws UnirestException */ public PiHoleStats getStats() throws UnirestException { HttpResponse<String> response = Unirest .post(this.url + "api.php") .header("cache-control", "no-cache").asString(); logger.info("[PiHole] Query result: {} for url:[{}]", response.getBody(), this.url); return gson.fromJson(response.getBody(), PiHoleStats.class); }
/** * Write text to desktop using keyboard * @param text */ @Override public void writeText(String text) { if (nodeUrl == null) { throw new ScenarioException("You cannot write text before driver has been created and corresponding node instanciated"); } logger.info("writing text: " + text); try { Unirest.post(String.format("%s%s", nodeUrl, NODE_TASK_SERVLET)) .queryString("action", "writeText") .queryString("text", text).asString(); } catch (UnirestException e) { logger.warn(String.format("Could not write text: %s", e.getMessage())); } }
/** * Kill process * @param processName */ @Override public void killProcess(String processName) { if (nodeUrl == null) { throw new ScenarioException("You cannot kill a remote process before driver has been created and corresponding node instanciated"); } logger.info("killing process: " + processName); try { Unirest.post(String.format("%s%s", nodeUrl, NODE_TASK_SERVLET)) .queryString("action", "kill") .queryString("process", processName).asString(); } catch (UnirestException e) { logger.warn(String.format("Could not kill process %s: %s", processName, e.getMessage())); } }
/** * Left clic on desktop at x,y * @param x x coordinate * @param y y coordinate */ @Override public void leftClic(int x, int y) { if (nodeUrl == null) { throw new ScenarioException("You cannot clic left before driver has been created and corresponding node instanciated"); } logger.info(String.format("clic left: %d,%d", x, y)); try { Unirest.post(String.format("%s%s", nodeUrl, NODE_TASK_SERVLET)) .queryString("action", "leftClic") .queryString("x", x) .queryString("y", y) .asString(); } catch (UnirestException e) { logger.warn(String.format("Could not clic left: %s", e.getMessage())); } }
/** * right clic on desktop at x,y * @param x x coordinate * @param y y coordinate */ @Override public void rightClic(int x, int y) { if (nodeUrl == null) { throw new ScenarioException("You cannot clic right before driver has been created and corresponding node instanciated"); } logger.info(String.format("clic right: %d,%d", x, y)); try { Unirest.post(String.format("%s%s", nodeUrl, NODE_TASK_SERVLET)) .queryString("action", "rightClic") .queryString("x", x) .queryString("y", y) .asString(); } catch (UnirestException e) { logger.warn(String.format("Could not clic right: %s", e.getMessage())); } }
/** * upload file to browser * @param fileName name of the file to upload * @param base64Content content of the file, encoded in base 64 */ @Override public void uploadFileToBrowser(String fileName, String base64Content) { if (nodeUrl == null) { throw new ScenarioException("You cannot upload file to browser before driver has been created and corresponding node instanciated"); } logger.info("uploading file to browser: " + fileName); try { Unirest.post(String.format("%s%s", nodeUrl, NODE_TASK_SERVLET)) .queryString("action", "uploadFile") .queryString("content", base64Content) .queryString("name", fileName) .asString(); } catch (UnirestException e) { logger.warn(String.format("Could send keys: %s", e.getMessage())); } }
/** * Use this method to get a new TelegramBot instance with the selected auth token * * @param authToken The bots auth token * * @return A new TelegramBot instance or null if something failed */ public static TelegramBot login(String authToken) { try { HttpRequestWithBody request = Unirest.post(API_URL + "bot" + authToken + "/getMe"); HttpResponse<String> response = request.asString(); JSONObject jsonResponse = Utils.processResponse(response); if (jsonResponse != null && Utils.checkResponseStatus(jsonResponse)) { JSONObject result = jsonResponse.getJSONObject("result"); return new TelegramBot(authToken, result.getInt("id"), result.getString("first_name"), result.getString("username")); } } catch (UnirestException e) { e.printStackTrace(); } return null; }
.header("cache-control", "no-cache").asString();
private EmailResponse getEmailResponse(HttpRequestWithBody requestWithBody) { HttpResponse<String> request = null; try { request = requestWithBody.asString(); } catch (UnirestException e) { throw new RuntimeException(e); } switch (request.getStatus()) { case 200: case 400: String response = request.getBody(); try { MailgunEmailResponse emailResponse = objectMapper.readValue(response, MailgunEmailResponse.class); emailResponse.setStatus(request.getStatus()); return emailResponse; } catch (IOException e) { throw new RuntimeException(e); } default: MailgunEmailResponse emailResponse = new MailgunEmailResponse(); emailResponse.setStatus(request.getStatus()); return emailResponse; } } }
private RocketChatClientResponse buildPostCall(RocketChatRestApiV1 call, RocketChatQueryParams queryParams, Object body) throws IOException { HttpRequestWithBody req = Unirest.post(serverUrl + call.getMethodName()).header("Content-Type", "application/json"); if (call.requiresAuth()) { req.header("X-Auth-Token", authToken); req.header("X-User-Id", userId); } if (queryParams != null && queryParams.get() != null && !queryParams.isEmpty()) { for (Entry<? extends String, ? extends String> e : queryParams.get().entrySet()) { req.queryString(e.getKey(), e.getValue()); } } if (body != null) { req.body(objectMapper.writeValueAsString(body)); } try { HttpResponse<String> res = req.asString(); return objectMapper.readValue(res.getBody(), RocketChatClientResponse.class); } catch (UnirestException e) { throw new IOException(e); } } }
/** * checkout entity object so that it can be found during test * used when versionning is enabled * @param entityType * @param entityId * @throws UnirestException */ private void checkoutEntity(String entityType, String entityId) throws UnirestException { Unirest.post(serverUrl + "/rest/domains/{domain}/projects/{project}/{entityType}s/{entityId}/versions/check-out") .routeParam(ENTITY_TYPE, entityType) .routeParam(ENTITY_ID, entityId) .routeParam(PROJECT_NAME, project) .routeParam(DOMAIN_NAME, domain) .asString(); }
/** * lock entity object so that it can be found during test * @param entityType * @param entityId * @throws UnirestException */ private void unlockEntity(String entityType, String entityId) throws UnirestException { Unirest.delete(serverUrl + "/rest/domains/{domain}/projects/{project}/{entityType}s/{entityId}/lock") .routeParam(ENTITY_TYPE, entityType) .routeParam(ENTITY_ID, entityId) .routeParam(PROJECT_NAME, project) .routeParam(DOMAIN_NAME, domain) .asString(); }
/** * checkin entity object so that it can be found during test * used when versionning is enabled * @param entityType * @param entityId * @throws UnirestException */ private void checkinEntity(String entityType, String entityId) throws UnirestException { Unirest.post(serverUrl + "/rest/domains/{domain}/projects/{project}/{entityType}s/{entityId}/versions/check-in") .routeParam(ENTITY_TYPE, entityType) .routeParam(ENTITY_ID, entityId) .routeParam(PROJECT_NAME, project) .routeParam(DOMAIN_NAME, domain) .asString(); }
response = postRequest.body(json).asString(); } else { response = postRequest.asString();