Skip to content

Commit fb7aa58

Browse files
authored
[Java][RestClient] Enable access token refresh (#20733)
1 parent 481c690 commit fb7aa58

File tree

8 files changed

+113
-0
lines changed
  • modules/openapi-generator/src/main/resources/Java/libraries/restclient
  • samples/client
    • echo_api/java/restclient/src/main/java/org/openapitools/client
    • others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client
    • petstore/java
      • restclient/src/main/java/org/openapitools/client
      • restclient-nullable-arrays/src/main/java/org/openapitools/client
      • restclient-swagger2/src/main/java/org/openapitools/client
      • restclient-useSingleRequestParameter/src/main/java/org/openapitools/client

8 files changed

+113
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ Here are some companies/projects (alphabetical order) using OpenAPI Generator in
631631
- [act coding](https://github.com/actcoding)
632632
- [Adaptant Solutions AG](https://www.adaptant.io/)
633633
- [adesso SE](https://www.adesso.de/)
634+
- [adorsys GmbH & Co.KG](https://adorsys.com/)
634635
- [Adyen](https://www.adyen.com/)
635636
- [Agoda](https://www.agoda.com/)
636637
- [Airthings](https://www.airthings.com/)

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import java.util.List;
4444
import java.util.Map;
4545
import java.util.Map.Entry;
4646
import java.util.TimeZone;
47+
import java.util.function.Supplier;
4748

4849
import {{javaxPackage}}.annotation.Nullable;
4950

@@ -240,6 +241,21 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
240241
throw new RuntimeException("No Bearer authentication configured!");
241242
}
242243

244+
/**
245+
* Helper method to set the supplier of access tokens for Bearer authentication.
246+
*
247+
* @param tokenSupplier the token supplier function
248+
*/
249+
public void setBearerToken(Supplier<String> tokenSupplier) {
250+
for (Authentication auth : authentications.values()) {
251+
if (auth instanceof HttpBearerAuth) {
252+
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
253+
return;
254+
}
255+
}
256+
throw new RuntimeException("No Bearer authentication configured!");
257+
}
258+
243259
/**
244260
* Helper method to set username for the first HTTP basic authentication.
245261
* @param username the username

samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.util.Map;
4747
import java.util.Map.Entry;
4848
import java.util.TimeZone;
49+
import java.util.function.Supplier;
4950

5051
import jakarta.annotation.Nullable;
5152

@@ -222,6 +223,21 @@ public void setBearerToken(String bearerToken) {
222223
throw new RuntimeException("No Bearer authentication configured!");
223224
}
224225

226+
/**
227+
* Helper method to set the supplier of access tokens for Bearer authentication.
228+
*
229+
* @param tokenSupplier the token supplier function
230+
*/
231+
public void setBearerToken(Supplier<String> tokenSupplier) {
232+
for (Authentication auth : authentications.values()) {
233+
if (auth instanceof HttpBearerAuth) {
234+
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
235+
return;
236+
}
237+
}
238+
throw new RuntimeException("No Bearer authentication configured!");
239+
}
240+
225241
/**
226242
* Helper method to set username for the first HTTP basic authentication.
227243
* @param username the username

samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.util.Map;
4747
import java.util.Map.Entry;
4848
import java.util.TimeZone;
49+
import java.util.function.Supplier;
4950

5051
import jakarta.annotation.Nullable;
5152

@@ -220,6 +221,21 @@ public void setBearerToken(String bearerToken) {
220221
throw new RuntimeException("No Bearer authentication configured!");
221222
}
222223

224+
/**
225+
* Helper method to set the supplier of access tokens for Bearer authentication.
226+
*
227+
* @param tokenSupplier the token supplier function
228+
*/
229+
public void setBearerToken(Supplier<String> tokenSupplier) {
230+
for (Authentication auth : authentications.values()) {
231+
if (auth instanceof HttpBearerAuth) {
232+
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
233+
return;
234+
}
235+
}
236+
throw new RuntimeException("No Bearer authentication configured!");
237+
}
238+
223239
/**
224240
* Helper method to set username for the first HTTP basic authentication.
225241
* @param username the username

samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.util.Map;
4747
import java.util.Map.Entry;
4848
import java.util.TimeZone;
49+
import java.util.function.Supplier;
4950

5051
import jakarta.annotation.Nullable;
5152

@@ -220,6 +221,21 @@ public void setBearerToken(String bearerToken) {
220221
throw new RuntimeException("No Bearer authentication configured!");
221222
}
222223

224+
/**
225+
* Helper method to set the supplier of access tokens for Bearer authentication.
226+
*
227+
* @param tokenSupplier the token supplier function
228+
*/
229+
public void setBearerToken(Supplier<String> tokenSupplier) {
230+
for (Authentication auth : authentications.values()) {
231+
if (auth instanceof HttpBearerAuth) {
232+
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
233+
return;
234+
}
235+
}
236+
throw new RuntimeException("No Bearer authentication configured!");
237+
}
238+
223239
/**
224240
* Helper method to set username for the first HTTP basic authentication.
225241
* @param username the username

samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.util.Map;
4747
import java.util.Map.Entry;
4848
import java.util.TimeZone;
49+
import java.util.function.Supplier;
4950

5051
import jakarta.annotation.Nullable;
5152

@@ -226,6 +227,21 @@ public void setBearerToken(String bearerToken) {
226227
throw new RuntimeException("No Bearer authentication configured!");
227228
}
228229

230+
/**
231+
* Helper method to set the supplier of access tokens for Bearer authentication.
232+
*
233+
* @param tokenSupplier the token supplier function
234+
*/
235+
public void setBearerToken(Supplier<String> tokenSupplier) {
236+
for (Authentication auth : authentications.values()) {
237+
if (auth instanceof HttpBearerAuth) {
238+
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
239+
return;
240+
}
241+
}
242+
throw new RuntimeException("No Bearer authentication configured!");
243+
}
244+
229245
/**
230246
* Helper method to set username for the first HTTP basic authentication.
231247
* @param username the username

samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.util.Map;
4747
import java.util.Map.Entry;
4848
import java.util.TimeZone;
49+
import java.util.function.Supplier;
4950

5051
import jakarta.annotation.Nullable;
5152

@@ -226,6 +227,21 @@ public void setBearerToken(String bearerToken) {
226227
throw new RuntimeException("No Bearer authentication configured!");
227228
}
228229

230+
/**
231+
* Helper method to set the supplier of access tokens for Bearer authentication.
232+
*
233+
* @param tokenSupplier the token supplier function
234+
*/
235+
public void setBearerToken(Supplier<String> tokenSupplier) {
236+
for (Authentication auth : authentications.values()) {
237+
if (auth instanceof HttpBearerAuth) {
238+
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
239+
return;
240+
}
241+
}
242+
throw new RuntimeException("No Bearer authentication configured!");
243+
}
244+
229245
/**
230246
* Helper method to set username for the first HTTP basic authentication.
231247
* @param username the username

samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.util.Map;
4747
import java.util.Map.Entry;
4848
import java.util.TimeZone;
49+
import java.util.function.Supplier;
4950

5051
import jakarta.annotation.Nullable;
5152

@@ -226,6 +227,21 @@ public void setBearerToken(String bearerToken) {
226227
throw new RuntimeException("No Bearer authentication configured!");
227228
}
228229

230+
/**
231+
* Helper method to set the supplier of access tokens for Bearer authentication.
232+
*
233+
* @param tokenSupplier the token supplier function
234+
*/
235+
public void setBearerToken(Supplier<String> tokenSupplier) {
236+
for (Authentication auth : authentications.values()) {
237+
if (auth instanceof HttpBearerAuth) {
238+
((HttpBearerAuth) auth).setBearerToken(tokenSupplier);
239+
return;
240+
}
241+
}
242+
throw new RuntimeException("No Bearer authentication configured!");
243+
}
244+
229245
/**
230246
* Helper method to set username for the first HTTP basic authentication.
231247
* @param username the username

0 commit comments

Comments
 (0)