From d19989b11e5c890739999c5dfe191e77089ba3d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Mon, 1 Jul 2024 08:28:09 +0100 Subject: [PATCH 01/23] feat(dart): export AlgoliaAgent from algolia_client_core package --- .../packages/client_core/lib/algolia_client_core.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/clients/algoliasearch-client-dart/packages/client_core/lib/algolia_client_core.dart b/clients/algoliasearch-client-dart/packages/client_core/lib/algolia_client_core.dart index 530dd6f3a2..de415d28a4 100644 --- a/clients/algoliasearch-client-dart/packages/client_core/lib/algolia_client_core.dart +++ b/clients/algoliasearch-client-dart/packages/client_core/lib/algolia_client_core.dart @@ -10,6 +10,7 @@ export 'src/api_client.dart'; export 'src/config/agent_segment.dart'; export 'src/config/client_options.dart'; export 'src/config/host.dart'; +export 'src/transport/algolia_agent.dart'; export 'src/transport/api_request.dart'; export 'src/transport/request_options.dart'; export 'src/transport/requester.dart'; From 1a8bcf306475cb027d2c7fa4c8e6f365925a04f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Mon, 1 Jul 2024 08:29:28 +0100 Subject: [PATCH 02/23] feat(dart): add algolia_chopper_requester package --- .../packages/chopper_requester/.gitignore | 3 + .../packages/chopper_requester/CHANGELOG.md | 3 + .../packages/chopper_requester/LICENSE | 21 ++++ .../packages/chopper_requester/README.md | 117 ++++++++++++++++++ .../chopper_requester/analysis_options.yaml | 1 + .../lib/algolia_chopper_requester.dart | 4 + .../lib/src/agent_interceptor.dart | 21 ++++ .../lib/src/auth_interceptor.dart | 33 +++++ .../lib/src/chopper_requester.dart | 109 ++++++++++++++++ .../lib/src/platform/platform.dart | 17 +++ .../lib/src/platform/platform_io.dart | 20 +++ .../lib/src/platform/platform_stub.dart | 11 ++ .../lib/src/platform/platform_web.dart | 20 +++ .../chopper_requester/lib/src/version.dart | 2 + .../packages/chopper_requester/pubspec.yaml | 22 ++++ .../chopper_requester/test/version_test.dart | 18 +++ 16 files changed, 422 insertions(+) create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/.gitignore create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/CHANGELOG.md create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/LICENSE create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/README.md create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/analysis_options.yaml create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/lib/algolia_chopper_requester.dart create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/agent_interceptor.dart create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/auth_interceptor.dart create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform.dart create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform_io.dart create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform_stub.dart create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform_web.dart create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/version.dart create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/test/version_test.dart diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/.gitignore b/clients/algoliasearch-client-dart/packages/chopper_requester/.gitignore new file mode 100644 index 0000000000..3a85790408 --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/.gitignore @@ -0,0 +1,3 @@ +# https://dart.dev/guides/libraries/private-files +# Created by `dart pub` +.dart_tool/ diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/CHANGELOG.md b/clients/algoliasearch-client-dart/packages/chopper_requester/CHANGELOG.md new file mode 100644 index 0000000000..effe43c82c --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- Initial version. diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/LICENSE b/clients/algoliasearch-client-dart/packages/chopper_requester/LICENSE new file mode 100644 index 0000000000..02b32ea5cd --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Klemen Tusar + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/README.md b/clients/algoliasearch-client-dart/packages/chopper_requester/README.md new file mode 100644 index 0000000000..18850d191b --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/README.md @@ -0,0 +1,117 @@ +# Chopper Requester for Algolia Search Client + +## 💡 Installation + +Add Algolia Client Core as a dependency in your project directly from pub.dev: + +#### For Dart projects: + +```shell +dart pub add algolia_chopper_requester +``` + +#### For Flutter projects: + +```shell +flutter pub add algolia_chopper_requester +``` + +### Basic Usage + +```dart +final String appId = 'latency'; +final String apiKey = '6be0576ff61c053d5f9a3225e2a90f76'; + +final SearchClient _client = SearchClient( + appId: appId, + apiKey: apiKey, + options: ClientOptions( + requester: ChopperRequester( + appId: appId, + apiKey: apiKey, + ) + ), +); + +Future search(String query) => _client.searchIndex( + request: SearchForHits( + indexName: 'flutter', + query: query, + hitsPerPage: 5, + ), + ); +``` + +You can configure the `ChopperRequester` with the following parameters: + +### Configuration + +```dart +final requester = ChopperRequester({ + /// Your Algolia Application ID + required String appId, + /// Your Algolia Search-Only API Key + required String apiKey, + /// Additional headers to send with the request + Map? headers, + /// The segments to include in the `User-Agent` header + Iterable? clientSegments, + /// The logger to use for debugging + Logger? logger, + /// The Chopper Interceptors to use for modifying the request + Iterable? interceptors, + /// The HTTP client to use for sending requests + Client? client +}); +``` + +### Advanced Usage + +To set the connect timeout one has to do that directly on the `Client`, i.e. + +```dart +final requester = ChopperRequester( + appId: appId, + apiKey: apiKey, + client: http.IOClient( + HttpClient()..connectionTimeout = const Duration(seconds: 60), + ), +); +``` + +### Custom Interceptors + +For interceptors please see the [Chopper documentation](https://hadrien-lejard.gitbook.io/chopper/interceptors). + +### Custom Clients + +Via the `client` option users can use platform specific HTTP clients such: +- [cronet_http](https://pub.dev/packages/cronet_http) on Android + ```dart + final requester = ChopperRequester( + appId: appId, + apiKey: apiKey, + client: CronetClient.fromCronetEngine( + CronetEngine.build( + cacheMode: CacheMode.memory, + cacheMaxSize: 50 * 1024 * 1024, + ), + closeEngine: true, + ), + ); + ``` +- [cupertino_http](https://pub.dev/packages/cupertino_http) on iOS/macOS + ```dart + final requester = ChopperRequester( + appId: appId, + apiKey: apiKey, + client: CupertinoClient.fromSessionConfiguration( + (URLSessionConfiguration.defaultSessionConfiguration() + ..timeoutIntervalForRequest = const Duration(seconds: 30)), + ), + ); + ``` + +## License + +Chopper Requester for Algolia Search Client is an open-sourced software licensed under the [MIT license](LICENSE). diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/analysis_options.yaml b/clients/algoliasearch-client-dart/packages/chopper_requester/analysis_options.yaml new file mode 100644 index 0000000000..572dd239d0 --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/analysis_options.yaml @@ -0,0 +1 @@ +include: package:lints/recommended.yaml diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/algolia_chopper_requester.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/algolia_chopper_requester.dart new file mode 100644 index 0000000000..193b31ba8f --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/algolia_chopper_requester.dart @@ -0,0 +1,4 @@ +library algolia_chopper_requester; + +export 'package:chopper/chopper.dart' show Interceptor; +export 'src/chopper_requester.dart'; diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/agent_interceptor.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/agent_interceptor.dart new file mode 100644 index 0000000000..e2649535a5 --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/agent_interceptor.dart @@ -0,0 +1,21 @@ +import 'dart:async' show FutureOr; + +import 'package:algolia_client_core/algolia_client_core.dart' show AlgoliaAgent; +import 'package:chopper/chopper.dart'; +import 'package:algolia_chopper_requester/src/platform/platform.dart'; + +/// Interceptor that attaches the Algolia agent to outgoing requests. +/// +/// This interceptor modifies the query parameters of each request to include the +/// formatted representation of the Algolia agent. +class AgentInterceptor implements Interceptor { + /// The Algolia agent to be attached to outgoing requests. + final AlgoliaAgent agent; + + /// Constructs an [AgentInterceptor] with the provided Algolia agent. + const AgentInterceptor({required this.agent}); + + @override + FutureOr> intercept(Chain chain) => + chain.proceed(Platform.algoliaAgent(chain, agent.formatted())); +} diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/auth_interceptor.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/auth_interceptor.dart new file mode 100644 index 0000000000..d7ffa35185 --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/auth_interceptor.dart @@ -0,0 +1,33 @@ +import 'dart:async' show FutureOr; + +import 'package:chopper/chopper.dart'; + +/// Interceptor that attaches the application id and API key to outgoing requests. +/// +/// This interceptor modifies the headers of each request to include the +/// application id and API key for Algolia authentication. +class AuthInterceptor implements Interceptor { + /// The application id used for Algolia authentication. + final String appId; + + /// The API key used for Algolia authentication. + final String apiKey; + + /// Constructs an [AuthInterceptor] with the provided application id and API key. + const AuthInterceptor({ + required this.appId, + required this.apiKey, + }); + + @override + FutureOr> intercept(Chain chain) => + chain.proceed( + applyHeaders( + chain.request, + { + 'x-algolia-application-id': appId, + 'x-algolia-api-key': apiKey, + }, + ), + ); +} diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart new file mode 100644 index 0000000000..bdb0692976 --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart @@ -0,0 +1,109 @@ +import 'dart:async' show TimeoutException; + +import 'package:algolia_client_core/algolia_client_core.dart'; +import 'package:chopper/chopper.dart'; +import 'package:algolia_chopper_requester/src/agent_interceptor.dart'; +import 'package:algolia_chopper_requester/src/auth_interceptor.dart'; +import 'package:algolia_chopper_requester/src/platform/platform.dart'; +import 'package:algolia_chopper_requester/src/version.dart'; +import 'package:http/http.dart' as http; +import 'package:logging/logging.dart' show Logger; + +/// A [Requester] implementation using the Chopper library. +/// +/// This class sends HTTP requests using the Chopper library and handles +/// response conversion and error handling. +class ChopperRequester implements Requester { + /// The underlying Chopper client. + final ChopperClient _client; + + /// Constructs a [ChopperClient] with the given [appId] and [apiKey]. + ChopperRequester({ + required String appId, + required String apiKey, + Map? headers, + Iterable? clientSegments, + Logger? logger, + Iterable? interceptors, + http.Client? client, + }) : _client = ChopperClient( + client: client, + converter: JsonConverter(), + interceptors: [ + AuthInterceptor( + appId: appId, + apiKey: apiKey, + ), + AgentInterceptor( + agent: AlgoliaAgent(packageVersion) + ..addAll([ + ...?clientSegments, + ...Platform.agentSegments(), + ]), + ), + if (logger != null) + HttpLoggingInterceptor( + level: Level.body, + onlyErrors: false, + logger: logger, + ), + ...?interceptors, + ], + ); + + @override + Future perform(HttpRequest request) async { + try { + final Response> response = await execute(request); + + if (response.isSuccessful) { + return HttpResponse( + response.statusCode, + response.body, + ); + } else { + throw AlgoliaApiException( + response.statusCode, + response.error ?? response.body, + ); + } + } on TimeoutException catch (e) { + throw AlgoliaTimeoutException(e); + } on http.ClientException catch (e) { + throw AlgoliaIOException(e); + } + } + + /// Executes the [request] and returns the response as an [HttpResponse]. + Future>> execute(HttpRequest request) async { + final Request chopperRequest = Request( + request.method, + Uri( + scheme: request.host.scheme, + host: request.host.url, + port: request.host.port, + path: request.path, + ), + _client.baseUrl, + body: request.body, + parameters: request.queryParameters, + headers: { + for (final MapEntry entry + in request.headers?.entries ?? const {}) + entry.key: entry.value.toString(), + if (request.body != null) 'content-type': 'application/json', + }, + ); + + return switch (options.timeout) { + null => await _client + .send, Map>(chopperRequest), + _ => await _client + .send, Map>(chopperRequest) + .timeout(options.timeout!), + }; + } + + @override + void close() => _client.dispose(); +} diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform.dart new file mode 100644 index 0000000000..ed22645843 --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform.dart @@ -0,0 +1,17 @@ +import 'package:algolia_client_core/algolia_client_core.dart'; +import 'package:chopper/chopper.dart'; + +import 'platform_stub.dart' + if (dart.library.html) 'platform_web.dart' + if (dart.library.io) 'platform_io.dart'; + +final class Platform { + /// Get [AgentSegment]s for the current platform. + static Iterable agentSegments() => platformAgentSegments(); + + /// Set Algolia Agent as User-Agent or as query param depending on the platform. + static Request algoliaAgent(Chain chain, String agent) => + platformAlgoliaAgent(chain, agent); + + Platform._(); +} diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform_io.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform_io.dart new file mode 100644 index 0000000000..f57e393edc --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform_io.dart @@ -0,0 +1,20 @@ +import 'dart:io' as io; + +import 'package:algolia_client_core/algolia_client_core.dart'; +import 'package:chopper/chopper.dart'; + +/// [AgentSegment]s for native platforms. +Iterable platformAgentSegments() => [ + AgentSegment( + value: 'Dart', + version: io.Platform.version, + ), + AgentSegment( + value: io.Platform.operatingSystem, + version: io.Platform.operatingSystemVersion, + ), + ]; + +/// [AlgoliaAgent] for native platforms as user-agent. +Request platformAlgoliaAgent(Chain chain, String agent) => + applyHeader(chain.request, "user-agent", agent); diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform_stub.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform_stub.dart new file mode 100644 index 0000000000..5d425c5b68 --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform_stub.dart @@ -0,0 +1,11 @@ +import 'package:algolia_client_core/algolia_client_core.dart'; +import 'package:chopper/chopper.dart'; + +/// [AgentSegment]s for unsupported platforms. +Iterable platformAgentSegments() => const []; + +/// [AlgoliaAgent] for unsupported platforms. +Request platformAlgoliaAgent(Chain chain, String agent) { + // NO-OP. + return chain.request; +} diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform_web.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform_web.dart new file mode 100644 index 0000000000..85ed15c42e --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/platform/platform_web.dart @@ -0,0 +1,20 @@ +import 'dart:html' as web; + +import 'package:algolia_client_core/algolia_client_core.dart'; +import 'package:chopper/chopper.dart'; + +/// [AgentSegment]s for web platforms. +Iterable platformAgentSegments() => [ + AgentSegment( + value: 'Platform', + version: 'Web ${web.window.navigator.platform}', + ), + ]; + +Request platformAlgoliaAgent(Chain chain, String agent) => + chain.request.copyWith( + parameters: { + ...chain.request.parameters, + 'X-Algolia-Agent': agent, + }, + ); diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/version.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/version.dart new file mode 100644 index 0000000000..824ebd3bc7 --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/version.dart @@ -0,0 +1,2 @@ +/// Current package version +const packageVersion = '1.0.0'; diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml b/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml new file mode 100644 index 0000000000..4963b54f8a --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml @@ -0,0 +1,22 @@ +name: algolia_chopper_requester +description: Chopper Requester for Algolia Search Client +version: 1.0.0 +topics: + - search + - discovery + - http + - client + +environment: + sdk: ^3.0.0 + +dependencies: + algolia_client_core: ^1.15.1 + chopper: ^8.0.1+1 + http: ^1.1.0 + json_annotation: ^4.8.1 + logging: ^1.2.0 + +dev_dependencies: + lints: ^4.0.0 + test: ^1.25.7 diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/test/version_test.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/test/version_test.dart new file mode 100644 index 0000000000..f2b4c5fdbb --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/test/version_test.dart @@ -0,0 +1,18 @@ +import 'dart:io'; + +import 'package:algolia_client_core/src/version.dart'; +import 'package:test/test.dart'; + +void main() { + if (Directory.current.path.endsWith('/test')) { + Directory.current = Directory.current.parent; + } + test('package version matches pubspec', () { + final pubspecPath = '${Directory.current.path}/pubspec.yaml'; + final pubspec = File(pubspecPath).readAsStringSync(); + final regex = RegExp('version:s*(.*)'); + final match = regex.firstMatch(pubspec); + expect(match, isNotNull); + expect(packageVersion, match?.group(1)?.trim()); + }); +} From 47ef5f6503b0ae4ae3b59f0872ba8cab9748eafc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Mon, 1 Jul 2024 08:41:16 +0100 Subject: [PATCH 03/23] feat(dart): add algolia_chopper_requester package --- .../packages/chopper_requester/pubspec.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml b/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml index 4963b54f8a..45625d63f1 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml @@ -14,7 +14,6 @@ dependencies: algolia_client_core: ^1.15.1 chopper: ^8.0.1+1 http: ^1.1.0 - json_annotation: ^4.8.1 logging: ^1.2.0 dev_dependencies: From f7ad718e780c1e50df53f533fc723b046f4e61ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Mon, 1 Jul 2024 08:52:47 +0100 Subject: [PATCH 04/23] feat(dart): remove chopper_requester/CHANGELOG.md --- .../packages/chopper_requester/CHANGELOG.md | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/CHANGELOG.md diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/CHANGELOG.md b/clients/algoliasearch-client-dart/packages/chopper_requester/CHANGELOG.md deleted file mode 100644 index effe43c82c..0000000000 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/CHANGELOG.md +++ /dev/null @@ -1,3 +0,0 @@ -## 1.0.0 - -- Initial version. From f47674ef83ae4cfaa024bc49438b99701aa1a8bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Mon, 1 Jul 2024 08:57:22 +0100 Subject: [PATCH 05/23] feat(dart): update chopper_requester/README.md --- .../packages/chopper_requester/README.md | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/README.md b/clients/algoliasearch-client-dart/packages/chopper_requester/README.md index 18850d191b..cf3a9241b8 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/README.md +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/README.md @@ -1,8 +1,27 @@ -# Chopper Requester for Algolia Search Client + +

