Class: DeepL::AdminClient

Inherits:
Object
  • Object
show all
Defined in:
lib/deepl/admin_client.rb

Overview

Client for the DeepL Admin API, used to manage the developer keys of your organization and to retrieve usage analytics. It owns its configuration and reads no environment variables.

All methods may raise a DeepL::Exceptions::Error or a subclass, for example DeepL::Exceptions::AuthorizationFailed if the admin key is not accepted.

Instance Method Summary collapse

Constructor Details

#initialize(admin_key, server_url: nil, headers: {}, max_retries: 5, timeout: 10, proxy: nil, send_platform_info: true, app_info: nil, http_client_options: nil, logger: nil) ⇒ AdminClient

A keyword argument given as nil takes its default, like an omitted one.

Parameters:

  • admin_key (String) —

    Admin API key, surrounding whitespace is removed.

  • server_url (String, nil) (defaults to: nil) —

    Base URL of the DeepL API, an absolute http or https URL with a host; trailing slashes are removed. Defaults to https://api.deepl.com (also for keys ending in :fx).

  • headers (Hash) (defaults to: {}) —

    HTTP headers sent with every request. A header of the same name in any casing replaces the library's, Authorization and User-Agent included, except the technical headers Content-Type, Content-Length, Transfer-Encoding and Host.

  • max_retries (Integer, nil) (defaults to: 5) —

    Number of retries after the first attempt.

  • timeout (Numeric, nil) (defaults to: 10) —

    Timeout of each attempt in seconds.

  • proxy (String, URI, Hash, nil) (defaults to: nil) —

    Proxy URL (+http+ or https scheme) as a String or URI, e.g. http://user:[email protected]:8080, or a hash with proxy_addr and proxy_port (string or symbol keys, both required), which is a plain HTTP proxy. An https proxy is connected to over TLS and needs net-http 0.5.0 or later (bundled from Ruby 3.4) and an https server URL.

  • send_platform_info (Boolean, nil) (defaults to: true) —

    Whether to send basic platform information.

  • app_info (DeepL::AppInfo, Hash, nil) (defaults to: nil) —

    Your application's app_name and app_version, added to the User-Agent.

  • http_client_options (DeepL::HTTPClientOptions, nil) (defaults to: nil) —

    Proxy, SSL and timeout options.

  • logger (Logger, nil) (defaults to: nil) —

    Logger for requests and retries.

Raises:

  • (ArgumentError) —

    If admin_key is empty, server_url is not an absolute http or https URL with a host, or proxy is not a valid proxy URL, URI or proxy hash (an empty host or blank proxy_addr is not valid), has a proxy URL scheme other than http or https, or is an https proxy that cannot be used (see proxy).



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/deepl/admin_client.rb', line 45

def initialize(admin_key, server_url: nil, headers: {}, max_retries: 5, timeout: 10, # rubocop:disable Metrics/ParameterLists
               proxy: nil, send_platform_info: true, app_info: nil, http_client_options: nil,
               logger: nil)
  @api = Utils::ClientBuilder.build_api(
    admin_key, key_name: 'admin_key', server_url: server_url,
               default_server_url: DEEPL_SERVER_URL, headers: headers,
               max_retries: max_retries, timeout: timeout, proxy: proxy,
               send_platform_info: send_platform_info, app_info: app_info,
               http_client_options: http_client_options, logger: logger
  )
end

Instance Method Details

#create_developer_key(label: nil, extra_body_parameters: nil) ⇒ DeepL::Resources::DeveloperKey

Creates a developer key. The API uses "DeepL API Key" as label if none is given.

Creation is retried only after failures that show the request was not processed (HTTP 429 and transient network failures while connecting, such as DNS resolution errors, refused connections, unreachable hosts or networks, connection timeouts and resets during the TLS or proxy handshake), so that a retry cannot create a second key. The raised error has should_retry? true for these and for a response body cut off mid-read, which is not retried.

Parameters:

  • label (String, nil) (defaults to: nil) —

    Label for the new key, not sent if nil.

  • extra_body_parameters (Hash, nil) (defaults to: nil) —

    Added to the request body last; replaces fields of the same name. Values are sent as given.

Returns:



116
117
118
# File 'lib/deepl/admin_client.rb', line 116

def create_developer_key(label: nil, extra_body_parameters: nil)
  Requests::Admin::CreateDeveloperKey.new(api, label, extra_body_parameters).request
end

#deactivate_developer_key(key, extra_body_parameters: nil) ⇒ DeepL::Resources::DeveloperKey

Deactivates a developer key. Deactivation is permanent.

Parameters:

  • key (String, DeepL::Resources::DeveloperKey) —

    Key ID or developer key.

  • extra_body_parameters (Hash, nil) (defaults to: nil) —

    Added to the request body last; replaces fields of the same name. Values are sent as given.

Returns:

Raises:

  • (ArgumentError) —

    If the key ID is empty.



136
137
138
# File 'lib/deepl/admin_client.rb', line 136

def deactivate_developer_key(key, extra_body_parameters: nil)
  update_developer_key('deactivate', { key_id: key_id(key) }, extra_body_parameters)
end

