Skip to content

Commit 1a2097e

Browse files
authored
[Java] Add add constructor to BYO OkHttpClient (#6401)
* [Java] Add add constructor to BYO OkHttpClient * client example * this client changed too
1 parent 171337e commit 1a2097e

File tree

3 files changed

+51
-0
lines changed
  • modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson
  • samples/client/petstore/java
    • okhttp-gson-parcelableModel/src/main/java/org/openapitools/client
    • okhttp-gson/src/main/java/org/openapitools/client

3 files changed

+51
-0
lines changed

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ public class ApiClient {
102102
authentications = Collections.unmodifiableMap(authentications);
103103
}
104104

105+
/*
106+
* Basic constructor with custom OkHttpClient
107+
*/
108+
public ApiClient(OkHttpClient client) {
109+
init();
110+
111+
httpClient = client;
112+
113+
// Setup authentications (key: authentication name, value: authentication).{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
114+
authentications.put("{{name}}", new HttpBasicAuth());{{/isBasicBasic}}{{^isBasicBasic}}
115+
authentications.put("{{name}}", new HttpBearerAuth("{{scheme}}"));{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}}
116+
authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}{{#isKeyInCookie}}"cookie"{{/isKeyInCookie}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}}
117+
authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
118+
// Prevent the authentications from being modified.
119+
authentications = Collections.unmodifiableMap(authentications);
120+
}
121+
105122
{{#hasOAuthMethods}}
106123
{{#oauthMethods}}
107124
{{#-first}}

samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ public ApiClient() {
9797
authentications = Collections.unmodifiableMap(authentications);
9898
}
9999

100+
/*
101+
* Basic constructor with custom OkHttpClient
102+
*/
103+
public ApiClient(OkHttpClient client) {
104+
init();
105+
106+
httpClient = client;
107+
108+
// Setup authentications (key: authentication name, value: authentication).
109+
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
110+
authentications.put("api_key_query", new ApiKeyAuth("query", "api_key_query"));
111+
authentications.put("http_basic_test", new HttpBasicAuth());
112+
authentications.put("petstore_auth", new OAuth());
113+
// Prevent the authentications from being modified.
114+
authentications = Collections.unmodifiableMap(authentications);
115+
}
116+
100117
/*
101118
* Constructor for ApiClient to support access token retry on 401/403 configured with client ID
102119
*/

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ public ApiClient() {
9797
authentications = Collections.unmodifiableMap(authentications);
9898
}
9999

100+
/*
101+
* Basic constructor with custom OkHttpClient
102+
*/
103+
public ApiClient(OkHttpClient client) {
104+
init();
105+
106+
httpClient = client;
107+
108+
// Setup authentications (key: authentication name, value: authentication).
109+
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
110+
authentications.put("api_key_query", new ApiKeyAuth("query", "api_key_query"));
111+
authentications.put("http_basic_test", new HttpBasicAuth());
112+
authentications.put("petstore_auth", new OAuth());
113+
// Prevent the authentications from being modified.
114+
authentications = Collections.unmodifiableMap(authentications);
115+
}
116+
100117
/*
101118
* Constructor for ApiClient to support access token retry on 401/403 configured with client ID
102119
*/

0 commit comments

Comments
 (0)