deepl-rb 3.9.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (215) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +27 -1
  3. data/CHANGELOG.md +741 -18
  4. data/Gemfile +4 -0
  5. data/README.md +1001 -327
  6. data/Rakefile +29 -7
  7. data/VERSION +1 -1
  8. data/deepl-rb.gemspec +75 -88
  9. data/lib/deepl/admin_client.rb +208 -0
  10. data/lib/deepl/api.rb +18 -8
  11. data/lib/deepl/app_info.rb +19 -0
  12. data/lib/deepl/client.rb +1284 -0
  13. data/lib/deepl/configuration.rb +27 -32
  14. data/lib/deepl/constants/admin_usage_group_by.rb +13 -0
  15. data/lib/deepl/constants/custom_tag_usage_aggregate_by.rb +13 -0
  16. data/lib/deepl/constants/document_status_code.rb +17 -0
  17. data/lib/deepl/constants/language_include.rb +17 -0
  18. data/lib/deepl/constants/language_resource_name.rb +21 -0
  19. data/lib/deepl/constants/tag_handling_version.rb +14 -0
  20. data/lib/deepl/exceptions/authorization_failed.rb +1 -1
  21. data/lib/deepl/exceptions/bad_request.rb +1 -3
  22. data/lib/deepl/exceptions/connection_error.rb +40 -0
  23. data/lib/deepl/exceptions/document_not_ready_error.rb +17 -0
  24. data/lib/deepl/exceptions/document_translation_error.rb +17 -1
  25. data/lib/deepl/exceptions/error.rb +13 -1
  26. data/lib/deepl/exceptions/limit_exceeded.rb +1 -1
  27. data/lib/deepl/exceptions/not_found.rb +8 -3
  28. data/lib/deepl/exceptions/quota_exceeded.rb +1 -1
  29. data/lib/deepl/exceptions/request_entity_too_large.rb +1 -2
  30. data/lib/deepl/exceptions/request_error.rb +26 -2
  31. data/lib/deepl/exceptions/server_error.rb +9 -2
  32. data/lib/deepl/exceptions/translation_memory_job_error.rb +32 -0
  33. data/lib/deepl/exceptions/validation_error.rb +15 -0
  34. data/lib/deepl/requests/admin/base.rb +94 -0
  35. data/lib/deepl/requests/admin/create_developer_key.rb +38 -0
  36. data/lib/deepl/requests/admin/custom_tag_usage_analytics.rb +38 -0
  37. data/lib/deepl/requests/admin/list_developer_keys.rb +28 -0
  38. data/lib/deepl/requests/admin/update_developer_key.rb +35 -0
  39. data/lib/deepl/requests/admin/usage_analytics.rb +36 -0
  40. data/lib/deepl/requests/base.rb +313 -65
  41. data/lib/deepl/requests/correct.rb +51 -0
  42. data/lib/deepl/requests/document/download.rb +11 -3
  43. data/lib/deepl/requests/document/get_status.rb +2 -2
  44. data/lib/deepl/requests/document/upload.rb +56 -20
  45. data/lib/deepl/requests/glossary/base.rb +30 -0
  46. data/lib/deepl/requests/glossary/create.rb +18 -18
  47. data/lib/deepl/requests/glossary/destroy.rb +4 -16
  48. data/lib/deepl/requests/glossary/entries.rb +6 -14
  49. data/lib/deepl/requests/glossary/find.rb +3 -13
  50. data/lib/deepl/requests/glossary/language_pairs.rb +22 -6
  51. data/lib/deepl/requests/glossary/list.rb +2 -2
  52. data/lib/deepl/requests/language_resources.rb +41 -0
  53. data/lib/deepl/requests/languages.rb +25 -6
  54. data/lib/deepl/requests/languages_for_resource.rb +56 -0
  55. data/lib/deepl/requests/multilingual_glossary/create.rb +45 -0
  56. data/lib/deepl/requests/multilingual_glossary/destroy.rb +36 -0
  57. data/lib/deepl/requests/multilingual_glossary/destroy_dictionary.rb +55 -0
  58. data/lib/deepl/requests/multilingual_glossary/entries.rb +68 -0
  59. data/lib/deepl/requests/multilingual_glossary/find.rb +35 -0
  60. data/lib/deepl/requests/multilingual_glossary/helpers.rb +71 -0
  61. data/lib/deepl/requests/multilingual_glossary/list.rb +39 -0
  62. data/lib/deepl/requests/multilingual_glossary/replace_dictionary.rb +41 -0
  63. data/lib/deepl/requests/multilingual_glossary/update.rb +40 -0
  64. data/lib/deepl/requests/rephrase.rb +8 -10
  65. data/lib/deepl/requests/spoken_terms/create.rb +42 -0
  66. data/lib/deepl/requests/spoken_terms/destroy.rb +36 -0
  67. data/lib/deepl/requests/spoken_terms/destroy_term_list.rb +42 -0
  68. data/lib/deepl/requests/spoken_terms/entries.rb +47 -0
  69. data/lib/deepl/requests/spoken_terms/find.rb +35 -0
  70. data/lib/deepl/requests/spoken_terms/helpers.rb +74 -0
  71. data/lib/deepl/requests/spoken_terms/list.rb +35 -0
  72. data/lib/deepl/requests/spoken_terms/replace_term_list.rb +41 -0
  73. data/lib/deepl/requests/spoken_terms/update.rb +40 -0
  74. data/lib/deepl/requests/style_profile/base.rb +83 -0
  75. data/lib/deepl/requests/style_profile/create.rb +40 -0
  76. data/lib/deepl/requests/style_profile/destroy.rb +34 -0
  77. data/lib/deepl/requests/style_profile/find.rb +33 -0
  78. data/lib/deepl/requests/style_profile/list.rb +43 -0
  79. data/lib/deepl/requests/style_profile/update.rb +35 -0
  80. data/lib/deepl/requests/style_rule/create.rb +24 -0
  81. data/lib/deepl/requests/style_rule/create_custom_instruction.rb +11 -2
  82. data/lib/deepl/requests/style_rule/destroy.rb +4 -2
  83. data/lib/deepl/requests/style_rule/destroy_custom_instruction.rb +5 -3
  84. data/lib/deepl/requests/style_rule/find.rb +4 -2
  85. data/lib/deepl/requests/style_rule/find_custom_instruction.rb +5 -3
  86. data/lib/deepl/requests/style_rule/helpers.rb +44 -0
  87. data/lib/deepl/requests/style_rule/list.rb +3 -1
  88. data/lib/deepl/requests/style_rule/update.rb +5 -2
  89. data/lib/deepl/requests/style_rule/update_configured_rules.rb +4 -2
  90. data/lib/deepl/requests/style_rule/update_custom_instruction.rb +7 -3
  91. data/lib/deepl/requests/translate.rb +43 -27
  92. data/lib/deepl/requests/translation_memory/base.rb +1 -4
  93. data/lib/deepl/requests/translation_memory/create_export.rb +7 -2
  94. data/lib/deepl/requests/translation_memory/create_import.rb +11 -1
  95. data/lib/deepl/requests/translation_memory/destroy.rb +1 -1
  96. data/lib/deepl/requests/translation_memory/find.rb +1 -1
  97. data/lib/deepl/requests/translation_memory/find_job.rb +1 -1
  98. data/lib/deepl/requests/translation_memory/list.rb +4 -3
  99. data/lib/deepl/requests/translation_memory/segments.rb +5 -4
  100. data/lib/deepl/requests/translation_memory/storage_base.rb +37 -9
  101. data/lib/deepl/requests/translation_memory/upload_file.rb +1 -1
  102. data/lib/deepl/requests/usage.rb +28 -1
  103. data/lib/deepl/requests/write_improvements.rb +42 -0
  104. data/lib/deepl/resources/admin_usage_report.rb +73 -0
  105. data/lib/deepl/resources/custom_tag_usage_report.rb +59 -0
  106. data/lib/deepl/resources/developer_key.rb +57 -0
  107. data/lib/deepl/resources/document_handle.rb +8 -34
  108. data/lib/deepl/resources/document_translation_status.rb +14 -7
  109. data/lib/deepl/resources/language.rb +5 -3
  110. data/lib/deepl/resources/language_resource.rb +57 -0
  111. data/lib/deepl/resources/language_support.rb +56 -0
  112. data/lib/deepl/resources/multilingual_glossary.rb +112 -0
  113. data/lib/deepl/resources/spoken_terms.rb +61 -0
  114. data/lib/deepl/resources/style_profile.rb +44 -0
  115. data/lib/deepl/resources/style_rule.rb +4 -23
  116. data/lib/deepl/resources/text.rb +18 -2
  117. data/lib/deepl/resources/translation_memory.rb +8 -2
  118. data/lib/deepl/resources/translation_memory_import.rb +1 -1
  119. data/lib/deepl/resources/translation_memory_job.rb +26 -7
  120. data/lib/deepl/resources/translation_memory_segments.rb +8 -6
  121. data/lib/deepl/resources/usage.rb +61 -5
  122. data/lib/deepl/utils/client_builder.rb +89 -0
  123. data/lib/deepl/utils/document_poller.rb +70 -0
  124. data/lib/deepl/utils/exception_builder.rb +3 -2
  125. data/lib/deepl/utils/glossary_entries.rb +30 -0
  126. data/lib/deepl/utils/http_client_builder.rb +116 -0
  127. data/lib/deepl/utils/number_parser.rb +34 -0
  128. data/lib/deepl/utils/output_path.rb +25 -0
  129. data/lib/deepl/utils/term_validator.rb +33 -0
  130. data/lib/deepl/utils/time_parser.rb +14 -0
  131. data/lib/deepl/utils/translation_memory_operations.rb +230 -0
  132. data/lib/deepl.rb +68 -108
  133. data/lib/http_client_options.rb +21 -1
  134. data/lib/version.rb +1 -1
  135. data/upgrading_to_v4.md +374 -0
  136. metadata +84 -85
  137. data/.circleci/config.yml +0 -27
  138. data/.github/workflows/add_issues_to_kanban.yml +0 -16
  139. data/.gitlab-ci.yml +0 -179
  140. data/lib/deepl/document_api.rb +0 -131
  141. data/lib/deepl/exceptions/not_supported.rb +0 -14
  142. data/lib/deepl/glossary_api.rb +0 -38
  143. data/lib/deepl/style_rule_api.rb +0 -61
  144. data/lib/deepl/translation_memory_api.rb +0 -309
  145. data/license_checker.sh +0 -8
  146. data/spec/api/api_spec.rb +0 -20
  147. data/spec/api/configuration_spec.rb +0 -122
  148. data/spec/api/deepl_spec.rb +0 -262
  149. data/spec/constants/constants_spec.rb +0 -158
  150. data/spec/integration_tests/document_api_spec.rb +0 -211
  151. data/spec/integration_tests/document_error_paths_spec.rb +0 -33
  152. data/spec/integration_tests/glossary_api_spec.rb +0 -114
  153. data/spec/integration_tests/glossary_error_paths_spec.rb +0 -107
  154. data/spec/integration_tests/integration_test_utils.rb +0 -178
  155. data/spec/integration_tests/languages_api_spec.rb +0 -54
  156. data/spec/integration_tests/languages_error_paths_spec.rb +0 -25
  157. data/spec/integration_tests/rephrase_api_spec.rb +0 -90
  158. data/spec/integration_tests/rephrase_error_paths_spec.rb +0 -53
  159. data/spec/integration_tests/smoke_test_spec.rb +0 -24
  160. data/spec/integration_tests/style_rule_api_spec.rb +0 -117
  161. data/spec/integration_tests/style_rule_error_paths_spec.rb +0 -45
  162. data/spec/integration_tests/translate_api_spec.rb +0 -117
  163. data/spec/integration_tests/translate_error_paths_spec.rb +0 -48
  164. data/spec/integration_tests/translation_memory_api_spec.rb +0 -244
  165. data/spec/integration_tests/translation_memory_error_paths_spec.rb +0 -87
  166. data/spec/integration_tests/usage_api_spec.rb +0 -29
  167. data/spec/integration_tests/usage_error_paths_spec.rb +0 -18
  168. data/spec/requests/document/upload_spec.rb +0 -130
  169. data/spec/requests/extra_body_parameters_types_spec.rb +0 -82
  170. data/spec/requests/glossary/create_spec.rb +0 -44
  171. data/spec/requests/glossary/destroy_spec.rb +0 -27
  172. data/spec/requests/glossary/entries_spec.rb +0 -27
  173. data/spec/requests/glossary/find_spec.rb +0 -28
  174. data/spec/requests/glossary/language_pairs_spec.rb +0 -27
  175. data/spec/requests/glossary/list_spec.rb +0 -27
  176. data/spec/requests/languages_spec.rb +0 -27
  177. data/spec/requests/rephrase_spec.rb +0 -46
  178. data/spec/requests/style_rule/create_custom_instruction_spec.rb +0 -30
  179. data/spec/requests/style_rule/create_spec.rb +0 -29
  180. data/spec/requests/style_rule/destroy_custom_instruction_spec.rb +0 -28
  181. data/spec/requests/style_rule/destroy_spec.rb +0 -27
  182. data/spec/requests/style_rule/find_custom_instruction_spec.rb +0 -29
  183. data/spec/requests/style_rule/find_spec.rb +0 -28
  184. data/spec/requests/style_rule/list_spec.rb +0 -27
  185. data/spec/requests/style_rule/update_configured_rules_spec.rb +0 -31
  186. data/spec/requests/style_rule/update_custom_instruction_spec.rb +0 -32
  187. data/spec/requests/style_rule/update_spec.rb +0 -29
  188. data/spec/requests/translate_spec.rb +0 -338
  189. data/spec/requests/translation_memory/create_export_spec.rb +0 -37
  190. data/spec/requests/translation_memory/create_import_spec.rb +0 -61
  191. data/spec/requests/translation_memory/destroy_spec.rb +0 -36
  192. data/spec/requests/translation_memory/download_export_spec.rb +0 -54
  193. data/spec/requests/translation_memory/find_job_spec.rb +0 -37
  194. data/spec/requests/translation_memory/find_spec.rb +0 -36
  195. data/spec/requests/translation_memory/list_spec.rb +0 -27
  196. data/spec/requests/translation_memory/segments_spec.rb +0 -58
  197. data/spec/requests/translation_memory/upload_file_spec.rb +0 -55
  198. data/spec/requests/usage_spec.rb +0 -27
  199. data/spec/resources/custom_instruction_spec.rb +0 -32
  200. data/spec/resources/glossary_spec.rb +0 -38
  201. data/spec/resources/language_pair_spec.rb +0 -23
  202. data/spec/resources/language_spec.rb +0 -45
  203. data/spec/resources/style_rule_spec.rb +0 -68
  204. data/spec/resources/text_spec.rb +0 -23
  205. data/spec/resources/translation_memory_export_spec.rb +0 -38
  206. data/spec/resources/translation_memory_import_spec.rb +0 -30
  207. data/spec/resources/translation_memory_job_spec.rb +0 -109
  208. data/spec/resources/translation_memory_segments_spec.rb +0 -78
  209. data/spec/resources/translation_memory_spec.rb +0 -52
  210. data/spec/resources/usage_spec.rb +0 -35
  211. data/spec/spec_helper.rb +0 -62
  212. data/spec/support/live_mock_server.rb +0 -12
  213. data/spec/support/managed_glossary.rb +0 -65
  214. data/spec/support/managed_style_rule.rb +0 -17
  215. data/spec/support/managed_translation_memory.rb +0 -55
