Skip to content

Update the request object with sensible defaults and access methods, #13 #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ Here is a quick example:
Client client = new Client();

Request request = new Request();
request.baseUri = "api.test.com";
request.method = Method.GET;
request.setBaseUri("api.test.com");
request.setMethod(Method.GET);
String param = "param";
request.endpoint = "/your/api/" + param + "/call";
request.setEndpoint("/your/api/" + param + "/call");

try {
Response response = client.api(request);
Expand All @@ -77,17 +77,13 @@ try {
`POST /your/api/{param}/call` with headers, query parameters and a request body.

```java
Map<String,String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Authorization", "Bearer YOUR_API_KEY");
request.headers = requestHeaders;
Map<String,String> queryParams = new HashMap<String, String>();
queryParams.put("limit", "100");
queryParams.put("offset", "0");
request.queryParams = queryParams;
request.body ="{\"name\": \"My Request Body\"}";
request.method = Method.POST;
request.addHeader("Authorization", "Bearer YOUR_API_KEY");
request.addQueryParam("limit", "100");
request.addQueryParam("offset", "0");
request.setBody("{\"name\": \"My Request Body\"}");
request.setMethod(Method.POST);
String param = "param";
request.endpoint = "/your/api/" + param + "/call";
request.setEndpoint("/your/api/" + param + "/call");

try {
Response response = client.api(request);
Expand Down
47 changes: 21 additions & 26 deletions examples/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@ public static void main(String[] args) throws IOException {
Client client = new Client();

Request request = new Request();
request.baseUri = "api.sendgrid.com";
Map<String,String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY"));
request.headers = requestHeaders;
request.setBaseUri("api.sendgrid.com");
request.addHeader("Authorization", "Bearer " + System.getenv("SENDGRID_API_KEY"));

Response response = new Response();

// GET Collection
request.method = Method.GET;
request.endpoint = "/v3/api_keys";
Map<String,String> queryParams = new HashMap<String, String>();
queryParams.put("limit", "100");
queryParams.put("offset", "0");
request.queryParams = queryParams;
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.statusCode);
Expand All @@ -39,13 +35,13 @@ public static void main(String[] args) throws IOException {
} catch (IOException ex) {
throw ex;
}
request.queryParams = null;
request.clearQueryParams();

// POST
request.method = Method.POST;
request.endpoint = "/v3/api_keys";
request.body =
"{\"name\": \"My api Key\",\"scopes\": [\"mail.send\",\"alerts.create\",\"alerts.read\"]}";
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.statusCode);
Expand All @@ -62,11 +58,11 @@ public static void main(String[] args) throws IOException {
} catch (IOException ex) {
throw ex;
}
request.body = "";
request.clearBody();

// GET Single
request.method = Method.GET;
request.endpoint = "/v3/api_keys/" + apiKeyId;
request.setMethod(Method.GET);
request.setEndpoint("/v3/api_keys/" + apiKeyId);
try {
response = client.api(request);
System.out.println(response.statusCode);
Expand All @@ -77,8 +73,8 @@ public static void main(String[] args) throws IOException {
}

// PATCH
request.method = Method.PATCH;
request.body = "{\"name\": \"A New Hope\"}";
request.setMethod(Method.PATCH);
request.setBody("{\"name\": \"A New Ho}");
try {
response = client.api(request);
System.out.println(response.statusCode);
Expand All @@ -87,12 +83,11 @@ public static void main(String[] args) throws IOException {
} catch (IOException ex) {
throw ex;
}
request.body = "";
request.clearBody();

// PUT
request.method = Method.PUT;
request.body =
"{\"name\": \"A New Hope\",\"scopes\": [\"user.profile.read\",\"user.profile.update\"]}";
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.statusCode);
Expand All @@ -101,10 +96,10 @@ public static void main(String[] args) throws IOException {
} catch (IOException ex) {
throw ex;
}
request.body = "";
request.clearBody();

// DELETE
request.method = Method.DELETE;
request.setMethod(Method.DELETE);
try {
response = client.api(request);
System.out.println(response.statusCode);
Expand Down
52 changes: 26 additions & 26 deletions src/main/java/com/sendgrid/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ public Response get(Request request) throws URISyntaxException, IOException {
HttpGet httpGet = null;

try {
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
httpGet = new HttpGet(uri.toString());
} catch (URISyntaxException ex) {
throw ex;
}

if (request.headers != null) {
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
if (request.getHeaders() != null) {
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
Expand All @@ -171,20 +171,20 @@ public Response post(Request request) throws URISyntaxException, IOException {
HttpPost httpPost = null;

try {
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
httpPost = new HttpPost(uri.toString());
} catch (URISyntaxException ex) {
throw ex;
}

if (request.headers != null) {
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
if (request.getHeaders() != null) {
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}

httpPost.setEntity(new StringEntity(request.body, Charset.forName("UTF-8")));
if (request.body != "") {
httpPost.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8")));
if (request.getBody() != "") {
httpPost.setHeader("Content-Type", "application/json");
}

Expand All @@ -200,20 +200,20 @@ public Response patch(Request request) throws URISyntaxException, IOException {
HttpPatch httpPatch = null;

try {
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
httpPatch = new HttpPatch(uri.toString());
} catch (URISyntaxException ex) {
throw ex;
}

if (request.headers != null) {
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
if (request.getHeaders() != null) {
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
httpPatch.setHeader(entry.getKey(), entry.getValue());
}
}

httpPatch.setEntity(new StringEntity(request.body, Charset.forName("UTF-8")));
if (request.body != "") {
httpPatch.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8")));
if (request.getBody() != "") {
httpPatch.setHeader("Content-Type", "application/json");
}
return executeApiCall(httpPatch);
Expand All @@ -228,20 +228,20 @@ public Response put(Request request) throws URISyntaxException, IOException {
HttpPut httpPut = null;

try {
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
httpPut = new HttpPut(uri.toString());
} catch (URISyntaxException ex) {
throw ex;
}

if (request.headers != null) {
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
if (request.getHeaders() != null) {
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
httpPut.setHeader(entry.getKey(), entry.getValue());
}
}

httpPut.setEntity(new StringEntity(request.body, Charset.forName("UTF-8")));
if (request.body != "") {
httpPut.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8")));
if (request.getBody() != "") {
httpPut.setHeader("Content-Type", "application/json");
}

Expand All @@ -256,20 +256,20 @@ public Response delete(Request request) throws URISyntaxException, IOException {
HttpDeleteWithBody httpDelete = null;

try {
uri = buildUri(request.baseUri, request.endpoint, request.queryParams);
uri = buildUri(request.getBaseUri(), request.getEndpoint(), request.getQueryParams());
httpDelete = new HttpDeleteWithBody(uri.toString());
} catch (URISyntaxException ex) {
throw ex;
}

if (request.headers != null) {
for (Map.Entry<String, String> entry : request.headers.entrySet()) {
if (request.getHeaders() != null) {
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
httpDelete.setHeader(entry.getKey(), entry.getValue());
}
}

httpDelete.setEntity(new StringEntity(request.body, Charset.forName("UTF-8")));
if (request.body != "") {
httpDelete.setEntity(new StringEntity(request.getBody(), Charset.forName("UTF-8")));
if (request.getBody() != "") {
httpDelete.setHeader("Content-Type", "application/json");
}

Expand All @@ -287,7 +287,7 @@ private Response executeApiCall(HttpRequestBase httpPost) throws IOException {
//throwing IOException here to not break API behavior.
throw new IOException("Request returned status Code "+statusLine.getStatusCode()+"Body:"+(response!=null?response.body:null));
}

} finally {
if (serverResponse != null) {
serverResponse.close();
Expand All @@ -301,10 +301,10 @@ private Response executeApiCall(HttpRequestBase httpPost) throws IOException {
*/
public Response api(Request request) throws IOException {
try {
if (request.method == null) {
if (request.getMethod() == null) {
throw new IOException("We only support GET, PUT, PATCH, POST and DELETE.");
}
switch (request.method) {
switch (request.getMethod()) {
case GET:
return get(request);
case POST:
Expand Down
Loading