Skip to content

Commit ebc76f9

Browse files
committed
feat(dart): implement setClientApiKey
1 parent 1cb13bf commit ebc76f9

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/auth_interceptor.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ class AuthInterceptor implements Interceptor {
1111
final String appId;
1212

1313
/// The API key used for Algolia authentication.
14-
final String apiKey;
14+
final String _apiKey;
1515

1616
/// Constructs an [AuthInterceptor] with the provided application id and API key.
1717
const AuthInterceptor({
1818
required this.appId,
19-
required this.apiKey,
20-
});
19+
required String apiKey,
20+
}) : _apiKey = apiKey;
2121

2222
@override
2323
FutureOr<Response<BodyType>> intercept<BodyType>(Chain<BodyType> chain) =>
@@ -26,7 +26,7 @@ class AuthInterceptor implements Interceptor {
2626
chain.request,
2727
{
2828
'x-algolia-application-id': appId,
29-
'x-algolia-api-key': apiKey,
29+
'x-algolia-api-key': _apiKey,
3030
},
3131
),
3232
);

clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import 'package:logging/logging.dart' show Logger;
1515
/// response conversion and error handling.
1616
class ChopperRequester implements Requester {
1717
/// The underlying Chopper client.
18-
final ChopperClient _client;
18+
AuthInterceptor _authInterceptor;
19+
late final ChopperClient _client;
1920

2021
/// Constructs a [ChopperClient] with the given [appId] and [apiKey].
2122
ChopperRequester({
@@ -27,30 +28,32 @@ class ChopperRequester implements Requester {
2728
Iterable<Interceptor>? interceptors,
2829
http.Client? client,
2930
JsonConverter? converter,
30-
}) : _client = ChopperClient(
31-
client: client,
32-
converter: converter ?? JsonConverter(),
33-
interceptors: [
34-
AuthInterceptor(
35-
appId: appId,
36-
apiKey: apiKey,
37-
),
38-
AgentInterceptor(
39-
agent: AlgoliaAgent(packageVersion)
40-
..addAll([
41-
...?clientSegments,
42-
...Platform.agentSegments(),
43-
]),
44-
),
45-
if (logger != null)
46-
HttpLoggingInterceptor(
47-
level: Level.body,
48-
onlyErrors: false,
49-
logger: logger,
50-
),
51-
...?interceptors,
52-
],
53-
);
31+
}) : _authInterceptor = AuthInterceptor(
32+
appId: appId,
33+
apiKey: apiKey,
34+
) {
35+
_client = ChopperClient(
36+
client: client,
37+
converter: converter ?? JsonConverter(),
38+
interceptors: [
39+
_authInterceptor,
40+
AgentInterceptor(
41+
agent: AlgoliaAgent(packageVersion)
42+
..addAll([
43+
...?clientSegments,
44+
...Platform.agentSegments(),
45+
]),
46+
),
47+
if (logger != null)
48+
HttpLoggingInterceptor(
49+
level: Level.body,
50+
onlyErrors: false,
51+
logger: logger,
52+
),
53+
...?interceptors,
54+
],
55+
);
56+
}
5457

5558
@override
5659
Future<HttpResponse> perform(HttpRequest request) async {
@@ -114,4 +117,12 @@ class ChopperRequester implements Requester {
114117

115118
@override
116119
void close() => _client.dispose();
120+
121+
@override
122+
void setClientApiKey(String apiKey) {
123+
_authInterceptor = AuthInterceptor(
124+
appId: _authInterceptor.appId,
125+
apiKey: apiKey,
126+
);
127+
}
117128
}

0 commit comments

Comments
 (0)