Skip to content

Commit

Permalink
feat(clients): helper to switch API key in use (generated)
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#3616

Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com>
Co-authored-by: Thomas Raffray <Fluf22@users.noreply.github.com>
Co-authored-by: Pierre Millot <pierre.millot@algolia.com>
  • Loading branch information
3 people committed Sep 6, 2024
1 parent 8dcbc45 commit 388689a
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 62 deletions.
16 changes: 8 additions & 8 deletions packages/algoliasearch/lib/src/api/search_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,14 @@ import 'package:algoliasearch/src/model/search_method_params.dart';
import 'package:algoliasearch/src/model/search_responses.dart';

final class SearchClient implements ApiClient {
@override
final String apiKey;

@override
final String appId;

@override
final ClientOptions options;

final RetryStrategy _retryStrategy;

SearchClient({
required this.appId,
required this.apiKey,
required String appId,
required String apiKey,
this.options = const ClientOptions(),
}) : _retryStrategy = RetryStrategy.create(
segment:
Expand All @@ -46,6 +40,12 @@ final class SearchClient implements ApiClient {
assert(apiKey.isNotEmpty, '`apiKey` is missing.');
}

/// Allows to switch the API key used to authenticate requests.
@override
void setClientApiKey({required String apiKey}) {
_retryStrategy.requester.setClientApiKey(apiKey);
}

/// This method allow you to send requests to the Algolia REST API.
///
/// Parameters:
Expand Down
9 changes: 3 additions & 6 deletions packages/client_core/lib/src/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import 'package:algolia_client_core/src/config/client_options.dart';

/// An abstract class representing an API client with specific properties and options.
abstract interface class ApiClient {
/// The unique identifier for the application using the API client.
String get appId;

/// The API key used for authentication.
String get apiKey;

/// A set of custom client options to configure the behavior of the API client.
ClientOptions get options;

/// Allow switching the API key used to authenticate requests.
void setClientApiKey({required String apiKey});

/// Dispose of underlying resources.
void dispose();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class AuthInterceptor extends Interceptor {
final String appId;

/// The API key used for Algolia authentication.
final String apiKey;
String apiKey;

/// Constructs an [AuthInterceptor] with the provided application id and API key.
AuthInterceptor({
Expand Down
53 changes: 30 additions & 23 deletions packages/client_core/lib/src/transport/dio/dio_requester.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import 'package:dio/dio.dart';
/// response conversion and error handling.
class DioRequester implements Requester {
/// The underlying Dio client.
final Dio _client;
final AuthInterceptor _authInterceptor;
late final Dio _client;

/// Constructs a [DioRequester] with the given [appId], [apiKey], and [options].
DioRequester({
Expand All @@ -28,29 +29,30 @@ class DioRequester implements Requester {
Function(Object?)? logger,
Iterable<Interceptor>? interceptors,
HttpClientAdapter? httpClientAdapter,
}) : _client = Dio(
BaseOptions(
headers: headers,
connectTimeout: connectTimeout,
}) : _authInterceptor = AuthInterceptor(
appId: appId,
apiKey: apiKey,
) {
_client = Dio(
BaseOptions(
headers: headers,
connectTimeout: connectTimeout,
),
)..interceptors.addAll([
_authInterceptor,
AgentInterceptor(
agent: AlgoliaAgent(packageVersion)
..addAll(clientSegments ?? const [])
..addAll(Platform.agentSegments()),
),
if (logger != null)
LogInterceptor(
requestBody: true,
responseBody: true,
logPrint: logger,
),
)..interceptors.addAll([
AuthInterceptor(
appId: appId,
apiKey: apiKey,
),
AgentInterceptor(
agent: AlgoliaAgent(packageVersion)
..addAll(clientSegments ?? const [])
..addAll(Platform.agentSegments()),
),
if (logger != null)
LogInterceptor(
requestBody: true,
responseBody: true,
logPrint: logger,
),
if (interceptors != null) ...interceptors,
]) {
if (interceptors != null) ...interceptors,
]);
if (httpClientAdapter != null) {
_client.httpClientAdapter = httpClientAdapter;
}
Expand Down Expand Up @@ -114,4 +116,9 @@ class DioRequester implements Requester {

@override
void close() => _client.close();

@override
void setClientApiKey(String apiKey) {
_authInterceptor.apiKey = apiKey;
}
}
3 changes: 3 additions & 0 deletions packages/client_core/lib/src/transport/requester.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ abstract class Requester {
/// The method returns a Future that resolves to an [HttpResponse].
Future<HttpResponse> perform(HttpRequest request);

/// Allows to switch the API key used to authenticate requests.
void setClientApiKey(String apiKey);

/// Closes any underlying resources that the Requester might be using.
///
/// By default, it does nothing (no-op), but it can be implemented to handle resource cleanup
Expand Down
16 changes: 8 additions & 8 deletions packages/client_insights/lib/src/api/insights_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import 'package:algolia_client_insights/src/model/events_response.dart';
import 'package:algolia_client_insights/src/model/insights_events.dart';

final class InsightsClient implements ApiClient {
@override
final String apiKey;

@override
final String appId;

@override
final ClientOptions options;

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

InsightsClient({
required this.appId,
required this.apiKey,
required String appId,
required String apiKey,
this.options = const ClientOptions(),
this.region,
}) : _retryStrategy = RetryStrategy.create(
Expand All @@ -47,6 +41,12 @@ final class InsightsClient implements ApiClient {
assert(apiKey.isNotEmpty, '`apiKey` is missing.');
}

/// Allows to switch the API key used to authenticate requests.
@override
void setClientApiKey({required String apiKey}) {
_retryStrategy.requester.setClientApiKey(apiKey);
}

/// This method allow you to send requests to the Algolia REST API.
///
/// Parameters:
Expand Down
16 changes: 8 additions & 8 deletions packages/client_recommend/lib/src/api/recommend_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@ import 'package:algolia_client_recommend/src/model/search_recommend_rules_params
import 'package:algolia_client_recommend/src/model/search_recommend_rules_response.dart';

final class RecommendClient implements ApiClient {
@override
final String apiKey;

@override
final String appId;

@override
final ClientOptions options;

final RetryStrategy _retryStrategy;

RecommendClient({
required this.appId,
required this.apiKey,
required String appId,
required String apiKey,
this.options = const ClientOptions(),
}) : _retryStrategy = RetryStrategy.create(
segment: AgentSegment(value: "Recommend", version: packageVersion),
Expand All @@ -49,6 +43,12 @@ final class RecommendClient implements ApiClient {
assert(apiKey.isNotEmpty, '`apiKey` is missing.');
}

/// Allows to switch the API key used to authenticate requests.
@override
void setClientApiKey({required String apiKey}) {
_retryStrategy.requester.setClientApiKey(apiKey);
}

/// This method allow you to send requests to the Algolia REST API.
///
/// Parameters:
Expand Down
16 changes: 8 additions & 8 deletions packages/client_search/lib/src/api/search_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,14 @@ import 'package:algolia_client_search/src/model/updated_rule_response.dart';
import 'package:algolia_client_search/src/model/user_id.dart';

final class SearchClient implements ApiClient {
@override
final String apiKey;

@override
final String appId;

@override
final ClientOptions options;

final RetryStrategy _retryStrategy;

SearchClient({
required this.appId,
required this.apiKey,
required String appId,
required String apiKey,
this.options = const ClientOptions(),
}) : _retryStrategy = RetryStrategy.create(
segment: AgentSegment(value: "Search", version: packageVersion),
Expand All @@ -101,6 +95,12 @@ final class SearchClient implements ApiClient {
assert(apiKey.isNotEmpty, '`apiKey` is missing.');
}

/// Allows to switch the API key used to authenticate requests.
@override
void setClientApiKey({required String apiKey}) {
_retryStrategy.requester.setClientApiKey(apiKey);
}

/// Creates a new API key with specific permissions and restrictions.
///
/// Required API Key ACLs:
Expand Down

0 comments on commit 388689a

Please sign in to comment.