+ + Algolia for Dart + +

+ + +

+ Latest version + Publisher +

+ +

+ Algolia API Chopper Requester +

+ +

+ Use the Chopper Requester to send requests to the Algolia API with the Algolia Search Client for Dart. This package provides a custom Requester for the Algolia Search Client, allowing users to send requests to the Algolia API using the Chopper HTTP client. +

## 💡 Installation -Add Algolia Client Core as a dependency in your project directly from pub.dev: +Add Chopper Requester as a dependency in your project directly from pub.dev: #### For Dart projects: From 0e3c6ec5f97bf74de7dec40d44232949df7c5b9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Mon, 1 Jul 2024 08:58:15 +0100 Subject: [PATCH 06/23] feat(dart): remove chopper_requester/analysis_options.yaml --- .../packages/chopper_requester/analysis_options.yaml | 1 - 1 file changed, 1 deletion(-) delete mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/analysis_options.yaml diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/analysis_options.yaml b/clients/algoliasearch-client-dart/packages/chopper_requester/analysis_options.yaml deleted file mode 100644 index 572dd239d0..0000000000 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/analysis_options.yaml +++ /dev/null @@ -1 +0,0 @@ -include: package:lints/recommended.yaml From 28cd14157a2e8b6892853bfd7d474ea685276fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Mon, 1 Jul 2024 09:02:01 +0100 Subject: [PATCH 07/23] feat(dart): remove chopper_requester/LICENSE --- .../packages/chopper_requester/LICENSE | 21 ------------------- 1 file changed, 21 deletions(-) delete mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/LICENSE diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/LICENSE b/clients/algoliasearch-client-dart/packages/chopper_requester/LICENSE deleted file mode 100644 index 02b32ea5cd..0000000000 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2024 Klemen Tusar - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. From aab0906a99b553249f41219f82421a4add78bf59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Mon, 1 Jul 2024 09:03:42 +0100 Subject: [PATCH 08/23] feat(dart): sync chopper_requester/lib/src/version.dart --- .../packages/chopper_requester/lib/src/version.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/version.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/version.dart index 824ebd3bc7..cc73592955 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/version.dart +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/version.dart @@ -1,2 +1,2 @@ /// Current package version -const packageVersion = '1.0.0'; +const packageVersion = '1.15.1'; From d812167a58d39acfc587d088f84710062038d5bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Mon, 1 Jul 2024 09:05:14 +0100 Subject: [PATCH 09/23] feat(dart): add license generation --- .../src/main/java/com/algolia/codegen/AlgoliaDartGenerator.java | 1 + 1 file changed, 1 insertion(+) diff --git a/generators/src/main/java/com/algolia/codegen/AlgoliaDartGenerator.java b/generators/src/main/java/com/algolia/codegen/AlgoliaDartGenerator.java index ee6f2c4a8b..b710638fb6 100644 --- a/generators/src/main/java/com/algolia/codegen/AlgoliaDartGenerator.java +++ b/generators/src/main/java/com/algolia/codegen/AlgoliaDartGenerator.java @@ -101,6 +101,7 @@ public void processOpts() { supportingFiles.add(new SupportingFile("version.mustache", srcFolder, "version.dart")); supportingFiles.add(new SupportingFile("LICENSE", "", "LICENSE")); supportingFiles.add(new SupportingFile("LICENSE", "../client_core/", "LICENSE")); + supportingFiles.add(new SupportingFile("LICENSE", "../chopper_requester/", "LICENSE")); // Search config additionalProperties.put("isSearchClient", client.equals("search")); From c5fd54ca03e52759187383fbc625274561aa82dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Mon, 1 Jul 2024 10:51:14 +0100 Subject: [PATCH 10/23] feat(dart): add option to pass custom JsonConverter to ChopperRequester constructor --- .../packages/chopper_requester/README.md | 13 ++++++++++--- .../lib/src/chopper_requester.dart | 3 ++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/README.md b/clients/algoliasearch-client-dart/packages/chopper_requester/README.md index cf3a9241b8..67e8d567e3 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/README.md +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/README.md @@ -80,7 +80,9 @@ final requester = ChopperRequester({ /// The Chopper Interceptors to use for modifying the request Iterable? interceptors, /// The HTTP client to use for sending requests - Client? client + Client? client, + /// A custom JSON converter to use for serializing and deserializing JSON + JsonConverter? converter, }); ``` @@ -98,11 +100,11 @@ final requester = ChopperRequester( ); ``` -### Custom Interceptors +#### Custom Interceptors For interceptors please see the [Chopper documentation](https://hadrien-lejard.gitbook.io/chopper/interceptors). -### Custom Clients +#### Custom Clients Via the `client` option users can use platform specific HTTP clients such: - [cronet_http](https://pub.dev/packages/cronet_http) on Android @@ -130,6 +132,11 @@ Via the `client` option users can use platform specific HTTP clients such: ), ); ``` + +#### Parsing JSON in the background using Isolates + +Parsing JSON in the background is a good idea if you don't want to block the main thread. +Please see the [Chopper documentation](https://hadrien-lejard.gitbook.io/chopper/faq#decoding-json-using-isolates) on Decoding JSON using thread pool workers. ## License diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart index bdb0692976..bff41cfce3 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart @@ -26,9 +26,10 @@ class ChopperRequester implements Requester { Logger? logger, Iterable? interceptors, http.Client? client, + JsonConverter? converter, }) : _client = ChopperClient( client: client, - converter: JsonConverter(), + converter: converter ?? JsonConverter(), interceptors: [ AuthInterceptor( appId: appId, From 40cf2617e4eb68dcff95a3648f9e1897841ecb52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Mon, 1 Jul 2024 10:52:54 +0100 Subject: [PATCH 11/23] feat(dart): fix docs --- .../packages/chopper_requester/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/README.md b/clients/algoliasearch-client-dart/packages/chopper_requester/README.md index 67e8d567e3..573efd3c04 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/README.md +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/README.md @@ -136,7 +136,7 @@ Via the `client` option users can use platform specific HTTP clients such: #### Parsing JSON in the background using Isolates Parsing JSON in the background is a good idea if you don't want to block the main thread. -Please see the [Chopper documentation](https://hadrien-lejard.gitbook.io/chopper/faq#decoding-json-using-isolates) on Decoding JSON using thread pool workers. +Please see the [Chopper documentation](https://hadrien-lejard.gitbook.io/chopper/faq#decoding-json-using-isolates) on Decoding JSON using Isolate worker pools. ## License From f0a6e4f37984b8d5906f124e61f5a73f5bfad74e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Tue, 9 Jul 2024 07:43:07 +0100 Subject: [PATCH 12/23] feat(dart): fix URL construction --- .../lib/src/chopper_requester.dart | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart index bff41cfce3..fb2dd8691a 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart @@ -79,15 +79,9 @@ class ChopperRequester implements Requester { Future>> execute(HttpRequest request) async { final Request chopperRequest = Request( request.method, - Uri( - scheme: request.host.scheme, - host: request.host.url, - port: request.host.port, - path: request.path, - ), + requestUri(request), _client.baseUrl, body: request.body, - parameters: request.queryParameters, headers: { for (final MapEntry entry in request.headers?.entries ?? const {}) @@ -105,6 +99,22 @@ class ChopperRequester implements Requester { }; } + /// Constructs the request URI from the [request] details. + Uri requestUri(HttpRequest request) { + Uri uri = Uri( + scheme: request.host.scheme, + host: request.host.url, + port: request.host.port, + path: request.path, + ); + + return request.queryParameters.isNotEmpty + ? Uri.dataFromString( + "$uri?${request.queryParameters.entries.map((e) => "${e.key}=${e.value}").join("&")}", + ) + : uri; + } + @override void close() => _client.dispose(); } From 57dcfa0ccc6fc68a30fd7711c28659775ce1c153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Tue, 9 Jul 2024 08:01:55 +0100 Subject: [PATCH 13/23] feat(dart): explicitly set empty Uri as baseUri --- .../packages/chopper_requester/lib/src/chopper_requester.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart index fb2dd8691a..a280875d28 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart @@ -80,7 +80,7 @@ class ChopperRequester implements Requester { final Request chopperRequest = Request( request.method, requestUri(request), - _client.baseUrl, + Uri(), body: request.body, headers: { for (final MapEntry entry From 8b2a51d3593238e7a36103a2506c003133388141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Wed, 10 Jul 2024 22:27:00 +0100 Subject: [PATCH 14/23] feat(dart): bump algolia_client_core --- .../packages/chopper_requester/pubspec.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml b/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml index 45625d63f1..7444393218 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml @@ -11,11 +11,11 @@ environment: sdk: ^3.0.0 dependencies: - algolia_client_core: ^1.15.1 + algolia_client_core: ^1.17.0 chopper: ^8.0.1+1 http: ^1.1.0 logging: ^1.2.0 dev_dependencies: lints: ^4.0.0 - test: ^1.25.7 + test: ^1.25.8 From 00ba8c48770708af1aaeb71921c9c79b651a9ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Wed, 10 Jul 2024 22:38:35 +0100 Subject: [PATCH 15/23] feat(dart): downgrade test --- .../packages/chopper_requester/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml b/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml index 7444393218..01c03e8aa7 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml @@ -18,4 +18,4 @@ dependencies: dev_dependencies: lints: ^4.0.0 - test: ^1.25.8 + test: ^1.25.7 From 59329cd15285efeec50aad8f5cd5592cf9372c78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Wed, 10 Jul 2024 22:42:56 +0100 Subject: [PATCH 16/23] feat(dart): bump version --- .../packages/chopper_requester/lib/src/version.dart | 2 +- .../packages/chopper_requester/pubspec.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/version.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/version.dart index cc73592955..b507f011e6 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/version.dart +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/version.dart @@ -1,2 +1,2 @@ /// Current package version -const packageVersion = '1.15.1'; +const packageVersion = '1.17.0'; diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml b/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml index 01c03e8aa7..cff2f6badf 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/pubspec.yaml @@ -1,6 +1,6 @@ name: algolia_chopper_requester description: Chopper Requester for Algolia Search Client -version: 1.0.0 +version: 1.17.0 topics: - search - discovery From cdbaf2ec6489e4cecc4643adee61acda6b15443a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Wed, 10 Jul 2024 22:56:33 +0100 Subject: [PATCH 17/23] feat(dart): fix requestUri --- .../lib/src/chopper_requester.dart | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart index a280875d28..7bbad365f7 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart @@ -100,20 +100,17 @@ class ChopperRequester implements Requester { } /// Constructs the request URI from the [request] details. - Uri requestUri(HttpRequest request) { - Uri uri = Uri( - scheme: request.host.scheme, - host: request.host.url, - port: request.host.port, - path: request.path, - ); - - return request.queryParameters.isNotEmpty - ? Uri.dataFromString( - "$uri?${request.queryParameters.entries.map((e) => "${e.key}=${e.value}").join("&")}", - ) - : uri; - } + Uri requestUri(HttpRequest request) => Uri( + scheme: request.host.scheme, + host: request.host.url, + port: request.host.port, + path: request.path, + query: request.queryParameters.isNotEmpty + ? request.queryParameters.entries + .map((e) => "${e.key}=${e.value}") + .join("&") + : null, + ); @override void close() => _client.dispose(); From 2148ec0ff4817c3c201a053dd0ed7bbb2111b165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Wed, 10 Jul 2024 22:56:47 +0100 Subject: [PATCH 18/23] feat(dart): add example --- .../chopper_requester/example/example.dart | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart new file mode 100644 index 0000000000..81623eb311 --- /dev/null +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart @@ -0,0 +1,40 @@ +import 'package:algolia_chopper_requester/algolia_chopper_requester.dart'; +import 'package:algolia_client_core/algolia_client_core.dart'; + +void main() async { + const String appId = 'latency'; + const String apiKey = '6be0576ff61c053d5f9a3225e2a90f76'; + + // Creating an instance of the RetryStrategy with necessary parameters. + // This will retry the failed requests with a backoff strategy. + final requester = RetryStrategy.create( + segment: AgentSegment(value: 'CustomClient'), + appId: appId, + apiKey: apiKey, + defaultHosts: () => [ + Host(url: 'latency-dsn.algolia.net'), + Host(url: 'latency-1.algolianet.com'), + ], + options: ClientOptions( + requester: ChopperRequester( + appId: appId, + apiKey: apiKey, + ), + ), + ); + + // Executing a GET request on the '/1/indexes/instant_search' endpoint. + final response = await requester.execute( + request: ApiRequest( + method: RequestMethod.get, + path: '/1/indexes/instant_search', + queryParams: {'query': 'a', 'hitsPerPage': '5'}, + ), + ); + + // Printing json response. + print(response); + + // Dispose of the requester to clean up its resources. + requester.dispose(); +} From 9d25176102752f419f5c1c3fd814368c3d3933f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Thu, 11 Jul 2024 00:10:55 +0100 Subject: [PATCH 19/23] feat(dart): update example --- .../chopper_requester/example/example.dart | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart index 81623eb311..7f03b6250b 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart @@ -1,13 +1,22 @@ +import 'dart:io' show HttpClient; + import 'package:algolia_chopper_requester/algolia_chopper_requester.dart'; import 'package:algolia_client_core/algolia_client_core.dart'; +import 'package:http/http.dart' as http show Client; +import 'package:http/io_client.dart' show IOClient; void main() async { const String appId = 'latency'; const String apiKey = '6be0576ff61c053d5f9a3225e2a90f76'; + // Create a custom http [Client] with a connection timeout of 30 seconds + final http.Client client = IOClient( + HttpClient()..connectionTimeout = const Duration(seconds: 30), + ); + // Creating an instance of the RetryStrategy with necessary parameters. // This will retry the failed requests with a backoff strategy. - final requester = RetryStrategy.create( + final RetryStrategy requester = RetryStrategy.create( segment: AgentSegment(value: 'CustomClient'), appId: appId, apiKey: apiKey, @@ -19,12 +28,13 @@ void main() async { requester: ChopperRequester( appId: appId, apiKey: apiKey, + client: client, ), ), ); // Executing a GET request on the '/1/indexes/instant_search' endpoint. - final response = await requester.execute( + final Map response = await requester.execute( request: ApiRequest( method: RequestMethod.get, path: '/1/indexes/instant_search', @@ -37,4 +47,7 @@ void main() async { // Dispose of the requester to clean up its resources. requester.dispose(); + + // Disposing a custom client has to be done manually. + client.close(); } From 5e627e2c1e0961d8dc4898ac80da09eaa7712b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Thu, 11 Jul 2024 00:12:56 +0100 Subject: [PATCH 20/23] feat(dart): update example --- .../packages/chopper_requester/example/example.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart index 7f03b6250b..225570a414 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart @@ -28,6 +28,8 @@ void main() async { requester: ChopperRequester( appId: appId, apiKey: apiKey, + // Optionally, pass a custom http [Client] to the ChopperRequester. + // NOTE: The [Client] must be manually disposed of after use. client: client, ), ), From 581cee8cdadecc1dd3c5bc418d0e9bc48bfa3461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Thu, 11 Jul 2024 00:13:19 +0100 Subject: [PATCH 21/23] feat(dart): update example --- .../packages/chopper_requester/example/example.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart index 225570a414..155f7c2679 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/example/example.dart @@ -50,6 +50,6 @@ void main() async { // Dispose of the requester to clean up its resources. requester.dispose(); - // Disposing a custom client has to be done manually. + // Disposing a custom [Client] has to be done manually. client.close(); } From ebc76f9f1a5570f205d29c4cc91d0c2f49936ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Mon, 9 Sep 2024 21:02:09 +0100 Subject: [PATCH 22/23] feat(dart): implement setClientApiKey --- .../lib/src/auth_interceptor.dart | 8 +-- .../lib/src/chopper_requester.dart | 61 +++++++++++-------- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/auth_interceptor.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/auth_interceptor.dart index d7ffa35185..fdd2018ad0 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/auth_interceptor.dart +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/auth_interceptor.dart @@ -11,13 +11,13 @@ class AuthInterceptor implements Interceptor { final String appId; /// The API key used for Algolia authentication. - final String apiKey; + final String _apiKey; /// Constructs an [AuthInterceptor] with the provided application id and API key. const AuthInterceptor({ required this.appId, - required this.apiKey, - }); + required String apiKey, + }) : _apiKey = apiKey; @override FutureOr> intercept(Chain chain) => @@ -26,7 +26,7 @@ class AuthInterceptor implements Interceptor { chain.request, { 'x-algolia-application-id': appId, - 'x-algolia-api-key': apiKey, + 'x-algolia-api-key': _apiKey, }, ), ); diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart index 7bbad365f7..d6e70f1f55 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart @@ -15,7 +15,8 @@ import 'package:logging/logging.dart' show Logger; /// response conversion and error handling. class ChopperRequester implements Requester { /// The underlying Chopper client. - final ChopperClient _client; + AuthInterceptor _authInterceptor; + late final ChopperClient _client; /// Constructs a [ChopperClient] with the given [appId] and [apiKey]. ChopperRequester({ @@ -27,30 +28,32 @@ class ChopperRequester implements Requester { Iterable? interceptors, http.Client? client, JsonConverter? converter, - }) : _client = ChopperClient( - client: client, - converter: converter ?? JsonConverter(), - interceptors: [ - AuthInterceptor( - appId: appId, - apiKey: apiKey, - ), - AgentInterceptor( - agent: AlgoliaAgent(packageVersion) - ..addAll([ - ...?clientSegments, - ...Platform.agentSegments(), - ]), - ), - if (logger != null) - HttpLoggingInterceptor( - level: Level.body, - onlyErrors: false, - logger: logger, - ), - ...?interceptors, - ], - ); + }) : _authInterceptor = AuthInterceptor( + appId: appId, + apiKey: apiKey, + ) { + _client = ChopperClient( + client: client, + converter: converter ?? JsonConverter(), + interceptors: [ + _authInterceptor, + AgentInterceptor( + agent: AlgoliaAgent(packageVersion) + ..addAll([ + ...?clientSegments, + ...Platform.agentSegments(), + ]), + ), + if (logger != null) + HttpLoggingInterceptor( + level: Level.body, + onlyErrors: false, + logger: logger, + ), + ...?interceptors, + ], + ); + } @override Future perform(HttpRequest request) async { @@ -114,4 +117,12 @@ class ChopperRequester implements Requester { @override void close() => _client.dispose(); + + @override + void setClientApiKey(String apiKey) { + _authInterceptor = AuthInterceptor( + appId: _authInterceptor.appId, + apiKey: apiKey, + ); + } } From b5fab80d21d5597fb3fb6027ffa6d070d80399f4 Mon Sep 17 00:00:00 2001 From: Klemen Tusar Date: Tue, 26 Nov 2024 12:37:44 +0000 Subject: [PATCH 23/23] Update chopper_requester.dart --- .../packages/chopper_requester/lib/src/chopper_requester.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart index d6e70f1f55..3aab660d82 100644 --- a/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart +++ b/clients/algoliasearch-client-dart/packages/chopper_requester/lib/src/chopper_requester.dart @@ -81,7 +81,7 @@ class ChopperRequester implements Requester { /// Executes the [request] and returns the response as an [HttpResponse]. Future>> execute(HttpRequest request) async { final Request chopperRequest = Request( - request.method, + request.method.toUpperCase(), requestUri(request), Uri(), body: request.body,