From 4600a389a3177ac4cf85af3db8a2281abd85e02e Mon Sep 17 00:00:00 2001 From: huytranrjc Date: Mon, 30 Oct 2017 17:24:44 +0900 Subject: [PATCH] Fix Issue #71 --- examples/Example.java | 94 ++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 37 deletions(-) diff --git a/examples/Example.java b/examples/Example.java index a986d89..8f2eb4f 100644 --- a/examples/Example.java +++ b/examples/Example.java @@ -13,22 +13,16 @@ import java.util.Map; public class Example { - public static void main(String[] args) throws IOException { - Client client = new Client(); - - Request request = new Request(); - request.setBaseUri("api.sendgrid.com"); - request.addHeader("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY")); - - Response response = new Response(); - - // GET Collection + + private static String apiKeyId = ""; + + private static void getCollection(Client client, Request request) throws IOException { request.setMethod(Method.GET); request.setEndpoint("/v3/api_keys"); request.addQueryParam("limit", "100"); request.addQueryParam("offset", "0"); try { - response = client.api(request); + Response response = client.api(request); System.out.println(response.getStatusCode()); System.out.println(response.getBody()); System.out.println(response.getHeaders()); @@ -36,76 +30,102 @@ public static void main(String[] args) throws IOException { throw ex; } request.clearQueryParams(); - - // POST + } + + private static void post(Client client, Request request) throws IOException { request.setMethod(Method.POST); request.setEndpoint("/v3/api_keys"); request.setBody("{\"name\": \"My api Key\",\"scopes\": [\"mail.send\",\"alerts.create\",\"alerts.read\"]}"); try { - response = client.api(request); + Response response = client.api(request); System.out.println(response.getStatusCode()); System.out.println(response.getBody()); System.out.println(response.getHeaders()); - } catch (IOException ex) { - throw ex; - } - String apiKeyId = ""; - try { ObjectMapper mapper = new ObjectMapper(); JsonNode json = mapper.readTree(response.getBody()); apiKeyId = json.path("api_key_id").asText(); } catch (IOException ex) { throw ex; } - request.clearBody(); - - // GET Single + request.clearBody(); + } + + private static void getSingle(Client client, Request request) throws IOException { request.setMethod(Method.GET); request.setEndpoint("/v3/api_keys/" + apiKeyId); try { - response = client.api(request); + Response response = client.api(request); System.out.println(response.getStatusCode()); System.out.println(response.getBody()); System.out.println(response.getHeaders()); } catch (IOException ex) { throw ex; - } - - // PATCH + } + } + + private static void patch(Client client, Request request) throws IOException { request.setMethod(Method.PATCH); request.setBody("{\"name\": \"A New Ho}"); try { - response = client.api(request); + Response response = client.api(request); System.out.println(response.getStatusCode()); System.out.println(response.getBody()); System.out.println(response.getHeaders()); } catch (IOException ex) { throw ex; } - request.clearBody(); - - // PUT + request.clearBody(); + } + + private static void put(Client client, Request request) throws IOException { request.setMethod(Method.PUT); request.setBody("{\"name\": \"A New Hope\",\"scopes\": [\"user.profile.read\",\"user.profile.update\"]}"); try { - response = client.api(request); + Response response = client.api(request); System.out.println(response.getStatusCode()); System.out.println(response.getBody()); System.out.println(response.getHeaders()); } catch (IOException ex) { throw ex; } - request.clearBody(); - - // DELETE + request.clearBody(); + } + + private static void delete(Client client, Request request) throws IOException { request.setMethod(Method.DELETE); try { - response = client.api(request); + Response response = client.api(request); System.out.println(response.getStatusCode()); System.out.println(response.getHeaders()); } catch (IOException ex) { throw ex; - } + } + } + + public static void main(String[] args) throws IOException { + Client client = new Client(); + + Request request = new Request(); + request.setBaseUri("api.sendgrid.com"); + request.addHeader("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY")); + + // GET Collection + getCollection(client, request); + + // POST + post(client, request); + + // GET Single + getSingle(client, request); + + // PATCH + patch(client, request); + + // PUT + put(client, request); + + // DELETE + delete(client, request); } -} \ No newline at end of file +}