@@ -4,33 +4,23 @@
4
4
  # frozen_string_literal: true
5
5
 
6
6
  module DeepL
7
+ # The options of one DeepL::Client or DeepL::AdminClient, built by Utils::ClientBuilder.
8
+ # Nothing is read from the environment.
9
+ # @api private
7
10
  class Configuration
8
- ATTRIBUTES = %i[auth_key host logger max_doc_status_queries max_network_retries user_agent
9
- version].freeze
10
-
11
- attr_accessor(*ATTRIBUTES)
12
-
13
- DEEPL_SERVER_URL = 'https://api.deepl.com'
14
- DEEPL_SERVER_URL_FREE = 'https://api-free.deepl.com'
15
- private_constant :DEEPL_SERVER_URL, :DEEPL_SERVER_URL_FREE
16
-
17
- def initialize(data = {}, app_info_name = nil, app_info_version = nil, send_platform_info = true) # rubocop:disable all
18
- data.each { |key, value| send("#{key}=", value) }
19
- @auth_key ||= ENV.fetch('DEEPL_AUTH_KEY', nil)
20
- @host ||= ENV.fetch('DEEPL_SERVER_URL', nil)
21
- @host ||= if self.class.free_account_auth_key?(@auth_key)
22
- DEEPL_SERVER_URL_FREE
23
- else
24
- DEEPL_SERVER_URL
25
- end
26
- @version ||= 'v2'
27
- @user_agent ||= construct_user_agent(send_platform_info, app_info_name, app_info_version)
28
- @max_network_retries ||= 5
29
- @logger ||= nil
30
- end
31
-
32
- def validate!
33
- raise Exceptions::Error, 'auth_key not provided' if auth_key.nil? || auth_key.empty?
11
+ ATTRIBUTES = %i[auth_key host logger max_network_retries user_agent headers timeout].freeze
12
+
13
+ attr_reader(*ATTRIBUTES)
14
+
15
+ def initialize(auth_key:, host:, max_network_retries:, timeout:, logger:, headers:, # rubocop:disable Metrics/ParameterLists
16
+ user_agent:, send_platform_info:, app_info:)
17
+ @auth_key = auth_key
18
+ @host = host
19
+ @max_network_retries = max_network_retries
20
+ @timeout = timeout
21
+ @logger = logger
22
+ @headers = headers
23
+ @user_agent = user_agent || construct_user_agent(send_platform_info, app_info)
34
24
  end
