/** * Send a get request * @param url * @return response * @throws IOException */ public static String get(String url) throws IOException { return get(url, null); }
/** * Evaluate given expression * For example expr="2.5 + 3 / sqrt(16)" will return "3.25" * @param expr * @return result * @throws Exception */ public String eval(@Name("expr") String expr) throws Exception { String url = CALC_API_URL + "?q=" + URLEncoder.encode(expr, "UTF-8"); String resp = HttpUtil.get(url); // the field names in resp are not enclosed by quotes :( resp = resp.replaceAll("lhs:", "\"lhs\":"); resp = resp.replaceAll("rhs:", "\"rhs\":"); resp = resp.replaceAll("error:", "\"error\":"); resp = resp.replaceAll("icc:", "\"icc\":"); ObjectMapper mapper = JOM.getInstance(); ObjectNode json = mapper.readValue(resp, ObjectNode.class); String error = json.get("error").asText(); if (error != null && !error.isEmpty()) { throw new Exception(error); } String rhs = json.get("rhs").asText(); return rhs; }
String response = HttpUtil.get(url);
/** * Translate text * @param text The text to be translated * @param target The target language code, for example "en" * @param source The source language code, for example "nl". Optional * @return * @throws Exception */ public String translate(@Name("text") String text, @Name("target") String target, @Name("source") @Optional String source) throws Exception { String key = getState().get("key",String.class); if (key == null) { throw new Exception("No valid API Key set. " + "Google Translate API is a paid service " + "and requires an API Key for billing."); } String url = TRANSLATE_API_URL + "?q=" + URLEncoder.encode(text, "UTF-8") + "&key=" + URLEncoder.encode(key, "UTF-8") + "&target=" + URLEncoder.encode(target, "UTF-8"); if (source != null) { url += "&source=" + URLEncoder.encode(source, "UTF-8"); } String resp = HttpUtil.get(url); return resp; }
/** * Retrieve a list with all calendars in this google calendar */ @Override public ArrayNode getCalendarList() throws Exception { String url = CALENDAR_URI + "users/me/calendarList"; String resp = HttpUtil.get(url, getAuthorizationHeaders()); ObjectNode calendars = JOM.getInstance().readValue(resp, ObjectNode.class); // check for errors if (calendars.has("error")) { ObjectNode error = (ObjectNode) calendars.get("error"); throw new JSONRPCException(error); } // get items from response ArrayNode items = null; if (calendars.has("items")) { items = (ArrayNode) calendars.get("items"); } else { items = JOM.createArrayNode(); } return items; }
/** * Retrieve a list with all task lists in this google tasks */ @Override public ArrayNode getTaskList() throws Exception { String url = CALENDAR_URI + "users/@me/lists"; String resp = HttpUtil.get(url, getAuthorizationHeaders()); ObjectNode calendars = JOM.getInstance().readValue(resp, ObjectNode.class); // check for errors if (calendars.has("error")) { ObjectNode error = (ObjectNode)calendars.get("error"); throw new JSONRPCException(error); } // get items from response ArrayNode items = null; if (calendars.has("items")) { items = (ArrayNode)calendars.get("items"); } else { items = JOM.createArrayNode(); } return items; }
Map<String, String> headers = new HashMap<String, String>(); headers.put("Authorization", token_type + " " + access_token); String resp = HttpUtil.get(url, headers);
String resp = HttpUtil.get(url, headers); ObjectMapper mapper = JOM.getInstance(); ObjectNode task = mapper.readValue(resp, ObjectNode.class);
/** * Set access token and refresh token, used to authorize the calendar agent. * These tokens must be retrieved via Oauth 2.0 authorization. * * @param access_token * @param token_type * @param expires_in * @param refresh_token * @throws IOException */ public void setAuthorization(@Name("access_token") String access_token, @Name("token_type") String token_type, @Name("expires_in") Integer expires_in, @Name("refresh_token") String refresh_token) throws IOException { State state = getState(); // retrieve user information String url = "https://www.googleapis.com/oauth2/v1/userinfo"; Map<String, String> headers = new HashMap<String, String>(); headers.put("Authorization", token_type + " " + access_token); String resp = HttpUtil.get(url, headers); ObjectNode info = JOM.getInstance().readValue(resp, ObjectNode.class); String email = info.has("email") ? info.get("email").asText() : null; String name = info.has("name") ? info.get("name").asText() : null; DateTime expires_at = calculateExpiresAt(expires_in); Authorization auth = new Authorization(access_token, token_type, expires_at, refresh_token); // store the tokens in the state state.put("auth", auth); state.put("email", email); state.put("name", name); }
String resp = HttpUtil.get(url, headers); ObjectMapper mapper = JOM.getInstance(); ObjectNode event = mapper.readValue(resp, ObjectNode.class);
String resp = HttpUtil.get(url, headers); ObjectMapper mapper = JOM.getInstance(); ObjectNode json = mapper.readValue(resp, ObjectNode.class);
String resp = HttpUtil.get(url, headers); ObjectMapper mapper = JOM.getInstance(); ObjectNode json = mapper.readValue(resp, ObjectNode.class);