Skip to content
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
37 changes: 29 additions & 8 deletions src/main/java/com/github/sendgrid/SendGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.github.kevinsawicki.http.HttpRequest;
import org.json.JSONObject;
import org.json.JSONException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;

public class SendGrid {
Expand Down Expand Up @@ -32,7 +35,7 @@ public class SendGrid {
private String subject;
private String text;
private String html;
private ArrayList<File> files = new ArrayList<File>();
private ArrayList<Attachment> attachments = new ArrayList<Attachment>();
private JSONObject headers = new JSONObject();

public SendGrid(String username, String password) {
Expand Down Expand Up @@ -80,8 +83,8 @@ public String web() {
if (this.getBcc() != null) {
request.part(PARAM_BCC, this.getBcc());
}
for (File file:this.getFiles()) {
request.part(String.format(PARAM_FILES, file.getName()), file);
for (Attachment attachment:this.getAttachments()) {
request.part(String.format(PARAM_FILES, attachment.name), attachment.contents);
}
request.part(PARAM_HEADERS, this.getHeaders().toString());

Expand Down Expand Up @@ -124,8 +127,8 @@ public String getHtml() {
return this.html;
}

public ArrayList<File> getFiles() {
return this.files;
public ArrayList<Attachment> getAttachments() {
return this.attachments;
}

public JSONObject getHeaders() {
Expand Down Expand Up @@ -177,8 +180,8 @@ public SendGrid setHtml(String html) {
return this;
}

public SendGrid addFile(File file) {
this.files.add(file);
public SendGrid addAttachment(Attachment attachment) {
this.attachments.add(attachment);
return this;
}

Expand All @@ -191,4 +194,22 @@ public SendGrid addHeader(String key, String value) {

return this;
}

public static class Attachment
{
public final String name;
public final InputStream contents;

public Attachment(File file) throws FileNotFoundException
{
this.name = file.getName();
this.contents = new FileInputStream(file);
}

public Attachment(String name, InputStream contents)
{
this.name = name;
this.contents = contents;
}
}
}
25 changes: 14 additions & 11 deletions src/test/java/com/github/sendgrid/SendGridTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.github.sendgrid;

import org.json.JSONException;
import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.hasItems;
import org.json.JSONObject;
import org.json.JSONException;

public class SendGridTest {
private static final String USERNAME = "username";
Expand Down Expand Up @@ -121,26 +123,27 @@ public void testSetHtml() {
}

@Test
public void testAddFile() {
public void testAddFile() throws FileNotFoundException {
SendGrid sendgrid = new SendGrid(USERNAME, PASSWORD);

File file = new File(getClass().getResource("/test.txt").getFile());
sendgrid.addFile(file);
SendGrid.Attachment attachment = new SendGrid.Attachment(file);
sendgrid.addAttachment(attachment);

assertThat(sendgrid.getFiles(), hasItems(file));
assertThat(sendgrid.getAttachments(), hasItems(attachment));
}

@Test
public void testAddMultipleFiles() {
public void testAddMultipleFiles() throws FileNotFoundException {
SendGrid sendgrid = new SendGrid(USERNAME, PASSWORD);

File file = new File(getClass().getResource("/test.txt").getFile());
File file2 = new File(getClass().getResource("/image.png").getFile());
SendGrid.Attachment attachment1 = new SendGrid.Attachment(new File(getClass().getResource("/test.txt").getFile()));
SendGrid.Attachment attachment2 = new SendGrid.Attachment(new File(getClass().getResource("/image.png").getFile()));

sendgrid.addFile(file);
sendgrid.addFile(file2);
sendgrid.addAttachment(attachment1);
sendgrid.addAttachment(attachment2);

assertThat(sendgrid.getFiles(), hasItems(file, file2));
assertThat(sendgrid.getAttachments(), hasItems(attachment1, attachment2));
}

@Test
Expand Down