Translator constructor

Translator({
  1. required String authKey,
  2. String? serverUrl,
  3. Map<String, String>? headers,
  4. int maxRetries = 5,
})

Construct a Translator object wrapping the DeepL API using your authentication key.

This does not connect to the API, and returns immediately.

Takes authKey as specified in your account.

Takes serverUrl as base URL of DeepL API, can be overridden for example for testing purposes. By default, the correct DeepL API URL is selected based on the user account type (free or paid).

Takes HTTP headers attached to every HTTP request. By default, no extra headers are used. Note that during Translator initialization headers for Authorization and User-Agent are added, unless they are overridden in this option.

Takes maxRetries for the maximum number of failed attempts that Translator will retry, per request. By default, 5 retries are made. Note: only errors due to transient conditions are retried.

Implementation

Translator({
  required String authKey,
  String? serverUrl,
  Map<String, String>? headers,
  int maxRetries = 5,
}) {
  assert(authKey.isNotEmpty, 'authKey must be a non-empty string');
  if (serverUrl != null) {
    _serverUrl = serverUrl;
  } else if (isFreeAccountAuthKey(authKey)) {
    _serverUrl = 'https://api-free.deepl.com';
  } else {
    _serverUrl = 'https://api.deepl.com';
  }
  _headers = {
    'Authorization': 'DeepL-Auth-Key $authKey',
    'User-Agent': 'deepl_dart/1.4.0',
    ...(headers ?? {}),
  };
  _httpClient = RetryClient(http.Client(), retries: maxRetries);
}