35
25
 
36
26
  def attributes
@@ -38,20 +28,25 @@ module DeepL
38
28
  end
39
29
 
40
30
  def ==(other)
41
- attributes == other.attributes
31
+ other.is_a?(Configuration) && attributes == other.attributes
42
32
  end
43
33
 
44
- def self.free_account_auth_key?(key)
45
- key&.end_with?(':fx')
34
+ # Redacts the auth key, so that it never shows in the inspect output of objects holding the
35
+ # configuration.
36
+ def inspect
37
+ shown = attributes.merge(auth_key: auth_key.nil? ? nil : '[REDACTED]')
38
+ "#<#{self.class.name} #{shown.map { |name, value| "#{name}=#{value.inspect}" }.join(', ')}>"
46
39
  end
47
40
 
48
- def construct_user_agent(send_platform_info, app_info_name, app_info_version)
41
+ private
42
+
43
+ def construct_user_agent(send_platform_info, app_info)
49
44
  library_info_str = "deepl-ruby/#{DeepL::VERSION}"
50
45
  if send_platform_info
51
46
  library_info_str += " (#{RbConfig::CONFIG['host_os']}) ruby/#{RUBY_VERSION}"
52
47
  end
53
- if app_info_name && app_info_version
54
- library_info_str += " #{app_info_name}/#{app_info_version}"
48
+ if app_info&.app_name && app_info.app_version
49
+ library_info_str += " #{app_info.app_name}/#{app_info.app_version}"
55
50
  end
