Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: package/chopper_requester #14

Closed
wants to merge 6 commits into from

Conversation

techouse
Copy link

@techouse techouse commented Jun 30, 2024

Hi 👋

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

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
});

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)),
      ),
    );

Full disclosure: I'm one of the maintainers of Chopper.

@techouse
Copy link
Author

techouse commented Jul 1, 2024

Superseded by algolia/api-clients-automation#3291

@techouse techouse closed this Jul 1, 2024
@techouse techouse deleted the feat/chopper-requester branch July 1, 2024 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant