11package com .kinde .accounts .manager .impl ;
22
3- import com .google .inject .Inject ;
4- import com .kinde .accounts .dto .EntitlementDto ;
5- import com .kinde .accounts .manager .EntitlementsManager ;
6- import com .kinde .accounts .util .ApiResponseHandler ;
7- import com .kinde .accounts .util .PaginationHelper ;
8- import com .kinde .accounts .dto .DtoConverter ;
9- import com .kinde .KindeClientSession ;
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+ import java .util .concurrent .CompletableFuture ;
6+ import java .util .concurrent .ExecutionException ;
7+
108import org .openapitools .client .ApiException ;
119import org .openapitools .client .api .DefaultApi ;
12- import org .openapitools .client .model .Entitlement ;
1310import org .openapitools .client .model .EntitlementResponse ;
1411import org .openapitools .client .model .EntitlementsResponse ;
1512import org .openapitools .client .model .EntitlementsResponseData ;
1613import org .slf4j .Logger ;
1714import org .slf4j .LoggerFactory ;
1815
19- import java .util .ArrayList ;
20- import java .util .List ;
21- import java .util .concurrent .CompletableFuture ;
22- import java .util .concurrent .ExecutionException ;
16+ import com .google .inject .Inject ;
17+ import com .kinde .KindeClientSession ;
18+ import com .kinde .accounts .dto .DtoConverter ;
19+ import com .kinde .accounts .dto .EntitlementDto ;
20+ import com .kinde .accounts .manager .EntitlementsManager ;
21+ import com .kinde .accounts .util .ApiResponseHandler ;
22+
2323
2424/**
2525 * Implementation of EntitlementsManager that handles entitlements operations.
@@ -30,40 +30,39 @@ public class EntitlementsManagerImpl implements EntitlementsManager {
3030 private static final Logger log = LoggerFactory .getLogger (EntitlementsManagerImpl .class );
3131
3232 private final DefaultApi apiClient ;
33- private final PaginationHelper paginationHelper ;
3433 private final ApiResponseHandler responseHandler ;
3534 private final KindeClientSession session ;
3635
3736 @ Inject
3837 public EntitlementsManagerImpl (
3938 DefaultApi apiClient ,
40- PaginationHelper paginationHelper ,
4139 ApiResponseHandler responseHandler ,
4240 KindeClientSession session ) {
4341 this .apiClient = apiClient ;
44- this .paginationHelper = paginationHelper ;
4542 this .responseHandler = responseHandler ;
4643 this .session = session ;
47- configureApiClient ();
4844 }
4945
5046 /**
5147 * Configures the API client with authentication headers from the session.
48+ * Called lazily before API use to avoid infinite recursion when the session's
49+ * getAccessToken() triggers retrieveTokens() which creates a KindeAccountsClient.
5250 */
5351 private void configureApiClient () {
5452 String accessToken = session .getAccessToken ();
55- if (accessToken != null && !accessToken .isEmpty ()) {
56- apiClient .getApiClient ().setBearerToken (accessToken );
57- }
53+ apiClient .getApiClient ().setBearerToken (
54+ (accessToken != null && !accessToken .isEmpty ()) ? accessToken : null );
5855 }
5956
6057 @ Override
6158 public List <EntitlementDto > getAllEntitlements () {
6259 try {
6360 return getAllEntitlementsAsync ().get ();
64- } catch (InterruptedException | ExecutionException e ) {
61+ } catch (InterruptedException e ) {
6562 Thread .currentThread ().interrupt ();
6663 throw new RuntimeException ("Failed to get entitlements" , e );
64+ } catch (ExecutionException e ) {
65+ throw new RuntimeException ("Failed to get entitlements" , e );
6766 }
6867 }
6968
@@ -72,9 +71,11 @@ public EntitlementDto getEntitlement(String key) {
7271 responseHandler .validateKey (key , "entitlement" );
7372 try {
7473 return getEntitlementAsync (key ).get ();
75- } catch (InterruptedException | ExecutionException e ) {
74+ } catch (InterruptedException e ) {
7675 Thread .currentThread ().interrupt ();
7776 throw new RuntimeException ("Failed to get entitlement: " + key , e );
77+ } catch (ExecutionException e ) {
78+ throw new RuntimeException ("Failed to get entitlement: " + key , e );
7879 }
7980 }
8081
@@ -83,6 +84,7 @@ public CompletableFuture<List<EntitlementDto>> getAllEntitlementsAsync() {
8384 log .debug ("Getting all entitlements asynchronously" );
8485
8586 return CompletableFuture .supplyAsync (() -> {
87+ configureApiClient ();
8688 try {
8789 EntitlementsResponse allEntitlements = new EntitlementsResponse ();
8890 EntitlementsResponseData allData = new EntitlementsResponseData ();
@@ -142,6 +144,7 @@ public CompletableFuture<EntitlementDto> getEntitlementAsync(String key) {
142144 log .debug ("Getting entitlement with key: {} asynchronously" , key );
143145
144146 return CompletableFuture .supplyAsync (() -> {
147+ configureApiClient ();
145148 try {
146149 EntitlementResponse response = apiClient .getEntitlement (key );
147150
0 commit comments