56
51
  library_info_str
57
52
  end
@@ -0,0 +1,13 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Constants
8
+ class AdminUsageGroupBy < BaseConstant
9
+ KEY = 'key'
10
+ KEY_AND_DAY = 'key_and_day'
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Constants
8
+ class CustomTagUsageAggregateBy < BaseConstant
9
+ PERIOD = 'period'
10
+ DAY = 'day'
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Constants
8
+ # Values of +DeepL::Resources::DocumentTranslationStatus#status+. The status stays a plain
9
+ # string, so a value the API adds later is kept as it is.
10
+ class DocumentStatusCode < BaseConstant
11
+ QUEUED = 'queued'
12
+ TRANSLATING = 'translating'
13
+ DONE = 'done'
14
+ ERROR = 'error'
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Constants
8
+ # Values of the +include+ option of +DeepL::Client#get_languages_for_resource+. Other strings
9
+ # are accepted and sent unchanged.
10
+ class LanguageInclude < BaseConstant
11
+ # Also return beta languages and features.
12
+ BETA = 'beta'
13
+ # Also return features that rely on third-party providers.
14
+ EXTERNAL = 'external'
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Constants
8
+ # Values of the +resource+ argument of +DeepL::Client#get_languages_for_resource+ and of
9
+ # +DeepL::Resources::LanguageResource#name+. Other strings are accepted and sent unchanged, so
10
+ # a resource the API adds later can be queried before it is listed here.
11
+ class LanguageResourceName < BaseConstant
12
+ TRANSLATE_TEXT = 'translate_text'
13
+ TRANSLATE_DOCUMENT = 'translate_document'
14
+ GLOSSARY = 'glossary'
15
+ VOICE = 'voice'
16
+ WRITE = 'write'
17
+ STYLE_RULES = 'style_rules'
18
+ TRANSLATION_MEMORY = 'translation_memory'
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Constants
8
+ # Values of the +tag_handling_version+ option. Any other string is sent as given.
9
+ class TagHandlingVersion < BaseConstant
10
+ V1 = 'v1'
11
+ V2 = 'v2'
12
+ end
13
+ end
14
+ end
@@ -7,7 +7,7 @@ module DeepL
7
7
  module Exceptions
