public void completed(HttpResponse<JsonNode> response) { JSONObject responseBody = response.getBody().getObject(); if ("ERROR".equals(responseBody.getString("result"))) { onFail((JSONObject) responseBody.get("data")); return; } onSuccess(responseBody.getJSONObject("data")); }
private static void checkForError (HttpResponse<JsonNode> response) throws Exception { JSONObject jsonResponse = response.getBody().getObject(); JSONArray errors = (JSONArray) jsonResponse.get("errors"); if (errors.length() > 0) { JSONObject error = (JSONObject) errors.get(0); String errorMsg = error.get("code").toString() + ": \"" + error.get("message").toString() + "\""; throw new Exception(errorMsg); } }
/** * Reset the state of the environment and return an initial observation. * * @return initial observation */ public O reset() { JsonNode resetRep = ClientUtils.post(url + ENVS_ROOT + instanceId + RESET, new JSONObject()); return observationSpace.getValue(resetRep.getObject(), "observation"); }
/** * transfer script from DCM */ public static ScriptRequestResponse getScriptFromRequest(HttpResponse<JsonNode> uploadRequest) { int status = uploadRequest.getStatus(); JsonNode body = uploadRequest.getBody(); logger.fine("Got " + status + " with body: " + body); if (status == 404) { return new ScriptRequestResponse(status); } int httpStatusCode = uploadRequest.getStatus(); String script = body.getObject().getString("script"); String datasetIdentifier = body.getObject().getString("datasetIdentifier"); long userId = body.getObject().getLong("userId"); ScriptRequestResponse scriptRequestResponse = new ScriptRequestResponse(httpStatusCode, datasetIdentifier, userId, script); return scriptRequestResponse; }
public List<GankIssue> loadGankIOIssues() throws UnirestException { List<GankIssue> issues = new ArrayList<GankIssue>(); HttpResponse<JsonNode> jsonResponse = Unirest.get(API_DATES).asJson(); JSONObject jsonObject = jsonResponse.getBody().getObject(); JSONArray results = jsonObject.getJSONArray("results"); int len = results.length(); for (int i = 0; i < len; i++) { issues.add(parseIssue(results.getString(i), len - i)); } return issues; }
public String ask(String query) throws UnirestException { JSONObject jsonOut = new JSONObject(); jsonOut.put("user", user) .put("key", key) .put("nick", session) .put("text", query); RequestBodyEntity post = Unirest.post("https://cleverbot.io/1.0/ask").header("Content-Type", "application/json") .body(jsonOut.toString()); JSONObject json = post.asJson().getBody().getObject(); return json.getString("response"); } }
public boolean isMountebankAllowingInjection() { try { HttpResponse<JsonNode> response = Unirest.get(baseUrl + "/config").asJson(); return response.getBody().getObject().getJSONObject("options").getBoolean("allowInjection"); } catch (UnirestException e) { return false; } }
public int getImposterCount() { try { HttpResponse<JsonNode> response = Unirest.get(baseUrl + "/imposters").asJson(); return ((JSONArray) response.getBody().getObject().get("imposters")).length(); } catch (UnirestException e) { return -1; } }
public static <O, A, AS extends ActionSpace<A>> Client<O, A, AS> build(String url, String envId, boolean render) { JSONObject body = new JSONObject().put("env_id", envId); JSONObject reply = ClientUtils.post(url + Client.ENVS_ROOT, body).getObject(); String instanceId; try { instanceId = reply.getString("instance_id"); } catch (JSONException e) { throw new RuntimeException("Environment id not found", e); } GymObservationSpace<O> observationSpace = fetchObservationSpace(url, instanceId); AS actionSpace = fetchActionSpace(url, instanceId); return new Client(url, envId, instanceId, observationSpace, actionSpace, render); }
/** * Step the environment by one action * * @param action action to step the environment with * @return the StepReply containing the next observation, the reward, if it is a terminal state and optional information. */ public StepReply<O> step(A action) { JSONObject body = new JSONObject().put("action", getActionSpace().encode(action)).put("render", render); JSONObject reply = ClientUtils.post(url + ENVS_ROOT + instanceId + STEP, body).getObject(); O observation = observationSpace.getValue(reply, "observation"); double reward = reply.getDouble("reward"); boolean done = reply.getBoolean("done"); JSONObject info = reply.getJSONObject("info"); return new StepReply<O>(observation, reward, done, info); }
public static PlexSession parseJson(JsonNode node) { PlexSession plexSession = new PlexSession(); JSONObject mediaContainerJSON = node.getObject().getJSONObject("MediaContainer"); plexSession.setMediaContainer(MediaContainer.fromJson(mediaContainerJSON)); return plexSession; } }
/** * dumps a string to hastebin * * @param message the text to send * @return key how to find it */ private static String handleHastebin(String message) throws UnirestException { return Unirest.post("https://hastebin.com/documents").body(message).asJson().getBody().getObject().getString("key"); } }
private void login() throws IOException { HttpResponse<JsonNode> loginResult; try { loginResult = Unirest.post(serverUrl + "v1/login").field("username", user).field("password", password).asJson(); } catch (UnirestException e) { throw new IOException(e); } if (loginResult.getStatus() == 401) throw new IOException("The username and password provided are incorrect."); if (loginResult.getStatus() != 200) throw new IOException("The login failed with a result of: " + loginResult.getStatus()); JSONObject data = loginResult.getBody().getObject().getJSONObject("data"); this.authToken = data.getString("authToken"); this.userId = data.getString("userId"); }
@Override public Boolean call() throws Exception { try { Unirest.get(getServiceUrl().toString() + APPS_ENDPOINT).header(HEADER_ACCEPT, APPLICATION_JSON).asJson().getBody().getObject(); } catch (UnirestException e) { //NOSONAR // meaning API is not ready return false; } return true; } }
@Override public JSONObject getStateInfoJSON() throws UnirestException { String stateUrl = getStateUrl(); GetRequest request = Unirest.get(stateUrl); HttpResponse<JsonNode> response = request.asJson(); return response.getBody().getObject(); }
@Test public void shouldCountTheNumberOfImposters() throws UnirestException { JsonNode jsonNode = mock(JsonNode.class); JSONObject jsonObject = mock(JSONObject.class); JSONArray jsonArray = mock(JSONArray.class); when(Unirest.get(Client.DEFAULT_BASE_URL + "/imposters")).thenReturn(getRequest); when(getRequest.asJson()).thenReturn(httpResponse); when(httpResponse.getBody()).thenReturn(jsonNode); when(jsonNode.getObject()).thenReturn(jsonObject); when(jsonObject.get("imposters")).thenReturn(jsonArray); when(jsonArray.length()).thenReturn(Integer.valueOf(3)); assertThat(client.getImposterCount()).isEqualTo(3); }
public static UserProfilePhotos createUserProfilePhotos(long user_id, TelegramBot telegramBot) { try { JSONObject json = Unirest.post(telegramBot.getBotAPIUrl() + "getUserProfilePhotos") .queryString("user_id", user_id).asJson().getBody().getObject(); if(json.getBoolean("ok")) { return new UserProfilePhotosImpl(json.getJSONObject("result")); } } catch (UnirestException e) { e.printStackTrace(); } return null; }
@Override public String execute(DiscordBot bot, String[] args, MessageChannel channel, User author, Message inputMessage) { try { String tags = ""; if (args.length > 0) { tags = "&tag=" + Joiner.on("+").join(args); } HttpResponse<JsonNode> response = Unirest.get("http://api.giphy.com/v1/gifs/random?api_key=" + BotConfig.GIPHY_TOKEN + tags).asJson(); return response.getBody().getObject().getJSONObject("data").getString("url"); } catch (Exception ignored) { //this exception is about as useful as a nipple on a male } return Templates.command.gif_not_today.formatGuild(channel); } }
@Test public void assertThatMountebankAllowsInjection() throws UnirestException { JSONObject jsonObjectOptions = mock(JSONObject.class); JSONObject jsonObjectAllowInjection = mock(JSONObject.class); when(Unirest.get(Client.DEFAULT_BASE_URL + "/config")).thenReturn(getRequest); when(getRequest.asJson()).thenReturn(httpResponse); when(httpResponse.getBody()).thenReturn(value); when(value.getObject()).thenReturn(jsonObjectOptions); when(jsonObjectOptions.getJSONObject("options")).thenReturn(jsonObjectAllowInjection); when(jsonObjectAllowInjection.getBoolean("allowInjection")).thenReturn(Boolean.TRUE); if (!client.isMountebankAllowingInjection()) { fail("Mountebank is not running with --allowInjection!"); } }
static public JSONObject get(String url) { HttpResponse<JsonNode> jsonResponse = null; try { jsonResponse = Unirest.get(url).header("content-type", "application/json").asJson(); } catch (UnirestException e) { unirestCrash(e); } checkReply(jsonResponse, url); return jsonResponse.getBody().getObject(); }