#get_custom_tag_usage_analytics(start_date, end_date, aggregate_by: nil, page: nil) ⇒ DeepL::Resources::CustomTagUsageReport

Retrieves the usage per custom tag within a date range, with the same limits as get_usage_analytics. Results are paginated: pass the next_page of a report as page to retrieve the following page.

Parameters:

  • start_date (Date, Time, DateTime, String) —

    Start date, as a Date, a Time, a DateTime or a YYYY-MM-DD string. Dates are UTC calendar days and both the start and the end day are included. For Time and DateTime values the UTC date of that moment is sent: +2026-10-20 00:00 02:00 is sent as 2026-10-19.

  • end_date (Date, Time, DateTime, String) —

    End date, in the same format as start_date.

  • aggregate_by (String, Symbol, nil) (defaults to: nil) —

    Aggregate the usage over the whole period or by day, see DeepL::Constants::CustomTagUsageAggregateBy. It is sent as is, and the aggregate_by of the report is the raw string the API returns.

  • page (Integer, nil) (defaults to: nil) —

    Page number to retrieve.

Returns:

Raises:



98
99
100
101
# File 'lib/deepl/admin_client.rb', line 98

def get_custom_tag_usage_analytics(start_date, end_date, aggregate_by: nil, page: nil)
  Requests::Admin::CustomTagUsageAnalytics.new(api, start_date, end_date, aggregate_by,
                                               page).request
end

#get_usage_analytics(start_date, end_date, group_by: nil) ⇒ DeepL::Resources::AdminUsageReport

Retrieves the usage of the organization within a date range. The API requires the start date to be before the end date (a report covers at least two days) and the range to be at most 366 days.

Parameters:

  • start_date (Date, Time, DateTime, String) —

    Start date, as a Date, a Time, a DateTime or a YYYY-MM-DD string. Dates are UTC calendar days and both the start and the end day are included. For Time and DateTime values the UTC date of that moment is sent: +2026-10-20 00:00 02:00 is sent as 2026-10-19.

  • end_date (Date, Time, DateTime, String) —

    End date, in the same format as start_date.

  • group_by (String, Symbol, nil) (defaults to: nil) —

    Group the usage by key or by key and day, see DeepL::Constants::AdminUsageGroupBy for the accepted values. It is sent as is, and the group_by of the report is the raw string the API returns.

Returns:

Raises:



77
78
79
# File 'lib/deepl/admin_client.rb', line 77

def get_usage_analytics(start_date, end_date, group_by: nil)
  Requests::Admin::UsageAnalytics.new(api, start_date, end_date, group_by).request
end

#inspect ⇒ Object



57
58
59
# File 'lib/deepl/admin_client.rb', line 57

def inspect
  "#<#{self.class.name} server_url=#{configuration.host}>"
end

#list_developer_keys ⇒ Array<DeepL::Resources::DeveloperKey>

Retrieves all developer keys of the organization, including deactivated ones.

Returns:



124
125
126
# File 'lib/deepl/admin_client.rb', line 124

def list_developer_keys
  Requests::Admin::ListDeveloperKeys.new(api).request
end

#rename_developer_key(key, label, extra_body_parameters: nil) ⇒ DeepL::Resources::DeveloperKey

Changes the label of a developer key.

Parameters:

  • key (String, DeepL::Resources::DeveloperKey) —

    Key ID or developer key.

  • label (String) —

    New label for the key.

  • extra_body_parameters (Hash, nil) (defaults to: nil) —

    Added to the request body last; replaces fields of the same name. Values are sent as given.

Returns:

Raises:

  • (ArgumentError) —

    If the key ID is empty.



149
150
151
# File 'lib/deepl/admin_client.rb', line 149

def rename_developer_key(key, label, extra_body_parameters: nil)
  update_developer_key('label', { key_id: key_id(key), label: label }, extra_body_parameters)
end

#set_developer_key_usage_limits(key, extra_body_parameters: nil, **limits) ⇒ DeepL::Resources::DeveloperKey

Changes the usage limits of a developer key. Only the given limits are changed; omitted limits keep their current value. A nil limit means unlimited and 0 blocks that usage.

admin_client.set_developer_key_usage_limits(key, characters: 0)
admin_client.set_developer_key_usage_limits(key, speech_to_text_milliseconds: nil)

Parameters:

  • key (String, DeepL::Resources::DeveloperKey) —

    Key ID or developer key.

  • limits (Hash) —

    Limits to change: :characters (characters per usage period across text translation, document translation and text improvement), :speech_to_text_milliseconds and :speech_to_speech_milliseconds, each an Integer or nil. The values are sent as given and validated by the API.

  • extra_body_parameters (Hash, nil) (defaults to: nil) —

    Added to the request body last; replaces fields of the same name. Values are sent as given.

Returns:

Raises:

  • (ArgumentError) —

    If the key ID is empty, or limits has a keyword other than :characters, :speech_to_text_milliseconds and :speech_to_speech_milliseconds.



170
171
172
173
174
# File 'lib/deepl/admin_client.rb', line 170

def set_developer_key_usage_limits(key, extra_body_parameters: nil, **limits)
  id = key_id(key)
  validate_usage_limits!(limits)
  update_developer_key('limits', limits.merge(key_id: id), extra_body_parameters)
end