8
8
  class AuthorizationFailed < RequestError
9
9
  def to_s
10
- 'Authorization failed. Please supply a valid auth_key parameter.'
10
+ with_body_message('Authorization failed. Please supply a valid auth_key parameter.')
11
11
  end
12
12
  end
13
13
  end
@@ -7,9 +7,7 @@ module DeepL
7
7
  module Exceptions
8
8
  class BadRequest < RequestError
9
9
  def to_s
10
- JSON.parse(response.body)['message']
11
- rescue JSON::ParserError
12
- response.body
10
+ body_message.to_s
13
11
  end
14
12
  end
15
13
  end
@@ -0,0 +1,40 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Exceptions
8
+ ##
9
+ # Raised by DeepL::Client and DeepL::AdminClient for a network failure without a complete HTTP
10
+ # response from the API (DNS, connection, TLS, proxy, timeout or a body cut off mid-read), after
11
+ # the retries are used up. Transient failures (DNS, connection, timeout, cut-off body) are
12
+ # retried and have +should_retry?+ true; TLS failures, proxy failures and malformed responses
13
+ # are raised at once and have +should_retry?+ false. Requests that create a resource retry only
14
+ # transient failures while connecting (DNS, refused connection, unreachable host or network,
15
+ # connection timeout, a reset during the TLS or proxy handshake), before the request is sent; of
16
+ # their other failures, only a cut-off body has +should_retry?+ true. The original exception is
17
+ # available as +cause+.
18
+ class ConnectionError < Error
19
+ # Raised from a +rescue+ block, so the original exception becomes +cause+.
20
+ # +should_retry: false+ marks a failure after which the request may have been processed.
21
+ def self.from_network_error(error, should_retry: true)
22
+ new("Connection to the DeepL API failed: #{error.message}",
23
+ should_retry: should_retry && transient?(error))
24
+ end
25
+
26
+ def self.transient?(error)
27
+ Requests::Base::RETRYABLE_NETWORK_ERRORS.any? { |type| error.is_a?(type) }
28
+ end
29
+
30
+ def initialize(message = nil, should_retry: true)
31
+ super(message)
32
+ @should_retry = should_retry
33
+ end
34
+
35
+ def should_retry?
36
+ @should_retry
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,17 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Exceptions
8
+ # HTTP 503 on a document download: the translated document is not ready yet. Raised after
9
+ # the retries.
10
+ class DocumentNotReadyError < ServerError
11
+ def to_s
12
+ message = body_message
13
+ message ? "Document not ready: #{message}" : 'Document not ready.'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -6,9 +6,25 @@
6
6
  module DeepL
