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
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ class Example {
To build the email with the simple editor, set `editor` to `EmailEditors.SIMPLE`, and do not set `content`. Then change the email with [the email methods](#emails). The ID of the email is in `campaign.emails[0].id`.

- `editor`: the only accepted value is `simple`, which selects the simple editor instead of the drag-and-drop builder. It has to be the same across every email in the payload; a mixed array is rejected. Setting it prohibits `emails.*.content`.
- `templateId` (`template_id`): the template the new email starts from. Allowed only when `editor` is `simple`, accepts only a template from the shared gallery, and also has to be the same across every email.
- `templateId` (`template_id`): the simple editor template the new email starts from. It is a template from the shared gallery, or a template that you saved from your own email with [`createTemplate`](#save-an-email-as-a-template). Allowed only when `editor` is `simple`, and also has to be the same across every email.
- Only `create()` accepts `editor` and `templateId`. If an email has one of them set, `update()` gets a `422` error.
- The SDK does not send `editor`, `templateId`, `replyTo` or `content` when they are `null`.

Expand Down Expand Up @@ -1146,6 +1146,8 @@ Without `placement`, the API puts the block at the end of the email. The positio

A `null` block throws `NullPointerException`, and the SDK sends no request.

The API removes the attributes and the `mobile` keys that the block type does not declare, and the insert is still a success. Then the response has `rejected` with the paths of these values, for example `editor_content.attrs.onclick`. Otherwise `rejected` is `null`. Only `createBlock` and `updateBlock` fill `rejected`. `getBlock` and `moveBlock` leave it `null`.

```java

// import ....
Expand Down Expand Up @@ -1174,6 +1176,11 @@ class Example {

System.out.println(blockId + " " + etag);

if (response.rejected != null) {

System.out.println(String.join(", ", response.rejected.keys));
}

} catch (MailerLiteException e) {

e.printStackTrace();
Expand All @@ -1186,6 +1193,8 @@ class Example {

The new `editorContent` replaces the whole content of the block. The API removes the keys that you do not send. So get the block, change its `editorContent`, and send all of it. The type and the position of the block do not change.

The API removes the attributes and the `mobile` keys that the block type does not declare, and the update is still a success. Then the response has `rejected` with the paths of these values. Otherwise `rejected` is `null`.

```java

// import ....
Expand All @@ -1206,6 +1215,11 @@ class Example {

System.out.println(response.etag);

if (response.rejected != null) {

System.out.println(String.join(", ", response.rejected.keys));
}

} catch (MailerLiteException e) {

e.printStackTrace();
Expand Down Expand Up @@ -1268,7 +1282,7 @@ class Example {

### Validate a block

Checks a block before you insert it, and saves nothing. It needs no email and no etag. An invalid block is not an error: the response has `valid` set to `false` and the `errors` for each field. A `null` block sends an empty body.
Checks a block before you insert it, and saves nothing. It needs no email and no etag. An invalid block is not an error: the response has `valid` set to `false` and the `errors` for each field. If the block is valid and an insert would remove attributes, `rejected` has their paths. A `null` block sends an empty body.

```java

Expand Down Expand Up @@ -1384,6 +1398,8 @@ class Example {

Copies the email to a new template. The API cannot delete templates.

To start a new campaign email from the template, send the `id` of the template as `templateId` (`template_id`) when you [create a campaign with the simple editor](#create-a-campaign-with-the-simple-editor).

```java

// import ....
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/mailerlite/sdk/emails/EmailBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public class EmailBase extends ApiResource {
public String editor;

/**
* The template ID: the template the new email starts from. Allowed only when {@code editor} is {@code simple},
* accepts only a template from the shared gallery, and also has to be the same across every email.
* The template ID: the simple editor template the new email starts from. It is a template from the shared
* gallery, or a template that you saved from your own email with {@code Emails.createTemplate()}. Allowed only
* when {@code editor} is {@code simple}, and also has to be the same across every email.
* <p>
* If it is null, the SDK does not send it. Only {@code CampaignBuilder.create()} accepts it. If it is set,
* {@code CampaignBuilder.update()} gets a 422 error.
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/mailerlite/sdk/emails/EmailRejected.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@
import com.google.gson.annotations.SerializedName;

/**
* The unsafe values that the API removed or replaced. The request is still a success.
* The values that the API removed or replaced. The request is still a success.
* <p>
* For settings, these are unsafe values. For a block, these are the attributes and the mobile keys that the block
* type does not declare.
*/
public class EmailRejected {

/** The message. */
@SerializedName("message")
public String message;

/** The paths of the values, for example "footer.social_links[0].url". */
/**
* The paths of the values, for example "footer.social_links[0].url" or "editor_content.attrs.onclick".
*/
@SerializedName("keys")
public String[] keys;
}
4 changes: 3 additions & 1 deletion src/main/java/com/mailerlite/sdk/emails/EmailValidation.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public class EmailValidation extends MailerLiteResponse {
public EmailSchema schema;

/**
* The unsafe values that an update would remove, or null if there are none. Only the validation of settings has it.
* The values that the write would remove, or null if there are none. The API sends it only when valid is true.
* For settings, these are unsafe values. For a block, these are the attributes and the mobile keys that the
* block type does not declare.
*/
@SerializedName("rejected")
public EmailRejected rejected;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/mailerlite/sdk/emails/Emails.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ public SingleEmailBlock getBlock(String emailId, String blockId) throws MailerLi
* <p>
* Without a placement, the API puts the block at the end of the email. The response has the new block with
* its placement and the new etag.
* <p>
* The API removes the attributes and the mobile keys that the block type does not declare, and the insert is
* still a success. Then the rejected field of the response has their paths. Otherwise it is null.
*
* @param emailId the email ID
* @param etag the etag from the last response for the email
Expand All @@ -121,6 +124,9 @@ public SingleEmailBlock createBlock(String emailId, String etag, EmailBlockInput
* <p>
* Without a placement, the API puts the block at the end of the email. The response has the new block with
* its placement and the new etag.
* <p>
* The API removes the attributes and the mobile keys that the block type does not declare, and the insert is
* still a success. Then the rejected field of the response has their paths. Otherwise it is null.
*
* @param emailId the email ID
* @param etag the etag from the last response for the email
Expand All @@ -144,6 +150,9 @@ public SingleEmailBlock createBlock(String emailId, String etag, EmailBlockInput
* <p>
* The new editor content replaces the whole content of the block. The API removes the keys that you do not
* send. Read the block, change it, and send the full editor content.
* <p>
* The API removes the attributes and the mobile keys that the block type does not declare, and the update is
* still a success. Then the rejected field of the response has their paths. Otherwise it is null.
*
* @param emailId the email ID
* @param blockId the block ID
Expand All @@ -163,6 +172,9 @@ public SingleEmailBlock updateBlock(String emailId, String blockId, String etag,
* <p>
* The new editor content replaces the whole content of the block. The API removes the keys that you do not
* send. Read the block, change it, and send the full editor content.
* <p>
* The API removes the attributes and the mobile keys that the block type does not declare, and the update is
* still a success. Then the rejected field of the response has their paths. Otherwise it is null.
*
* @param emailId the email ID
* @param blockId the block ID
Expand Down Expand Up @@ -267,6 +279,7 @@ public SingleEmailBlock moveBlock(String emailId, String blockId, String etag, E
* Checks a block with the rules of an insert, and saves nothing. It needs no email and no etag.
* <p>
* An invalid block is not an error: the response has valid set to false and the errors for each field.
* If the block is valid and an insert would remove attributes, rejected has their paths.
*
* @param block the block to check. A null block sends an empty body.
* @return the result of the validation
Expand Down Expand Up @@ -354,6 +367,9 @@ public EmailValidation validateSettings(String emailId, EmailSettings settings)

/**
* Copies an email to a new template. The API cannot delete templates.
* <p>
* To start a new campaign email from the template, set the templateId of the email to the ID of the template
* when you create a campaign with the simple editor.
*
* @param emailId the email ID
* @param name the name of the new template
Expand All @@ -367,6 +383,9 @@ public SingleEmailTemplate createTemplate(String emailId, String name) throws Ma

/**
* Copies an email to a new template. The API cannot delete templates.
* <p>
* To start a new campaign email from the template, set the templateId of the email to the ID of the template
* when you create a campaign with the simple editor.
*
* @param emailId the email ID
* @param name the name of the new template
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/mailerlite/sdk/emails/SingleEmailBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ public class SingleEmailBlock extends MailerLiteResponse {
/** The etag of the email. Send it with the next write to the email. */
@SerializedName("etag")
public String etag;

/**
* The attributes that the API removed from the block, or null if there are none. Only createBlock and
* updateBlock fill it. getBlock and moveBlock leave it null.
*/
@SerializedName("rejected")
public EmailRejected rejected;
}
55 changes: 55 additions & 0 deletions src/test/java/com/mailerlite/sdk/tests/EmailsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ public void getBlockDecodesTheBlock() throws Exception
assertEquals("56", response.block.placement.after);
assertNull(response.block.placement.before);
assertNull(response.block.children);
assertNull(response.rejected);
}

@Test
Expand Down Expand Up @@ -378,6 +379,24 @@ public void createBlockSendsTheBlock() throws Exception
assertEquals("center", map(response.block.editorContent, "attrs").get("textAlign"));
assertEquals("57", response.block.placement.after);
assertNull(response.block.placement.before);
assertNull(response.rejected);
}

@Test
public void createBlockReadsTheRejectedAttributes() throws Exception
{
client.respond(201, Fixtures.read("email_block_created_rejected.json"));

Map<String, Object> content = paragraph("left", "Hi");
map(content, "attrs").put("onclick", "x");

SingleEmailBlock response = emails.createBlock("123", "43", new EmailBlockInput(EmailBlockTypes.PARAGRAPH, content));

assertEquals(201, response.responseStatusCode);
assertEquals("44", response.etag);
assertEquals("59", response.block.id);
assertEquals("Some attributes were rejected.", response.rejected.message);
assertArrayEquals(new String[] {"editor_content.attrs.onclick"}, response.rejected.keys);
}

@Test
Expand Down Expand Up @@ -471,6 +490,26 @@ public void updateBlockSendsTheEditorContent() throws Exception
assertEquals("57", response.block.id);
assertEquals("56", response.block.placement.after);
assertEquals("58", response.block.placement.before);
assertNull(response.rejected);
}

@Test
public void updateBlockReadsTheRejectedAttributes() throws Exception
{
client.respond(200, Fixtures.read("email_block_updated_rejected.json"));

Map<String, Object> content = paragraph("left", "Hello again");
map(content, "attrs").put("onclick", "y");
content.put("mobile", Collections.singletonMap("onclick", "y"));

SingleEmailBlock response = emails.updateBlock("123", "57", "44", content);

assertEquals(200, response.responseStatusCode);
assertEquals("45", response.etag);
assertEquals("57", response.block.id);
assertEquals("Some attributes were rejected.", response.rejected.message);
assertArrayEquals(new String[] {"editor_content.attrs.onclick", "editor_content.mobile.onclick"},
response.rejected.keys);
}

@Test
Expand Down Expand Up @@ -554,6 +593,7 @@ public void moveBlockSendsThePlacement() throws Exception
assertEquals("57", response.block.id);
assertNull(response.block.placement.after);
assertEquals("1", response.block.placement.before);
assertNull(response.rejected);
}

@Test
Expand Down Expand Up @@ -611,6 +651,21 @@ public void validateBlockValid() throws Exception
assertNull(response.rejected);
}

@Test
public void validateBlockReadsTheRejectedAttributes() throws Exception
{
client.respond(200, Fixtures.read("email_block_validation_rejected.json"));

Map<String, Object> content = paragraph("left", "Hi");
map(content, "attrs").put("onclick", "x");

EmailValidation response = emails.validateBlock(new EmailBlockInput(EmailBlockTypes.PARAGRAPH, content));

assertTrue(response.valid);
assertEquals("Some attributes were rejected.", response.rejected.message);
assertArrayEquals(new String[] {"editor_content.attrs.onclick"}, response.rejected.keys);
}

@Test
public void validateBlockInvalid() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ private static List<Example> examples()

list.add(new Example("Insert a block")
.respond(200, Fixtures.read("email.json"))
.respond(201, Fixtures.read("email_block_created.json"))
.respond(201, Fixtures.read("email_block_created_rejected.json"))
.expect("GET /emails/123456")
.expect("POST /emails/123456/blocks"));

list.add(new Example("Update a block")
.respond(200, Fixtures.read("email_block.json"))
.respond(200, Fixtures.read("email_block_updated.json"))
.respond(200, Fixtures.read("email_block_updated_rejected.json"))
.expect("GET /emails/123456/blocks/57")
.expect("PATCH /emails/123456/blocks/57"));

Expand Down
15 changes: 15 additions & 0 deletions src/test/resources/responses/email_block_created_rejected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"data": {
"id": "59",
"type": "paragraph",
"editor_content": {
"content": [{"text": "Hi", "type": "text"}]
},
"placement": {"after": "58", "before": null}
},
"etag": "44",
"rejected": {
"message": "Some attributes were rejected.",
"keys": ["editor_content.attrs.onclick"]
}
}
16 changes: 16 additions & 0 deletions src/test/resources/responses/email_block_updated_rejected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"data": {
"id": "57",
"type": "paragraph",
"editor_content": {
"attrs": {"textAlign": "left"},
"content": [{"text": "Hello again", "type": "text"}]
},
"placement": {"after": "56", "before": "58"}
},
"etag": "45",
"rejected": {
"message": "Some attributes were rejected.",
"keys": ["editor_content.attrs.onclick", "editor_content.mobile.onclick"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"valid": true,
"errors": {},
"schema": {"version": "2.73.2", "hash": "0f3a9c"},
"rejected": {
"message": "Some attributes were rejected.",
"keys": ["editor_content.attrs.onclick"]
}
}