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

feat: add support for environment variables #586

Merged
merged 1 commit into from
Sep 23, 2020
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ Twilio.setEdge("sydney");

This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`.

### Environment Variables

`twilio-java` supports the credentials, region, and edge values stored in the following environment variables:
* `TWILIO_ACCOUNT_SID`
* `TWILIO_AUTH_TOKEN`
* `TWILIO_REGION`
* `TWILIO_EDGE`

If using these variables, the above client initialization can be skipped.

### Send an SMS

```java
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/com/twilio/Twilio.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@ public class Twilio {
public static final String VERSION = "7.55.3";
public static final String JAVA_VERSION = System.getProperty("java.version");

private static String username;
private static String password;
private static String accountSid;
private static String region;
private static String edge;
private static String username = System.getenv("TWILIO_ACCOUNT_SID");
private static String password = System.getenv("TWILIO_AUTH_TOKEN");
private static String accountSid; // username used if this is null
private static String region = System.getenv("TWILIO_REGION");
private static String edge = System.getenv("TWILIO_EDGE");
private static TwilioRestClient restClient;
private static ExecutorService executorService;

private Twilio() {}
private Twilio() {
}

/**
/*
* Ensures that the ListeningExecutorService is shutdown when the JVM exits.
*/
static {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
if (executorService != null) {
executorService.shutdownNow();
Expand Down Expand Up @@ -191,7 +193,7 @@ public static void setRestClient(final TwilioRestClient restClient) {
*
* @return the Twilio executor service
*/
public static ExecutorService getExecutorService() {
public static synchronized ExecutorService getExecutorService() {
if (Twilio.executorService == null) {
Twilio.executorService = Executors.newCachedThreadPool();
}
Expand Down