private String createAuthorizationUrl() throws IOException { Map<String, String> params = new HashMap<String, String>(); params.put("scope", SCOPE); params.put("redirect_uri", REDIRECT_URI); params.put("response_type", "code"); params.put("access_type", "offline"); params.put("client_id", CLIENT_ID); params.put("approval_prompt", "force"); String url = HttpUtil.appendQueryParams(OAUTH_URI + "/auth", params); return url; }
/** * Send a delete request * @param url Url as string * @return response Response as string * @throws IOException */ public static String delete(String url) throws IOException { return delete(url, null); }
/** * Send a delete request * @param url Url as string * @param headers Optional map with headers * @return response Response as string * @throws IOException */ public static String delete(String url, Map<String, String> headers) throws IOException { return fetch("DELETE", url, null, headers); }
for (int p = 0; p < pTemplate.length; p++) { String t = pTemplate[p]; if (isTemplateParam(t)) { String param = t.substring(1); String value = params.get(param); String url = join(pUrl, "/"); return url;
/** * Send a get request * @param url * @return response * @throws IOException */ public static String get(String url) throws IOException { return get(url, null); }
url = HttpUtil.appendQueryParams(url, params); String resp = HttpUtil.get(url, headers); ObjectMapper mapper = JOM.getInstance(); ObjectNode json = mapper.readValue(resp, ObjectNode.class);
/** * Send a post request * @param url Url as string * @param body Request body as string * @return response Response as string * @throws IOException */ public static String post(String url, String body) throws IOException { return post(url, body, null); }
/** * Post a form with parameters * @param url Url as string * @param params map with parameters/values * @return response Response as string * @throws IOException */ public static String postForm(String url, Map<String, String> params) throws IOException { return postForm(url, params, null); }
/** * Send a put request * @param url Url as string * @return response Response as string * @throws IOException */ public static String put(String url, String body) throws IOException { return put(url, body, null); }
String[] pTemplate = template.split("/"); String pLast = pTemplate[pTemplate.length - 1]; int limit = isTemplateParam(pLast) ? pTemplate.length : 0; String[] pUrl = url.split("/", limit); if (isTemplateParam(t)) { String param = t.substring(1); String value = 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; }
url = HttpUtil.appendQueryParams(url, params); String resp = HttpUtil.get(url, headers); ObjectMapper mapper = JOM.getInstance(); ObjectNode json = mapper.readValue(resp, ObjectNode.class);
return post(url, body, headers);
private ObjectNode exchangeCodeForAuthorization(String code) throws IOException { Map<String, String> params = new HashMap<String, String>(); params.put("code", code); params.put("client_id", CLIENT_ID); params.put("client_secret", CLIENT_SECRET); params.put("redirect_uri", REDIRECT_URI); params.put("grant_type", "authorization_code"); String res = HttpUtil.postForm(OAUTH_URI + "/token", params); ObjectNode json = mapper.readValue(res, ObjectNode.class); return json; }
Map<String, String> headers = getAuthorizationHeaders(); headers.put("Content-Type", "application/json"); String resp = HttpUtil.put(url, body, headers); ObjectNode updatedTask = mapper.readValue(resp, ObjectNode.class);
String response = HttpUtil.get(url);
private void sendAuthorizationToAgent(String agentUrl, String agentMethod, ObjectNode auth) throws IOException { JSONRequest rpcRequest = new JSONRequest(agentMethod, auth); HttpUtil.post(agentUrl, rpcRequest.toString()); }
/** * Send a put request * @param url Url as string * @param body Request body as string * @param headers Optional map with headers * @return response Response as string * @throws IOException */ public static String put(String url, String body, Map<String, String> headers) throws IOException { return fetch("PUT", url, body, headers); }
params.put("refresh_token", refresh_token); params.put("grant_type", "refresh_token"); String resp = HttpUtil.postForm(OAUTH_URI + "/token", params); ObjectNode json = JOM.getInstance().readValue(resp, ObjectNode.class); if (!json.has("access_token")) {
/** * Delete an existing task * @param taskId id of the task to be deleted * @param taksListId Optional task listid. the default task list is * used by default */ @Override public void deleteTask(@Name("taskId") String taskId, @Optional @Name("taskListId") String taskListId) throws Exception { if (taskListId == null) { taskListId = getDefaultTaskList(); } logger.info("deleteTask taskId=" + taskId + ", taskListId=" + taskListId); // TODO: cleanup // built url String url = CALENDAR_URI + "lists/" + taskListId + "/tasks/" + taskId; // perform POST request Map<String, String> headers = getAuthorizationHeaders(); String resp = HttpUtil.delete(url, headers); if (!resp.isEmpty()) { throw new Exception(resp); } }