Skip to content

Commit 903f77f

Browse files
committed
Merge branch 'apikeys'
2 parents 293d6ce + e1dbf68 commit 903f77f

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]";
@@ -53,6 +53,12 @@ public class SendGrid {
5353
private String endpoint;
5454
private CloseableHttpClient client;
5555

56+
/**
57+
* Constructor for using a username and password
58+
*
59+
* @param username
60+
* @param password
61+
*/
5662
public SendGrid(String username, String password) {
5763
this.username = username;
5864
this.password = password;
@@ -61,6 +67,19 @@ public SendGrid(String username, String password) {
6167
this.client = HttpClientBuilder.create().setUserAgent(USER_AGENT).build();
6268
}
6369

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

86-
builder.addTextBody("api_user", this.username);
87-
builder.addTextBody("api_key", this.password);
105+
// We are using an API key
106+
if (this.username != null) {
107+
builder.addTextBody("api_user", this.username);
108+
builder.addTextBody("api_key", this.password);
109+
}
88110

89111
String[] tos = email.getTos();
90112
String[] tonames = email.getToNames();
@@ -152,6 +174,11 @@ public SendGrid.Response send(Email email) throws SendGridException {
152174
HttpPost httppost = new HttpPost(this.url + this.endpoint);
153175
httppost.setEntity(this.buildBody(email));
154176

177+
// Using an API key
178+
if (this.username == null) {
179+
httppost.setHeader("Authorization", "Bearer " + this.password);
180+
}
181+
155182
try {
156183
HttpResponse res = this.client.execute(httppost);
157184
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

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

2564
@Test

0 commit comments

Comments
 (0)