Skip to content

Commit f0e0b12

Browse files
authored
SWI-9389 Client Updates (#226)
* SWI-9389 Client Updates * update template * update test
1 parent c0ab441 commit f0e0b12

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

custom_templates/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ public class ApiClient {
224224
}
225225
}
226226
RetryingOAuth retryingOAuth = new RetryingOAuth(client, tokenUrl, clientId, OAuthFlow.{{#lambda.uppercase}}{{#lambda.snakecase}}{{flow}}{{/lambda.snakecase}}{{/lambda.uppercase}}, clientSecret, parameters);
227+
initHttpClient(client, Collections.<Interceptor>singletonList(retryingOAuth));
227228
authentications.put("{{name}}", retryingOAuth);{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
228229
authentications.put("{{name}}", new HttpBasicAuth());{{/isBasicBasic}}{{#isBasicBearer}}
229230
authentications.put("{{name}}", new HttpBearerAuth("{{scheme}}"));{{/isBasicBearer}}{{/isBasic}}{{#isApiKey}}
@@ -281,7 +282,11 @@ public class ApiClient {
281282
}
282283

283284
protected void initHttpClient(List<Interceptor> interceptors) {
284-
OkHttpClient.Builder builder = new OkHttpClient.Builder();
285+
initHttpClient(new OkHttpClient(), interceptors);
286+
}
287+
288+
protected void initHttpClient(OkHttpClient okHttpClient, List<Interceptor> interceptors) {
289+
OkHttpClient.Builder builder = okHttpClient.newBuilder();
285290
builder.addNetworkInterceptor(getProgressInterceptor());
286291
for (Interceptor interceptor: interceptors) {
287292
builder.addInterceptor(interceptor);

src/main/java/com/bandwidth/sdk/ApiClient.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ public ApiClient(String clientId, String clientSecret, Map<String, String> param
182182
}
183183
}
184184
RetryingOAuth retryingOAuth = new RetryingOAuth(client, tokenUrl, clientId, OAuthFlow.APPLICATION, clientSecret, parameters);
185+
initHttpClient(client, Collections.<Interceptor>singletonList(retryingOAuth));
185186
authentications.put("OAuth2", retryingOAuth);
186187
authentications.put("Basic", new HttpBasicAuth());
187188

@@ -230,7 +231,11 @@ protected void initHttpClient() {
230231
}
231232

232233
protected void initHttpClient(List<Interceptor> interceptors) {
233-
OkHttpClient.Builder builder = new OkHttpClient.Builder();
234+
initHttpClient(new OkHttpClient(), interceptors);
235+
}
236+
237+
protected void initHttpClient(OkHttpClient okHttpClient, List<Interceptor> interceptors) {
238+
OkHttpClient.Builder builder = okHttpClient.newBuilder();
234239
builder.addNetworkInterceptor(getProgressInterceptor());
235240
for (Interceptor interceptor: interceptors) {
236241
builder.addInterceptor(interceptor);

src/test/java/com/bandwidth/sdk/unit/api/ApiClientTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.hamcrest.MatcherAssert.assertThat;
1818
import static org.hamcrest.Matchers.sameInstance;
1919
import static org.hamcrest.Matchers.not;
20+
import static org.hamcrest.Matchers.contains;
2021

2122
import java.util.Map;
2223

@@ -33,7 +34,7 @@ public void testHttpClient() throws Exception {
3334
assertThat(defaultOauthClient.getHttpClient(), instanceOf(OkHttpClient.class));
3435
assertThat(customOauthClient.getHttpClient(), instanceOf(OkHttpClient.class));
3536
assertThat(defaultOauthClient.getHttpClient(), not(sameInstance(customOauthClient.getHttpClient())));
36-
assertThat(customOauthClient.getHttpClient(), sameInstance(httpClient));
37+
assertThat(customOauthClient.getHttpClient().interceptors(), contains(instanceOf(RetryingOAuth.class)));
3738
}
3839

3940
@Test

0 commit comments

Comments
 (0)