7
7
  module Exceptions
8
8
  class DocumentTranslationError < Error
9
- def initialize(message, handle)
9
+ # @return [DeepL::Resources::DocumentHandle, nil] Handle of the uploaded document, +nil+ if
10
+ # the upload failed. Use it to query the status or download the document later.
11
+ attr_reader :document_handle
12
+
13
+ # +error+ is the failure being wrapped; the HTTP status and retryability of a DeepL error
14
+ # are copied from it.
15
+ def initialize(message, handle, error = nil)
10
16
  super(message)
11
17
  @handle = handle
18
+ @document_handle = handle
19
+ return unless error.is_a?(Error)
20
+
21
+ @http_status_code = error.http_status_code
22
+ @should_retry = error.should_retry?
23
+ end
24
+
25
+ # @return [Boolean] +should_retry?+ of the wrapped DeepL error, +false+ without one.
26
+ def should_retry?
27
+ @should_retry || false
12
28
  end
13
29
  end
14
30
  end
@@ -6,8 +6,20 @@
6
6
  module DeepL
7
7
  module Exceptions
8
8
  class Error < StandardError
9
+ # @return [Integer, nil] HTTP status of the failed response, +nil+ if there was none.
10
+ attr_reader :http_status_code
11
+
12
+ # @param [String, nil] message
13
+ # @param [Integer, nil] http_status_code HTTP status of the failed response.
14
+ # @param [Boolean] should_retry Whether repeating the request may succeed.
15
+ def initialize(message = nil, http_status_code: nil, should_retry: false)
16
+ super(message)
17
+ @http_status_code = http_status_code
18
+ @should_retry = should_retry
19
+ end
20
+
9
21
  def should_retry?
10
- false
22
+ @should_retry || false
11
23
  end
12
24
  end
13
25
  end
@@ -7,7 +7,7 @@ module DeepL
7
7
  module Exceptions
8
8
  class LimitExceeded < RequestError
9
9
  def to_s
10
- 'Limit exceeded. Please wait and send your request once again.'
10
+ with_body_message('Limit exceeded. Please wait and send your request once again.')
11
11
  end
12
12
 
13
13
  def should_retry?
@@ -6,10 +6,15 @@
6
6
  module DeepL
7
7
  module Exceptions
8
8
  class NotFound < RequestError
9
+ # @param [String, nil] message Replaces the API's message, for a not-found condition the
10
+ # SDK detects in a successful response.
11
+ def initialize(response, request = nil, message = nil)
12
+ super(response, request)
13
+ @message = message
14
+ end
15
+
9
16
  def to_s
10
- JSON.parse(response.body)['message']
11
- rescue JSON::ParserError
12
- response.body
17
+ (@message || body_message).to_s
13
18
  end
14
19
  end
15
20
  end
@@ -7,7 +7,7 @@ module DeepL
7
7
  module Exceptions
