/** * Construct a new SendGrid API wrapper. * @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys * @param test is true if you are unit testing */ public SendGrid(String apiKey, Boolean test) { this.client = new Client(test); initializeSendGrid(apiKey); }
/** * Makes the call to the SendGrid API, override this method for testing. * @param request the request to make. * @return the response object. * @throws IOException in case of a network error. */ public Response makeCall(Request request) throws IOException { return client.api(request); }
/** * A thin wrapper around the HTTP methods. */ public Response api(Request request) throws IOException { try { if (request.getMethod() == null) { throw new IOException("We only support GET, PUT, PATCH, POST and DELETE."); } switch (request.getMethod()) { case GET: return get(request); case POST: return post(request); case PUT: return put(request); case PATCH: return patch(request); case DELETE: return delete(request); default: throw new IOException("We only support GET, PUT, PATCH, POST and DELETE."); } } catch (IOException ex) { throw ex; } catch (URISyntaxException ex) { StringWriter errors = new StringWriter(); ex.printStackTrace(new PrintWriter(errors)); throw new IOException(errors.toString()); } }
/** * Make a GET request and provide the status code, response body and * response headers. */ public Response get(Request request) throws URISyntaxException, IOException { URI uri = null; HttpGet httpGet = null; try { uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); httpGet = new HttpGet(uri.toString()); } catch (URISyntaxException ex) { throw ex; } if (request.getHeaders() != null) { for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) { httpGet.setHeader(entry.getKey(), entry.getValue()); } } return executeApiCall(httpGet); }
private Response executeApiCall(HttpRequestBase httpPost) throws IOException { try { CloseableHttpResponse serverResponse = httpClient.execute(httpPost); try { Response response = getResponse(serverResponse); if(response.getStatusCode() >= 300) { //throwing IOException here to not break API behavior. throw new IOException("Request returned status Code "+response.getStatusCode()+"Body:"+response.getBody()); } return response; } finally { serverResponse.close(); } } catch(ClientProtocolException e) { throw new IOException(e.getMessage()); } }
/** * Make a POST request and provide the status code, response body and * response headers. */ public Response post(Request request) throws URISyntaxException, IOException { URI uri = null; HttpPost httpPost = null; try { uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); httpPost = new HttpPost(uri.toString()); } catch (URISyntaxException ex) { throw ex; } if (request.getHeaders() != null) { for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) { httpPost.setHeader(entry.getKey(), entry.getValue()); } } httpPost.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8"))); if (request.getBody() != "") { httpPost.setHeader("Content-Type", "application/json"); } return executeApiCall(httpPost); }
/** * Make a PUT request and provide the status code, response body and * response headers. */ public Response put(Request request) throws URISyntaxException, IOException { URI uri = null; HttpPut httpPut = null; try { uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); httpPut = new HttpPut(uri.toString()); } catch (URISyntaxException ex) { throw ex; } if (request.getHeaders() != null) { for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) { httpPut.setHeader(entry.getKey(), entry.getValue()); } } httpPut.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8"))); if (request.getBody() != "") { httpPut.setHeader("Content-Type", "application/json"); } return executeApiCall(httpPut); }
/** * Construct a new SendGrid API wrapper. * @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys */ public SendGrid(String apiKey) { this.client = new Client(); initializeSendGrid(apiKey); }
/** * Makes the call to the SendGrid API, override this method for testing. * @param request the request to make. * @return the response object. * @throws IOException in case of a network error. */ public Response makeCall(Request request) throws IOException { return client.api(request); }
/** * Make a PATCH request and provide the status code, response body and * response headers. */ public Response patch(Request request) throws URISyntaxException, IOException { URI uri = null; HttpPatch httpPatch = null; try { uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); httpPatch = new HttpPatch(uri.toString()); } catch (URISyntaxException ex) { throw ex; } if (request.getHeaders() != null) { for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) { httpPatch.setHeader(entry.getKey(), entry.getValue()); } } httpPatch.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8"))); if (request.getBody() != "") { httpPatch.setHeader("Content-Type", "application/json"); } return executeApiCall(httpPatch); }
/** * Construct a new SendGrid API wrapper. * @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys * @param test is true if you are unit testing */ public SendGrid(String apiKey, Boolean test) { this.client = new Client(test); initializeSendGrid(apiKey); }
/** * Make a DELETE request and provide the status code and response headers. */ public Response delete(Request request) throws URISyntaxException, IOException { URI uri = null; HttpDeleteWithBody httpDelete = null; try { uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams()); httpDelete = new HttpDeleteWithBody(uri.toString()); } catch (URISyntaxException ex) { throw ex; } if (request.getHeaders() != null) { for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) { httpDelete.setHeader(entry.getKey(), entry.getValue()); } } httpDelete.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8"))); if (request.getBody() != "") { httpDelete.setHeader("Content-Type", "application/json"); } return executeApiCall(httpDelete); }
/** * Construct a new SendGrid API wrapper. * @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys */ public SendGrid(String apiKey) { this.client = new Client(); initializeSendGrid(apiKey); }