Skip to content

Commit e1dbf68

Browse files
committed
Add support for API keys
1 parent 517a99f commit e1dbf68

File tree

4 files changed

+76
-8
lines changed

4 files changed

+76
-8
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class SendGridExample {
3535
Compile and run this example with
3636

3737
```bash
38-
$ javac -classpath sendgrid-1.2.1-jar.jar:. SendGridExample.java && java -classpath sendgrid-1.2.1-jar.jar:. SendGridExample
38+
$ javac -classpath sendgrid-2.2.0-jar.jar:. SendGridExample.java && java -classpath sendgrid-2.2.0-jar.jar:. SendGridExample
3939
```
4040

4141
## Installation
@@ -50,7 +50,7 @@ Add the following to your build.gradle file in the root of your project.
5050
...
5151
dependencies {
5252
...
53-
compile 'com.sendgrid:sendgrid-java:2.0.0'
53+
compile 'com.sendgrid:sendgrid-java:2.2.0'
5454
}
5555
5656
repositories {
@@ -83,11 +83,13 @@ import com.sendgrid.*;
8383

8484
## Usage
8585

86-
To begin using this library, initialize the SendGrid object with your SendGrid credentials.
86+
To begin using this library, initialize the SendGrid object with your SendGrid credentials OR a SendGrid API Key. API Key is the preferred method. API Keys are in beta. To configure API keys, visit https://sendgrid.com/beta/settings/api_keys.
8787

8888
```java
8989
import com.sendgrid.SendGrid;
9090
SendGrid sendgrid = new SendGrid("sendgrid_username", "sendgrid_password");
91+
// or
92+
SendGrid sendgrid = new SendGrid("sendgrid_api_key");
9193
```
9294

9395
Add your message details.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ apply plugin: 'maven'
1717
apply plugin: 'signing'
1818

1919
group = 'com.sendgrid'
20-
version = '2.1.1'
20+
version = '2.2.0'
2121
ext.packaging = 'jar'
2222

2323
allprojects {

src/main/java/com/sendgrid/SendGrid.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.apache.http.entity.ContentType;
2727

2828
public class SendGrid {
29-
private static final String VERSION = "2.1.1";
29+
private static final String VERSION = "2.2.0";
3030
private static final String USER_AGENT = "sendgrid/" + VERSION + ";java";
3131

3232
private static final String PARAM_TO = "to[%d]";
@@ -51,6 +51,12 @@ public class SendGrid {
5151
private String endpoint;
5252
private CloseableHttpClient client;
5353

54+
/**
55+
* Constructor for using a username and password
56+
*
57+
* @param username
58+
* @param password
59+
*/
5460
public SendGrid(String username, String password) {
5561
this.username = username;
5662
this.password = password;
@@ -59,6 +65,19 @@ public SendGrid(String username, String password) {
5965
this.client = HttpClientBuilder.create().setUserAgent(USER_AGENT).build();
6066
}
6167

68+
/**
69+
* Constructor for using an API key
70+
*
71+
* @param apiKey
72+
*/
73+
public SendGrid(String apiKey) {
74+
this.password = apiKey;
75+
this.username = null;
76+
this.url = "https://api.sendgrid.com";
77+
this.endpoint = "/api/mail.send.json";
78+
this.client = HttpClientBuilder.create().setUserAgent(USER_AGENT).build();
79+
}
80+
6281
public SendGrid setUrl(String url) {
6382
this.url = url;
6483
return this;
@@ -81,8 +100,11 @@ public SendGrid setClient(CloseableHttpClient client) {
81100
public HttpEntity buildBody(Email email) {
82101
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
83102

84-
builder.addTextBody("api_user", this.username);
85-
builder.addTextBody("api_key", this.password);
103+
// We are using an API key
104+
if (this.username != null) {
105+
builder.addTextBody("api_user", this.username);
106+
builder.addTextBody("api_key", this.password);
107+
}
86108

87109
String[] tos = email.getTos();
88110
String[] tonames = email.getToNames();
@@ -150,6 +172,11 @@ public SendGrid.Response send(Email email) throws SendGridException {
150172
HttpPost httppost = new HttpPost(this.url + this.endpoint);
151173
httppost.setEntity(this.buildBody(email));
152174

175+
// Using an API key
176+
if (this.username == null) {
177+
httppost.setHeader("Authorization", "Bearer " + this.password);
178+
}
179+
153180
try {
154181
HttpResponse res = this.client.execute(httppost);
155182
return new SendGrid.Response(res.getStatusLine().getStatusCode(), EntityUtils.toString(res.getEntity()));

src/test/java/com/sendgrid/SendGridTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.sendgrid;
22

3+
import java.io.BufferedReader;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
import java.io.IOException;
37
import java.util.HashMap;
48
import java.util.Map;
59

@@ -16,7 +20,42 @@ public class SendGridTest {
1620
@Test
1721
public void testVersion() {
1822
SendGrid client = new SendGrid(USERNAME, PASSWORD);
19-
assertEquals(client.getVersion(), "2.1.1");
23+
assertEquals(client.getVersion(), "2.2.0");
24+
}
25+
26+
@Test
27+
public void testBuildGradleVersion() {
28+
try {
29+
SendGrid client = new SendGrid(USERNAME, PASSWORD);
30+
BufferedReader br = new BufferedReader(new FileReader("./build.gradle"));
31+
String line = br.readLine();
32+
String regex = "version\\s*=\\s*'" + client.getVersion() + "'";
33+
34+
while (line != null) {
35+
if (line.matches(regex)) {
36+
br.close();
37+
return;
38+
}
39+
line = br.readLine();
40+
}
41+
br.close();
42+
fail("build.gradle version does not match");
43+
} catch (FileNotFoundException e) {
44+
e.printStackTrace();
45+
} catch (IOException e) {
46+
e.printStackTrace();
47+
}
48+
49+
}
50+
51+
@Test
52+
public void testUsernamePasswordConstructor() {
53+
SendGrid client = new SendGrid(USERNAME, PASSWORD);
54+
}
55+
56+
@Test
57+
public void testApiKeyConstructor() {
58+
SendGrid client = new SendGrid(PASSWORD);
2059
}
2160

2261
@Test

0 commit comments

Comments
 (0)