8
8
  class QuotaExceeded < RequestError
9
9
  def to_s
10
- 'Quota exceeded. The character limit has been reached.'
10
+ with_body_message('Quota exceeded. The character limit has been reached.')
11
11
  end
12
12
  end
13
13
  end
@@ -7,8 +7,7 @@ module DeepL
7
7
  module Exceptions
8
8
  class RequestEntityTooLarge < RequestError
9
9
  def to_s
10
- 'The request size has reached the supported limit. ' \
11
- "Make sure that you're not sending more than 50 text parts."
10
+ with_body_message('The request size has reached the supported limit.')
12
11
  end
13
12
  end
14
13
  end
@@ -8,13 +8,37 @@ module DeepL
8
8
  class RequestError < Error
9
9
  attr_reader :request, :response
10
10
 
11
- def initialize(response)
11
+ def initialize(response, request = nil)
12
12
  super()
13
13
  @response = response
14
+ @request = request
15
+ @http_status_code = response.code.to_i if response.respond_to?(:code)
14
16
  end
15
17
 
16
18
  def to_s
17
- 'Unknown error.'
19
+ message = body_message
20
+ status = "HTTP status #{http_status_code}"
21
+ message ? "Request failed with #{status}: #{message}" : "Request failed with #{status}."
22
+ end
23
+
24
+ private
25
+
26
+ # +text+ followed by the API's message, if the body has one; +text+'s final period is
27
+ # dropped before it.
28
+ def with_body_message(text)
29
+ message = body_message
30
+ message ? "#{text.chomp('.')}: #{message}" : text
31
+ end
32
+
33
+ # The +message+ of a JSON object body, else the raw body; +nil+ for an empty body.
34
+ def body_message
35
+ body = response&.body
36
+ return nil if body.nil? || body.to_s.strip.empty?
37
+
38
+ data = JSON.parse(body)
39
+ data.is_a?(Hash) && data['message'].is_a?(String) ? data['message'] : body
40
+ rescue JSON::ParserError
41
+ body
18
42
  end
19
43
  end
20
44
  end
@@ -6,12 +6,19 @@
6
6
  module DeepL
7
7
  module Exceptions
8
8
  class ServerError < RequestError
9
+ # @param [Boolean] should_retry False for a request that is unsafe to repeat once sent.
10
+ def initialize(response, request = nil, should_retry: true)
11
+ super(response, request)
12
+ @should_retry = should_retry
13
+ end
14
+
9
15
  def to_s
10
- 'An internal server error occured. Try again after waiting a short period.'
16
+ with_body_message('An internal server error occured. ' \
17
+ 'Try again after waiting a short period.')
11
18
  end
12
19
 
13
20
  def should_retry?
14
- true
21
+ @should_retry
15
22
  end
16
23
  end
17
24
  end
