Skip to content

Commit

Permalink
Provide easy way to override BASE_URL to allow functional application…
Browse files Browse the repository at this point in the history
… testing plivo#37
  • Loading branch information
dmitrykiru committed Sep 3, 2018
1 parent 3b30cfa commit 2c50d68
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/main/java/com/plivo/api/PlivoClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public void serialize(Enum value, JsonGenerator gen, SerializerProvider provider
.setLevel(Level.BODY);
private final String authId;
private final String authToken;
private final String baseUrl;
private OkHttpClient httpClient;
private Retrofit retrofit;
private PlivoAPIService apiService = null;
Expand All @@ -103,6 +104,17 @@ public PlivoClient(String authId, String authToken) {
this(authId, authToken, new Builder());
}

public PlivoClient(String authId, String authToken, String baseUrl) {
this(authId, authToken, baseUrl, new Builder());
}

/**
* Constructor left for backward compatibility
*/
public PlivoClient(String authId, String authToken, OkHttpClient.Builder httpClientBuilder) {
this(authId, authToken, BASE_URL, new Builder());
}

/**
* Constructs a new PlivoClient instance. To set a proxy, timeout etc, you can pass in an OkHttpClient.Builder, on which you can set
* the timeout and proxy using:
Expand All @@ -117,13 +129,14 @@ public PlivoClient(String authId, String authToken) {
* @param authToken
* @param httpClientBuilder
*/
public PlivoClient(String authId, String authToken, OkHttpClient.Builder httpClientBuilder) {
public PlivoClient(String authId, String authToken, String baseUrl, OkHttpClient.Builder httpClientBuilder) {
if (!(Utils.isAccountIdValid(authId) || Utils.isSubaccountIdValid(authId))) {
throw new IllegalArgumentException("invalid account ID");
}

this.authId = authId;
this.authToken = authToken;
this.baseUrl = baseUrl;

httpClient = httpClientBuilder
.addNetworkInterceptor(interceptor)
Expand Down Expand Up @@ -159,7 +172,7 @@ public PlivoClient(String authId, String authToken, OkHttpClient.Builder httpCli

retrofit = new Retrofit.Builder()
.client(httpClient)
.baseUrl(BASE_URL)
.baseUrl(baseUrl)
.addConverterFactory(JacksonConverterFactory.create(objectMapper))
.build();

Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/plivo/api/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public void setUp() throws Exception {
server = new MockWebServer();
server.start();

PlivoClient.BASE_URL = server.url("/").toString();
Plivo.init(authId, authToken);
Plivo.init(authId, authToken, server.url("/").toString());
Plivo.getClient().setTesting(true);
}

Expand Down

0 comments on commit 2c50d68

Please sign in to comment.