api-clients-automation icon indicating copy to clipboard operation
api-clients-automation copied to clipboard

feat(dart): add algolia_chopper_requester package

Open techouse opened this issue 1 year ago • 9 comments
trafficstars

🧭 What and Why

In this PR I have created a custom Requester based on Chopper.

Why? Dio is great, but if my Flutter app uses http I now have to have 2 different clients. 🙈 On top of that Chopper gives the user the option to provide their own Client.

final Requester chopperRequester = 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<String, dynamic>? headers,

  /// The segments to include in the `User-Agent` header
  Iterable<AgentSegment>? clientSegments,

  /// The logger to use for debugging
  Logger? logger,

  /// The Chopper Interceptors to use for modifying the request
  Iterable<Interceptor>? interceptors,

  /// The HTTP client to use for sending requests
  /// Will use https://pub.dev/packages/http by default
  /// Accepts any [Client], for example https://pub.dev/packages/cupertino_http
  /// or https://pub.dev/packages/cronet_http
  Client? client,

  /// A custom JSON converter to use for serializing and deserializing JSON
  JsonConverter? converter,
});

Basic Usage

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<SearchResponse> search(String query) => _client.searchIndex(
      request: SearchForHits(
        indexName: 'flutter',
        query: query,
        hitsPerPage: 5,
      ),
    );

Advanced Usage

To set the connect timeout one has to do that directly on the Client, i.e.

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.

Custom Clients

Via the client option users can use platform specific HTTP clients such:

  • cronet_http on Android
    final requester = ChopperRequester(
      appId: appId,
      apiKey: apiKey,
      client: CronetClient.fromCronetEngine(
        CronetEngine.build(
          cacheMode: CacheMode.memory,
          cacheMaxSize: 50 * 1024 * 1024,
        ),
        closeEngine: true,
      ),
    );
    
  • cupertino_http on iOS/macOS
    final requester = ChopperRequester(
      appId: appId,
      apiKey: apiKey,
      client: CupertinoClient.fromSessionConfiguration(
        (URLSessionConfiguration.defaultSessionConfiguration()
            ..timeoutIntervalForRequest = const Duration(seconds: 30)),
      ),
    );
    

NOTE: Custom HTTP clients must be manully disposed of, i.e.

final http.Client client = IOClient(
  HttpClient()..connectionTimeout = const Duration(seconds: 30),
);

/// ... use client ...

client.close();

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 on Decoding JSON using Isolate worker pools.

Changes included:

  • export AlgoliaAgent from algolia_client_core package
  • add new package algolia_chopper_requester

Full disclosure: I'm one of the maintainers of Chopper. Supersedes https://github.com/algolia/algoliasearch-client-dart/pull/14

techouse avatar Jul 01 '24 07:07 techouse