diff --git a/examples/Example.java b/examples/Example.java index f457492..8320e81 100644 --- a/examples/Example.java +++ b/examples/Example.java @@ -13,16 +13,10 @@ 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"); @@ -33,8 +27,9 @@ 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\"]}"); @@ -52,18 +47,20 @@ public static void main(String[] args) throws IOException { } 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 { processResponse(); } 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 { @@ -71,26 +68,55 @@ public static void main(String[] args) throws IOException { } 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 { processResponse(); + } 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); } //Refactor method