Skip to content

Commit 388689a

Browse files
algolia-botFluf22millotp
committed
feat(clients): helper to switch API key in use (generated)
algolia/api-clients-automation#3616 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Thomas Raffray <[email protected]> Co-authored-by: Pierre Millot <[email protected]>
1 parent 8dcbc45 commit 388689a

File tree

8 files changed

+69
-62
lines changed

8 files changed

+69
-62
lines changed

packages/algoliasearch/lib/src/api/search_client.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,14 @@ import 'package:algoliasearch/src/model/search_method_params.dart';
1010
import 'package:algoliasearch/src/model/search_responses.dart';
1111

1212
final class SearchClient implements ApiClient {
13-
@override
14-
final String apiKey;
15-
16-
@override
17-
final String appId;
18-
1913
@override
2014
final ClientOptions options;
2115

2216
final RetryStrategy _retryStrategy;
2317

2418
SearchClient({
25-
required this.appId,
26-
required this.apiKey,
19+
required String appId,
20+
required String apiKey,
2721
this.options = const ClientOptions(),
2822
}) : _retryStrategy = RetryStrategy.create(
2923
segment:
@@ -46,6 +40,12 @@ final class SearchClient implements ApiClient {
4640
assert(apiKey.isNotEmpty, '`apiKey` is missing.');
4741
}
4842

43+
/// Allows to switch the API key used to authenticate requests.
44+
@override
45+
void setClientApiKey({required String apiKey}) {
46+
_retryStrategy.requester.setClientApiKey(apiKey);
47+
}
48+
4949
/// This method allow you to send requests to the Algolia REST API.
5050
///
5151
/// Parameters:

packages/client_core/lib/src/api_client.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ import 'package:algolia_client_core/src/config/client_options.dart';
22

33
/// An abstract class representing an API client with specific properties and options.
44
abstract interface class ApiClient {
5-
/// The unique identifier for the application using the API client.
6-
String get appId;
7-
8-
/// The API key used for authentication.
9-
String get apiKey;
10-
115
/// A set of custom client options to configure the behavior of the API client.
126
ClientOptions get options;
137

8+
/// Allow switching the API key used to authenticate requests.
9+
void setClientApiKey({required String apiKey});
10+
1411
/// Dispose of underlying resources.
1512
void dispose();
1613
}

packages/client_core/lib/src/transport/dio/auth_interceptor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class AuthInterceptor extends Interceptor {
99
final String appId;
1010

1111
/// The API key used for Algolia authentication.
12-
final String apiKey;
12+
String apiKey;
1313

1414
/// Constructs an [AuthInterceptor] with the provided application id and API key.
1515
AuthInterceptor({

packages/client_core/lib/src/transport/dio/dio_requester.dart

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import 'package:dio/dio.dart';
1616
/// response conversion and error handling.
1717
class DioRequester implements Requester {
1818
/// The underlying Dio client.
19-
final Dio _client;
19+
final AuthInterceptor _authInterceptor;
20+
late final Dio _client;
2021

2122
/// Constructs a [DioRequester] with the given [appId], [apiKey], and [options].
2223
DioRequester({
@@ -28,29 +29,30 @@ class DioRequester implements Requester {
2829
Function(Object?)? logger,
2930
Iterable<Interceptor>? interceptors,
3031
HttpClientAdapter? httpClientAdapter,
31-
}) : _client = Dio(
32-
BaseOptions(
33-
headers: headers,
34-
connectTimeout: connectTimeout,
32+
}) : _authInterceptor = AuthInterceptor(
33+
appId: appId,
34+
apiKey: apiKey,
35+
) {
36+
_client = Dio(
37+
BaseOptions(
38+
headers: headers,
39+
connectTimeout: connectTimeout,
40+
),
41+
)..interceptors.addAll([
42+
_authInterceptor,
43+
AgentInterceptor(
44+
agent: AlgoliaAgent(packageVersion)
45+
..addAll(clientSegments ?? const [])
46+
..addAll(Platform.agentSegments()),
47+
),
48+
if (logger != null)
49+
LogInterceptor(
50+
requestBody: true,
51+
responseBody: true,
52+
logPrint: logger,
3553
),
36-
)..interceptors.addAll([
37-
AuthInterceptor(
38-
appId: appId,
39-
apiKey: apiKey,
40-
),
41-
AgentInterceptor(
42-
agent: AlgoliaAgent(packageVersion)
43-
..addAll(clientSegments ?? const [])
44-
..addAll(Platform.agentSegments()),
45-
),
46-
if (logger != null)
47-
LogInterceptor(
48-
requestBody: true,
49-
responseBody: true,
50-
logPrint: logger,
51-
),
52-
if (interceptors != null) ...interceptors,
53-
]) {
54+
if (interceptors != null) ...interceptors,
55+
]);
5456
if (httpClientAdapter != null) {
5557
_client.httpClientAdapter = httpClientAdapter;
5658
}
@@ -114,4 +116,9 @@ class DioRequester implements Requester {
114116

115117
@override
116118
void close() => _client.close();
119+
120+
@override
121+
void setClientApiKey(String apiKey) {
122+
_authInterceptor.apiKey = apiKey;
123+
}
117124
}

packages/client_core/lib/src/transport/requester.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ abstract class Requester {
1111
/// The method returns a Future that resolves to an [HttpResponse].
1212
Future<HttpResponse> perform(HttpRequest request);
1313

14+
/// Allows to switch the API key used to authenticate requests.
15+
void setClientApiKey(String apiKey);
16+
1417
/// Closes any underlying resources that the Requester might be using.
1518
///
1619
/// By default, it does nothing (no-op), but it can be implemented to handle resource cleanup

packages/client_insights/lib/src/api/insights_client.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ import 'package:algolia_client_insights/src/model/events_response.dart';
88
import 'package:algolia_client_insights/src/model/insights_events.dart';
99

1010
final class InsightsClient implements ApiClient {
11-
@override
12-
final String apiKey;
13-
14-
@override
15-
final String appId;
16-
1711
@override
1812
final ClientOptions options;
1913

@@ -22,8 +16,8 @@ final class InsightsClient implements ApiClient {
2216
final RetryStrategy _retryStrategy;
2317

2418
InsightsClient({
25-
required this.appId,
26-
required this.apiKey,
19+
required String appId,
20+
required String apiKey,
2721
this.options = const ClientOptions(),
2822
this.region,
2923
}) : _retryStrategy = RetryStrategy.create(
@@ -47,6 +41,12 @@ final class InsightsClient implements ApiClient {
4741
assert(apiKey.isNotEmpty, '`apiKey` is missing.');
4842
}
4943

44+
/// Allows to switch the API key used to authenticate requests.
45+
@override
46+
void setClientApiKey({required String apiKey}) {
47+
_retryStrategy.requester.setClientApiKey(apiKey);
48+
}
49+
5050
/// This method allow you to send requests to the Algolia REST API.
5151
///
5252
/// Parameters:

packages/client_recommend/lib/src/api/recommend_client.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,14 @@ import 'package:algolia_client_recommend/src/model/search_recommend_rules_params
1414
import 'package:algolia_client_recommend/src/model/search_recommend_rules_response.dart';
1515

1616
final class RecommendClient implements ApiClient {
17-
@override
18-
final String apiKey;
19-
20-
@override
21-
final String appId;
22-
2317
@override
2418
final ClientOptions options;
2519

2620
final RetryStrategy _retryStrategy;
2721

2822
RecommendClient({
29-
required this.appId,
30-
required this.apiKey,
23+
required String appId,
24+
required String apiKey,
3125
this.options = const ClientOptions(),
3226
}) : _retryStrategy = RetryStrategy.create(
3327
segment: AgentSegment(value: "Recommend", version: packageVersion),
@@ -49,6 +43,12 @@ final class RecommendClient implements ApiClient {
4943
assert(apiKey.isNotEmpty, '`apiKey` is missing.');
5044
}
5145

46+
/// Allows to switch the API key used to authenticate requests.
47+
@override
48+
void setClientApiKey({required String apiKey}) {
49+
_retryStrategy.requester.setClientApiKey(apiKey);
50+
}
51+
5252
/// This method allow you to send requests to the Algolia REST API.
5353
///
5454
/// Parameters:

packages/client_search/lib/src/api/search_client.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,14 @@ import 'package:algolia_client_search/src/model/updated_rule_response.dart';
6666
import 'package:algolia_client_search/src/model/user_id.dart';
6767

6868
final class SearchClient implements ApiClient {
69-
@override
70-
final String apiKey;
71-
72-
@override
73-
final String appId;
74-
7569
@override
7670
final ClientOptions options;
7771

7872
final RetryStrategy _retryStrategy;
7973

8074
SearchClient({
81-
required this.appId,
82-
required this.apiKey,
75+
required String appId,
76+
required String apiKey,
8377
this.options = const ClientOptions(),
8478
}) : _retryStrategy = RetryStrategy.create(
8579
segment: AgentSegment(value: "Search", version: packageVersion),
@@ -101,6 +95,12 @@ final class SearchClient implements ApiClient {
10195
assert(apiKey.isNotEmpty, '`apiKey` is missing.');
10296
}
10397

98+
/// Allows to switch the API key used to authenticate requests.
99+
@override
100+
void setClientApiKey({required String apiKey}) {
101+
_retryStrategy.requester.setClientApiKey(apiKey);
102+
}
103+
104104
/// Creates a new API key with specific permissions and restrictions.
105105
///
106106
/// Required API Key ACLs:

0 commit comments

Comments
 (0)