Skip to content

Commit

Permalink
docs: fix the kitchen sink example and link to it in the use cases doc (
Browse files Browse the repository at this point in the history
#637)

The "Kitchen Sink" example and other mail-send examples were out-of-date. This change updates them to use the latest package version.
  • Loading branch information
childish-sambino authored Jul 15, 2020
1 parent d9f4413 commit c6e6361
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3,926 deletions.
5 changes: 2 additions & 3 deletions USE_CASES.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
This documentation provides examples for specific use cases. Please [open an issue](https://github.com/sendgrid/sendgrid-java/issues) or make a pull request for any use cases you would like us to document here. Thank you!

# Table of Contents
# Use Cases

* [Send Mail Examples](examples/helpers/mail/Example.java)
* [Transactional Templates](#transactional-templates)
* [Legacy Templates](#legacy-templates)
* [How to Setup a Domain Authentication](#domain-authentication)
* [How to View Email Statistics](#how-to-view-email-statistics)
* [Send an Email With Twilio Email (Pilot)](#send-an-email-with-twilio-email-pilot)
* [Send an SMS Message](#send-an-sms-message)

<a name="transactional-templates"></a>
# Transactional Templates

For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html) in the UI or via the API. Following is the template content we used for testing.
Expand Down Expand Up @@ -110,7 +110,6 @@ public class Example {
}
```

<a name="legacy-templates"></a>
# Legacy Templates

For this example, we assume you have created a [legacy template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing.
Expand Down
30 changes: 15 additions & 15 deletions examples/accesssettings/DeleteIPFromAccessSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
*/

public class DeleteIPFromAccessSettings extends Example {

private void run() throws IOException {
try {
String endPoint = "access_settings/whitelist";
String body = "{\"ids\":[1,2,3]}";
Request request = createRequest(Method.DELETE, endPoint, body);
Response response = execute(request);
printResponseInfo(response);
} catch (IOException ex) {
throw ex;
}
private void run() throws IOException {
try {
String endPoint = "access_settings/whitelist";
String body = "{\"ids\":[1,2,3]}";
Request request = createRequest(Method.DELETE, endPoint, body);
Response response = execute(request);
printResponseInfo(response);
} catch (IOException ex) {
throw ex;
}
}

public static void main(String[] args) throws IOException {
DeleteIPFromAccessSettings deleteIPFromAccessSettings = new DeleteIPFromAccessSettings();
deleteIPFromAccessSettings.run();
}
public static void main(String[] args) throws IOException {
DeleteIPFromAccessSettings deleteIPFromAccessSettings = new DeleteIPFromAccessSettings();
deleteIPFromAccessSettings.run();
}
}
102 changes: 29 additions & 73 deletions examples/helpers/mail/Example.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
import com.sendgrid.ASM;
import com.sendgrid.Attachments;
import com.sendgrid.BccSettings;
import com.sendgrid.ClickTrackingSetting;
import com.sendgrid.Client;
import com.sendgrid.Content;
import com.sendgrid.Email;
import com.sendgrid.FooterSetting;
import com.sendgrid.GoogleAnalyticsSetting;
import com.sendgrid.Mail;
import com.sendgrid.MailSettings;
import com.sendgrid.Method;
import com.sendgrid.OpenTrackingSetting;
import com.sendgrid.Personalization;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import com.sendgrid.Setting;
import com.sendgrid.SpamCheckSetting;
import com.sendgrid.SubscriptionTrackingSetting;
import com.sendgrid.TrackingSettings;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Example {

// Fully populated Mail object
public static Mail buildKitchenSink() throws IOException {
public static Mail buildKitchenSink() {
Mail mail = new Mail();

Email fromEmail = new Email();
Expand Down Expand Up @@ -143,7 +126,7 @@ public static Mail buildKitchenSink() throws IOException {

ASM asm = new ASM();
asm.setGroupId(99);
asm.setGroupsToDisplay(new int[] {4,5,6,7,8});
asm.setGroupsToDisplay(new int[]{4, 5, 6, 7, 8});
mail.setASM(asm);

// This must be a valid [batch ID](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) to work
Expand Down Expand Up @@ -208,7 +191,7 @@ public static Mail buildKitchenSink() throws IOException {
}

// API V3 Dynamic Template implementation
public static Mail buildDynamicTemplate() throws IOException {
public static Mail buildDynamicTemplate() {
Mail mail = new Mail();

Email fromEmail = new Email();
Expand All @@ -223,12 +206,12 @@ public static Mail buildDynamicTemplate() throws IOException {
personalization.addDynamicTemplateData("city", "Denver");
personalization.addTo(new Email("test@example.com"));
mail.addPersonalization(personalization);

return mail;
}

// Minimum required to send an email
public static Mail buildHelloEmail() throws IOException {
public static Mail buildHelloEmail() {
Email from = new Email("test@example.com");
String subject = "Hello World from the Twilio SendGrid Java Library";
Email to = new Email("test@example.com");
Expand All @@ -244,65 +227,38 @@ public static Mail buildHelloEmail() throws IOException {
}

public static void baselineExample() throws IOException {
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
sg.addRequestHeader("X-Mock", "true");

Request request = new Request();
Mail helloWorld = buildHelloEmail();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(helloWorld.build());
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
final Mail helloWorld = buildHelloEmail();
send(helloWorld);
}

public static void kitchenSinkExample() throws IOException {
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
sg.addRequestHeader("X-Mock", "true");

Request request = new Request();
Mail kitchenSink = buildKitchenSink();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(kitchenSink.build());
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
final Mail kitchenSink = buildKitchenSink();
send(kitchenSink);
}

public static void dynamicTemplateExample() throws IOException {
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
final Mail dynamicTemplate = buildDynamicTemplate();
send(dynamicTemplate);
}

private static void send(final Mail mail) throws IOException {
final SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
sg.addRequestHeader("X-Mock", "true");

Request request = new Request();
Mail dynamicTemplate = buildDynamicTemplate();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(dynamicTemplate.build());
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
final Request request = new Request();
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());

final Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
}

public static void main(String[] args) throws IOException {
// baselineExample();
// kitchenSinkExample();
baselineExample();
kitchenSinkExample();
dynamicTemplateExample();
}
}
}
3 changes: 2 additions & 1 deletion examples/ips/RetrieveAllPools.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ public static void main(String[] args) throws IOException {
} catch (IOException ex) {
throw ex;
}
}
}
}
Loading

0 comments on commit c6e6361

Please sign in to comment.