@@ -0,0 +1,32 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Exceptions
8
+ ##
9
+ # Any failure of DeepL::Client#import_translation_memory_from_filepath or
10
+ # #export_translation_memory_to_filepath except a missing input file. The failure being
11
+ # wrapped is the +cause+.
12
+ class TranslationMemoryJobError < Error
13
+ # @return [String, nil] ID of the import or export job, set as soon as the job was created,
14
+ # +nil+ if the failure happened before. Use it to query or resume the job.
15
+ attr_reader :job_id
16
+
17
+ # @return [DeepL::Resources::TranslationMemoryJob, nil] The last job status the helper
18
+ # fetched, +nil+ before the first status check.
19
+ attr_reader :job
20
+
21
+ # +error+ is the failure being wrapped; the HTTP status and retryability of a DeepL error
22
+ # are copied from it.
23
+ def initialize(message, job_id, job, error = nil)
24
+ deepl_error = error.is_a?(Error) ? error : nil
25
+ super(message, http_status_code: deepl_error&.http_status_code,
26
+ should_retry: deepl_error&.should_retry? || false)
27
+ @job_id = job_id
28
+ @job = job
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,15 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Exceptions
8
+ ##
9
+ # An invalid argument detected before any request is sent: an invalid glossary or spoken
10
+ # terms term, or a malformed glossary TSV line. An ArgumentError, not a
11
+ # DeepL::Exceptions::Error, like every other invalid argument.
12
+ class ValidationError < ArgumentError
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,94 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ require 'date'
7
+
8
+ module DeepL
9
+ module Requests
10
+ module Admin
11
+ class Base < Requests::Base
12
+ def initialize(api, extra_body_parameters = nil)
13
+ options = {}
14
+ options[:extra_body_parameters] = extra_body_parameters unless extra_body_parameters.nil?
15
+ super(api, options)
16
+ end
17
+
18
+ def to_s
19
+ "#{http_method} #{uri.request_uri}"
20
+ end
21
+
22
+ private
23
+
24
+ def http_method
25
+ 'GET'
26
+ end
27
+
28
+ def get_request # rubocop:disable Naming/AccessorMethodName
29
+ Net::HTTP::Get.new(uri.request_uri, add_json_content_type(headers))
30
+ end
31
+
32
+ def uri
33
+ @uri ||= begin
34
+ base_uri = URI(url)
35
+ query_string = encode_query(query_params)
36
+ base_uri.query = query_string unless query_string.empty?
37
+ base_uri
38
+ end
39
+ end
40
+
41
+ def query_params
42
+ {}
43
+ end
44
+
45
+ def date_range_params(start_date, end_date)
46
+ start_day = parse_date(start_date)
47
+ end_day = parse_date(end_date)
48
+
49
+ { 'start_date' => start_day.iso8601, 'end_date' => end_day.iso8601 }
50
+ end
51
+
52
+ def parse_date(value)
53
+ return value.new_offset(0).to_date if value.is_a?(DateTime)
54
+ return value if value.is_a?(Date)
55
+ return value.getutc.to_date if value.is_a?(Time)
56
+
57
+ date = parse_date_string(value) if value.is_a?(String)
58
+ return date if date
59
+
60
+ raise ArgumentError,
61
+ "Invalid date #{value.inspect}, expected a Date, a Time, a DateTime or " \
62
+ 'a YYYY-MM-DD string'
63
+ end
64
+
65
+ def parse_date_string(value)
66
+ return nil unless value.match?(/\A\d{4}-\d{2}-\d{2}\z/)
67
+
68
+ year, month, day = value.split('-').map(&:to_i)
69
+ Date.new(year, month, day) if Date.valid_date?(year, month, day)
70
+ end
71
+
72
+ def build_developer_key(request, response)
73
+ data = JSON.parse(response.body)
74
+ raise DeepL::Exceptions::Error, 'Developer key response is missing key_id' unless
75
+ usable_developer_key?(data)
76
+
77
+ Resources::DeveloperKey.new(data, request, response)
78
+ end
79
+
80
+ def usable_developer_key?(data)
81
+ data.is_a?(Hash) && data['key_id'].is_a?(String) && !data['key_id'].empty?
82
+ end
83
+
84
+ def parse_json_object(response)
85
+ data = JSON.parse(response.body)
86
+ raise DeepL::Exceptions::Error, 'Analytics response is not a JSON object' unless
87
+ data.is_a?(Hash)
88
+
89
+ data
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,38 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Requests
8
+ module Admin
9
+ class CreateDeveloperKey < Base
10
+ def initialize(api, label = nil, extra_body_parameters = nil)
11
+ super(api, extra_body_parameters)
12
+ @label = label
13
+ end
14
+
15
+ def request
16
+ payload = {}
17
+ payload[:label] = @label unless @label.nil?
18
+ build_developer_key(*execute_request_with_retries(post_request(payload)))
19
+ end
20
+
21
+ private
22
+
23
+ def http_method
24
+ 'POST'
25
+ end
26
+
27
+ def path
28
+ 'admin/developer-keys'
29
+ end
30
+
31
+ # A repeated creation request could create a second key.
32
+ def retry_only_unprocessed?
33
+ true
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ # Copyright 2026 DeepL SE (https://www.deepl.com)
2
+ # Use of this source code is governed by an MIT
3
+ # license that can be found in the LICENSE.md file.
4
+ # frozen_string_literal: true
5
+
6
+ module DeepL
7
+ module Requests
8
+ module Admin
9
+ class CustomTagUsageAnalytics < Base
10
+ def initialize(api, start_date, end_date, aggregate_by = nil, page = nil)
11
+ super(api)
12
+ @start_date = start_date
13
+ @end_date = end_date
14
+ @aggregate_by = aggregate_by
15
+ @page = page
16
+ end
17
+
18
+ def request
19
+ request, response = execute_request_with_retries(get_request)
20
+ Resources::CustomTagUsageReport.new(parse_json_object(response), request, response)
21
+ end
22
+
23
+ private
24
+
25
+ def query_params
26
+ params = date_range_params(@start_date, @end_date)
27
+ params['aggregate_by'] = @aggregate_by.to_s unless @aggregate_by.nil?
28
+ params['page'] = @page.to_s unless @page.nil?
29
+ params
30
+ end
31
+
32
+ def path
33
+ 'admin/analytics/custom-tags'
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end