-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Make ApiClient in retrofit2 be able to use own OkHttpClient #6699
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
Changes from 5 commits
c46b027
b4fec1c
6f646db
b0855b3
26f777a
ef3a88e
d7634ae
d1de6b8
ccf3c20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,10 +35,18 @@ public class ApiClient { | |
| private OkHttpClient.Builder okBuilder; | ||
| private Retrofit.Builder adapterBuilder; | ||
| private JSON json; | ||
| private OkHttpClient okHttpClient; | ||
|
|
||
| public ApiClient() { | ||
| apiAuthorizations = new LinkedHashMap<String, Interceptor>(); | ||
| createDefaultAdapter(); | ||
| okBuilder = new OkHttpClient.Builder(); | ||
| } | ||
|
|
||
| public ApiClient(OkHttpClient client){ | ||
| apiAuthorizations = new LinkedHashMap<String, Interceptor>(); | ||
| createDefaultAdapter(); | ||
| okHttpClient = client; | ||
| } | ||
|
|
||
| public ApiClient(String[] authNames) { | ||
|
|
@@ -114,7 +122,6 @@ public ApiClient(String authName, String clientId, String secret, String usernam | |
|
|
||
| public void createDefaultAdapter() { | ||
| json = new JSON(); | ||
| okBuilder = new OkHttpClient.Builder(); | ||
|
|
||
| String baseUrl = "http://petstore.swagger.io:80/v2"; | ||
| if (!baseUrl.endsWith("/")) | ||
|
|
@@ -128,10 +135,11 @@ public void createDefaultAdapter() { | |
| } | ||
|
|
||
| public <S> S createService(Class<S> serviceClass) { | ||
| return adapterBuilder | ||
| .client(okBuilder.build()) | ||
| .build() | ||
| .create(serviceClass); | ||
| if (okHttpClient != null) { | ||
| return adapterBuilder.client(okHttpClient).build().create(serviceClass); | ||
| }else { | ||
| return adapterBuilder.client(okBuilder.build()).build().create(serviceClass); | ||
| } | ||
| } | ||
|
|
||
| public ApiClient setDateFormat(DateFormat dateFormat) { | ||
|
|
@@ -303,7 +311,11 @@ public ApiClient addAuthorization(String authName, Interceptor authorization) { | |
| throw new RuntimeException("auth name \"" + authName + "\" already in api authorizations"); | ||
| } | ||
| apiAuthorizations.put(authName, authorization); | ||
| if(okBuilder == null){ | ||
| throw new Exception("okBuilder is null") | ||
|
||
| } | ||
| okBuilder.addInterceptor(authorization); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
space missing