Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #404 Add ability to impersonate subusers #495

Merged
merged 1 commit into from
May 10, 2019
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
28 changes: 28 additions & 0 deletions src/main/java/com/sendgrid/SendGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class SendGrid implements SendGridAPI {
/** The number of milliseconds to sleep between retries. */
private int rateLimitSleep;

/** The subuser to be impersonated. */
private String subuser;

/**
* Construct a new SendGrid API wrapper.
* @param apiKey is your SendGrid API Key: https://app.sendgrid.com/settings/api_keys
Expand Down Expand Up @@ -193,6 +196,31 @@ public void setRateLimitSleep(int rateLimitSleep) {
this.rateLimitSleep = rateLimitSleep;
}

/**
* Impersonate subuser for subsequent requests
* @param subuser the subuser to be impersonated
*/
public void addImpersonateSubuser(String subuser) {
this.subuser = subuser;
this.addRequestHeader("on-behalf-of", subuser);
}

/**
* Stop Impersonating the subuser
*/
public void removeImpersonateSubuser() {
this.subuser = null;
this.removeRequestHeader("on-behalf-of");
}

/**
* Get the impersonated subuser or null if empty
* @return the impersonated subuser
*/
public String getImpersonateSubuser() {
return this.subuser;
}

/**
* Makes the call to the SendGrid API, override this method for testing.
* @param request the request to make.
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/com/sendgrid/SendGridTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3406,4 +3406,33 @@ public void test_whitelabel_links__link_id__subuser_post() throws IOException {
Assert.assertEquals(200, response.getStatusCode());
}

@Test
public void test_add_impersonate_subuser() {
SendGrid sg = new SendGrid(SENDGRID_API_KEY);

sg.addImpersonateSubuser("subusername");
Assert.assertEquals(sg.getRequestHeaders().get("on-behalf-of"), "subusername");
}

@Test
public void test_remove_impersonate_subuser() {
SendGrid sg = new SendGrid(SENDGRID_API_KEY);

sg.addImpersonateSubuser("subusername");
Assert.assertEquals(sg.getRequestHeaders().get("on-behalf-of"), "subusername");

sg.removeImpersonateSubuser();
Assert.assertEquals(sg.getRequestHeaders().get("on-behalf-of"), null);
}

@Test
public void test_get_impersonate_subuser() {
SendGrid sg = new SendGrid(SENDGRID_API_KEY);

sg.addImpersonateSubuser("subusername");
Assert.assertEquals(sg.getImpersonateSubuser(), "subusername");

sg.removeImpersonateSubuser();
Assert.assertEquals(sg.getImpersonateSubuser(), null);
}
}