import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.sendgrid.Client;
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.HashMap;
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
    request.setMethod(Method.GET);
    request.setEndpoint("/v3/api_keys");
    request.addQueryParam("limit", "100");
    request.addQueryParam("offset", "0");
    try {
      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.clearQueryParams();

    // POST
    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);
      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.setMethod(Method.GET);
    request.setEndpoint("/v3/api_keys/" + apiKeyId);
    try {
      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
    request.setMethod(Method.PATCH);
    request.setBody("{\"name\": \"A New Ho}");
    try {
      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.setMethod(Method.PUT);
    request.setBody("{\"name\": \"A New Hope\",\"scopes\": [\"user.profile.read\",\"user.profile.update\"]}");
    try {
      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.setMethod(Method.DELETE);
    try {
      response = client.api(request);
      System.out.println(response.getStatusCode());
      System.out.println(response.getHeaders());
    } catch (IOException ex) {
      throw ex;
    }
  }
}
@@ -885,184 +885,24 @@
     
-response = client.api(request);%0A      System.out.println(response.getStatusCode());%0A      System.out.println(response.getBody());%0A      System.out.println(response.getHeaders()
+processResponse(
 );%0A 
@@ -1209,184 +1209,24 @@
     
-response = client.api(request);%0A      System.out.println(response.getStatusCode());%0A      System.out.println(response.getBody());%0A      System.out.println(response.getHeaders()
+processResponse(
 );%0A 
@@ -1676,530 +1676,210 @@
     
-response = client.api(request);%0A      System.out.println(response.getStatusCode());%0A      System.out.println(response.getBody());%0A      System.out.println(response.getHeaders());%0A    %7D catch (IOException ex) %7B%0A      throw ex;%0A    %7D%0A%0A    // PATCH%0A    request.setMethod(Method.PATCH);%0A    request.setBody(%22%7B%5C%22name%5C%22: %5C%22A New Ho%7D%22);%0A    try %7B%0A      response = client.api(request);%0A      System.out.println(response.getStatusCode());%0A      System.out.println(response.getBody());%0A      System.out.println(response.getHeaders()
+processResponse();%0A    %7D catch (IOException ex) %7B%0A      throw ex;%0A    %7D%0A%0A    // PATCH%0A    request.setMethod(Method.PATCH);%0A    request.setBody(%22%7B%5C%22name%5C%22: %5C%22A New Ho%7D%22);%0A    try %7B%0A      processResponse(
 );%0A 
@@ -2133,217 +2133,26 @@
     
-response = client.api(request);%0A      System.out.println(response.getStatusCode());%0A      System.out.println(response.getBody());%0A      System.out.println(response.getHeaders());%0A    %7D catch (IOException ex) %7B
+processResponse();
 %0A   
@@ -2454,9 +2454,256 @@
   %7D%0A  %7D%0A
-%7D
+  %0A  //Refactor method%0A  private void processResponse()%7B%0A      response = client.api(request);%0A      System.out.println(response.getStatusCode());%0A      System.out.println(response.getBody());%0A      System.out.println(response.getHeaders());%0A  %7D%0A%7D%0A

Update Example.java