deepl-rb 3.2.0 → 3.4.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +46 -27
- data/README.md +71 -0
- metadata +3 -95
- data/.circleci/config.yml +0 -27
- data/.github/workflows/add_issues_to_kanban.yml +0 -16
- data/.gitlab-ci.yml +0 -135
- data/.rubocop.yml +0 -51
- data/CODE_OF_CONDUCT.md +0 -132
- data/CONTRIBUTING.md +0 -37
- data/Gemfile +0 -24
- data/Rakefile +0 -53
- data/SECURITY.md +0 -58
- data/VERSION +0 -1
- data/deepl-rb.gemspec +0 -132
- data/lib/deepl/api.rb +0 -22
- data/lib/deepl/configuration.rb +0 -59
- data/lib/deepl/constants/base_constant.rb +0 -18
- data/lib/deepl/constants/formality.rb +0 -16
- data/lib/deepl/constants/model_type.rb +0 -14
- data/lib/deepl/constants/split_sentences.rb +0 -14
- data/lib/deepl/constants/tag_handling.rb +0 -13
- data/lib/deepl/constants/tone.rb +0 -20
- data/lib/deepl/constants/writing_style.rb +0 -20
- data/lib/deepl/document_api.rb +0 -121
- data/lib/deepl/exceptions/authorization_failed.rb +0 -14
- data/lib/deepl/exceptions/bad_request.rb +0 -16
- data/lib/deepl/exceptions/document_translation_error.rb +0 -15
- data/lib/deepl/exceptions/error.rb +0 -14
- data/lib/deepl/exceptions/limit_exceeded.rb +0 -18
- data/lib/deepl/exceptions/not_found.rb +0 -16
- data/lib/deepl/exceptions/not_supported.rb +0 -14
- data/lib/deepl/exceptions/quota_exceeded.rb +0 -14
- data/lib/deepl/exceptions/request_entity_too_large.rb +0 -15
- data/lib/deepl/exceptions/request_error.rb +0 -21
- data/lib/deepl/exceptions/server_error.rb +0 -18
- data/lib/deepl/glossary_api.rb +0 -38
- data/lib/deepl/requests/base.rb +0 -163
- data/lib/deepl/requests/document/download.rb +0 -44
- data/lib/deepl/requests/document/get_status.rb +0 -44
- data/lib/deepl/requests/document/upload.rb +0 -64
- data/lib/deepl/requests/glossary/create.rb +0 -59
- data/lib/deepl/requests/glossary/destroy.rb +0 -37
- data/lib/deepl/requests/glossary/entries.rb +0 -37
- data/lib/deepl/requests/glossary/find.rb +0 -38
- data/lib/deepl/requests/glossary/language_pairs.rb +0 -38
- data/lib/deepl/requests/glossary/list.rb +0 -37
- data/lib/deepl/requests/languages.rb +0 -37
- data/lib/deepl/requests/rephrase.rb +0 -54
- data/lib/deepl/requests/translate.rb +0 -82
- data/lib/deepl/requests/usage.rb +0 -33
- data/lib/deepl/resources/base.rb +0 -17
- data/lib/deepl/resources/document_handle.rb +0 -57
- data/lib/deepl/resources/document_translation_status.rb +0 -54
- data/lib/deepl/resources/glossary.rb +0 -28
- data/lib/deepl/resources/language.rb +0 -30
- data/lib/deepl/resources/language_pair.rb +0 -23
- data/lib/deepl/resources/text.rb +0 -24
- data/lib/deepl/resources/usage.rb +0 -27
- data/lib/deepl/utils/backoff_timer.rb +0 -46
- data/lib/deepl/utils/exception_builder.rb +0 -34
- data/lib/deepl.rb +0 -142
- data/lib/http_client_options.rb +0 -22
- data/spec/api/api_spec.rb +0 -20
- data/spec/api/configuration_spec.rb +0 -122
- data/spec/api/deepl_spec.rb +0 -431
- data/spec/constants/constants_spec.rb +0 -158
- data/spec/fixtures/vcr_cassettes/deepl_document.yml +0 -95
- data/spec/fixtures/vcr_cassettes/deepl_document_download.yml +0 -1214
- data/spec/fixtures/vcr_cassettes/deepl_glossaries.yml +0 -1163
- data/spec/fixtures/vcr_cassettes/deepl_languages.yml +0 -54
- data/spec/fixtures/vcr_cassettes/deepl_translate.yml +0 -358
- data/spec/fixtures/vcr_cassettes/deepl_usage.yml +0 -129
- data/spec/fixtures/vcr_cassettes/glossaries.yml +0 -1702
- data/spec/fixtures/vcr_cassettes/languages.yml +0 -229
- data/spec/fixtures/vcr_cassettes/rephrase_texts.yml +0 -401
- data/spec/fixtures/vcr_cassettes/translate_texts.yml +0 -10372
- data/spec/fixtures/vcr_cassettes/usage.yml +0 -171
- data/spec/integration_tests/document_api_spec.rb +0 -155
- data/spec/integration_tests/integration_test_utils.rb +0 -170
- data/spec/requests/glossary/create_spec.rb +0 -65
- data/spec/requests/glossary/destroy_spec.rb +0 -66
- data/spec/requests/glossary/entries_spec.rb +0 -62
- data/spec/requests/glossary/find_spec.rb +0 -68
- data/spec/requests/glossary/language_pairs_spec.rb +0 -40
- data/spec/requests/glossary/list_spec.rb +0 -54
- data/spec/requests/languages_spec.rb +0 -68
- data/spec/requests/rephrase_spec.rb +0 -172
- data/spec/requests/translate_spec.rb +0 -410
- data/spec/requests/usage_spec.rb +0 -43
- data/spec/resources/glossary_spec.rb +0 -38
- data/spec/resources/language_pair_spec.rb +0 -23
- data/spec/resources/language_spec.rb +0 -45
- data/spec/resources/text_spec.rb +0 -23
- data/spec/resources/usage_spec.rb +0 -35
- data/spec/spec_helper.rb +0 -92
data/lib/deepl/document_api.rb
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
# Copyright 2024 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
|
-
class DocumentApi
|
|
8
|
-
def initialize(api, options = {})
|
|
9
|
-
@api = api
|
|
10
|
-
@options = options
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
##
|
|
14
|
-
# Uploads the file at the given +input_file_path+ to be translated from +source_lang+ into
|
|
15
|
-
# +target_lang+. The API interface is async, so you need to poll using the returned
|
|
16
|
-
# `DeepL::Resources::DocumentHandle` until the translation is finished, then you can download it
|
|
17
|
-
#
|
|
18
|
-
# @param [String] input_file_path File path to the file to be translated
|
|
19
|
-
# @param [String, nil] source_lang Source language to use for the translation. `nil` will cause
|
|
20
|
-
# automatic source langauge detection to be used. Must be
|
|
21
|
-
# formatted as ISO 639-1, 2-letter language codes.
|
|
22
|
-
# @param [String] target_lang Target language to use for the translation. Must be formatted as
|
|
23
|
-
# ISO 639-1, 2-letter language codes, plus a hyphen "-" with the
|
|
24
|
-
# variant identifier for languages with variants/dialects/... .
|
|
25
|
-
# @param [String, nil] filename The filename of the file, including its extension. Used to open
|
|
26
|
-
# the different kinds of documents (PDFs, etc). If nil, will use
|
|
27
|
-
# the filename of +input_file_path+.
|
|
28
|
-
# @param [Hash] options Additional (body) options for the upload.
|
|
29
|
-
# @param [Hash] additional_headers Additional HTTP headers for the upload.
|
|
30
|
-
# @return [DeepL::Resources::DocumentHandle] Document handle for the uploaded document.
|
|
31
|
-
|
|
32
|
-
def upload(input_file_path, source_lang, target_lang, filename = nil, options = {},
|
|
33
|
-
additional_headers = {})
|
|
34
|
-
DeepL::Requests::Document::Upload.new(@api, input_file_path, source_lang, target_lang,
|
|
35
|
-
filename, options, additional_headers)
|
|
36
|
-
.request
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
##
|
|
40
|
-
# Queries the status of the translation of the document with the given +document_handle+.
|
|
41
|
-
#
|
|
42
|
-
# @param [DeepL::Resources::DocumentHandle] document_handle Handle returned by the `upload`
|
|
43
|
-
# method.
|
|
44
|
-
# @param [Hash] options Additional options for the upload.
|
|
45
|
-
# @param [Hash] additional_headers Additional HTTP headers for the status check.
|
|
46
|
-
# @return [DeepL::Resources::DocumentTranslationStatus] Status of the document translation.
|
|
47
|
-
|
|
48
|
-
def get_status(document_handle, options = {}, additional_headers = {})
|
|
49
|
-
DeepL::Requests::Document::GetStatus.new(@api, document_handle.document_id,
|
|
50
|
-
document_handle.document_key, options,
|
|
51
|
-
additional_headers).request
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
##
|
|
55
|
-
# Downloads the document identified by the +document_handle+ to +output_file+
|
|
56
|
-
#
|
|
57
|
-
# @param [DeepL::Resources::DocumentHandle] document_handle Handle returned by the `upload`
|
|
58
|
-
# method.
|
|
59
|
-
# @param [String] output_file Path to the file to write to. Will be overwritten if the file
|
|
60
|
-
# already exists.
|
|
61
|
-
# @return [DeepL::Resources::DocumentTranslationStatus] Status of the document translation.
|
|
62
|
-
|
|
63
|
-
def download(document_handle, output_file)
|
|
64
|
-
DeepL::Requests::Document::Download.new(@api, document_handle.document_id,
|
|
65
|
-
document_handle.document_key, output_file).request
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
##
|
|
69
|
-
# Translates a document with the DeepL API, `sleep`ing during waiting periods. Returns the
|
|
70
|
-
# status that was queried last. This can be either because the document translation terminated
|
|
71
|
-
# (successfully or with an error) or because the maximum number of status requests have been
|
|
72
|
-
# made. See the parameter `max_doc_status_queries` for details.
|
|
73
|
-
#
|
|
74
|
-
# @raise [DocumentTranslationError] If any error occurs during the process.
|
|
75
|
-
#
|
|
76
|
-
# @param [String] input_file Path to the file to be translated
|
|
77
|
-
# @param [String] output_file Path to the file to write to. Will be overwritten if the file
|
|
78
|
-
# already exists.
|
|
79
|
-
# @param [String, nil] source_lang Source language to use for the translation. `nil` will cause
|
|
80
|
-
# automatic source langauge detection to be used. Must be
|
|
81
|
-
# formatted as ISO 639-1, 2-letter language codes.
|
|
82
|
-
# @param [String] target_lang Target language to use for the translation. Must be formatted as
|
|
83
|
-
# ISO 639-1, 2-letter language codes, plus a hyphen "-" with the
|
|
84
|
-
# variant identifier for languages with variants/dialects/... .
|
|
85
|
-
# @param [String, nil] filename The filename of the file, including its extension. Used to open
|
|
86
|
-
# the different kinds of documents (PDFs, etc). If nil, will use
|
|
87
|
-
# the filename of +input_file_path+.
|
|
88
|
-
# @param [Hash] options Additional options for the upload.
|
|
89
|
-
# @param [Hash] additional_headers Additional headers for the upload.
|
|
90
|
-
# @return [DeepL::Resources::DocumentTranslationStatus] Status of the document translation.
|
|
91
|
-
|
|
92
|
-
def translate_document(input_file, output_file, source_lang, target_lang, # rubocop:disable Metrics/MethodLength,Metrics/ParameterLists
|
|
93
|
-
filename = nil, options = {}, additional_headers = {})
|
|
94
|
-
raise IOError 'File already exists at output path' if File.exist?(output_file)
|
|
95
|
-
|
|
96
|
-
begin
|
|
97
|
-
handle = upload(input_file, source_lang, target_lang, filename, options,
|
|
98
|
-
additional_headers)
|
|
99
|
-
translate_document_wait_and_download(handle, output_file)
|
|
100
|
-
rescue StandardError => e
|
|
101
|
-
FileUtils.rm_f(output_file)
|
|
102
|
-
raise Exceptions::DocumentTranslationError.new(
|
|
103
|
-
"Error occurred during document translation: #{e.message}", handle
|
|
104
|
-
)
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
private
|
|
109
|
-
|
|
110
|
-
def translate_document_wait_and_download(document_handle, output_file)
|
|
111
|
-
doc_status = document_handle.wait_until_document_translation_finished
|
|
112
|
-
if doc_status.error?
|
|
113
|
-
raise Exceptions::DocumentTranslationError.new(
|
|
114
|
-
"Exception when querying document status #{doc_status.error_message}", document_handle
|
|
115
|
-
)
|
|
116
|
-
else
|
|
117
|
-
download(document_handle, output_file)
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
end
|
|
121
|
-
end
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
# Copyright 2018 Daniel Herzog
|
|
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
|
-
class AuthorizationFailed < RequestError
|
|
9
|
-
def message
|
|
10
|
-
'Authorization failed. Please supply a valid auth_key parameter.'
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# Copyright 2018 Daniel Herzog
|
|
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
|
-
class BadRequest < RequestError
|
|
9
|
-
def message
|
|
10
|
-
JSON.parse(response.body)['message']
|
|
11
|
-
rescue JSON::ParserError
|
|
12
|
-
response.body
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
end
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
# Copyright 2024 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
|
-
class DocumentTranslationError < Error
|
|
9
|
-
def initialize(message, handle)
|
|
10
|
-
super(message)
|
|
11
|
-
@handle = handle
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
# Copyright 2018 Daniel Herzog
|
|
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
|
-
class Error < StandardError
|
|
9
|
-
def should_retry?
|
|
10
|
-
false
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
# Copyright 2018 Daniel Herzog
|
|
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
|
-
class LimitExceeded < RequestError
|
|
9
|
-
def message
|
|
10
|
-
'Limit exceeded. Please wait and send your request once again.'
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def should_retry?
|
|
14
|
-
true
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
end
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# Copyright 2022 Daniel Herzog
|
|
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
|
-
class NotFound < RequestError
|
|
9
|
-
def message
|
|
10
|
-
JSON.parse(response.body)['message']
|
|
11
|
-
rescue JSON::ParserError
|
|
12
|
-
response.body
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
end
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
# Copyright 2022 Daniel Herzog
|
|
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
|
-
class NotSupported < Error
|
|
9
|
-
def initialize(msg = 'The feature is not supported')
|
|
10
|
-
super
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
# Copyright 2018 Daniel Herzog
|
|
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
|
-
class QuotaExceeded < RequestError
|
|
9
|
-
def message
|
|
10
|
-
'Quota exceeded. The character limit has been reached.'
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
# Copyright 2022 Daniel Herzog
|
|
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
|
-
class RequestEntityTooLarge < RequestError
|
|
9
|
-
def message
|
|
10
|
-
'The request size has reached the supported limit. ' \
|
|
11
|
-
"Make sure that you're not sending more than 50 text parts."
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
# Copyright 2018 Daniel Herzog
|
|
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
|
-
class RequestError < Error
|
|
9
|
-
attr_reader :request, :response
|
|
10
|
-
|
|
11
|
-
def initialize(response)
|
|
12
|
-
super()
|
|
13
|
-
@response = response
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def message
|
|
17
|
-
'Unknown error.'
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
# Copyright 2024 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
|
-
class ServerError < RequestError
|
|
9
|
-
def message
|
|
10
|
-
'An internal server error occured. Try again after waiting a short period.'
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def should_retry?
|
|
14
|
-
true
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
end
|
data/lib/deepl/glossary_api.rb
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
# Copyright 2022 Daniel Herzog
|
|
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
|
-
class GlossaryApi
|
|
8
|
-
def initialize(api, options = {})
|
|
9
|
-
@api = api
|
|
10
|
-
@options = options
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def create(name, source_lang, target_lang, entries, options = {})
|
|
14
|
-
DeepL::Requests::Glossary::Create.new(@api, name, source_lang, target_lang, entries, options)
|
|
15
|
-
.request
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def destroy(glossary_id, options = {})
|
|
19
|
-
DeepL::Requests::Glossary::Destroy.new(@api, glossary_id, options).request
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def entries(glossary_id, options = {})
|
|
23
|
-
DeepL::Requests::Glossary::Entries.new(@api, glossary_id, options).request
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def find(glossary_id, options = {})
|
|
27
|
-
DeepL::Requests::Glossary::Find.new(@api, glossary_id, options).request
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def language_pairs(options = {})
|
|
31
|
-
DeepL::Requests::Glossary::LanguagePairs.new(@api, options).request
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def list(options = {})
|
|
35
|
-
DeepL::Requests::Glossary::List.new(@api, options).request
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
end
|
data/lib/deepl/requests/base.rb
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
# Copyright 2018 Daniel Herzog
|
|
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
|
-
class Base # rubocop:disable Metrics/ClassLength
|
|
9
|
-
attr_reader :api, :response, :options
|
|
10
|
-
|
|
11
|
-
def initialize(api, options = {}, additional_headers = {})
|
|
12
|
-
@api = api
|
|
13
|
-
@options = options
|
|
14
|
-
@additional_headers = additional_headers
|
|
15
|
-
@num_retries = 0
|
|
16
|
-
@backoff_timer = Utils::BackoffTimer.new
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def request
|
|
20
|
-
raise NotImplementedError
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def details
|
|
24
|
-
"HTTP Headers #{headers}"
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
private
|
|
28
|
-
|
|
29
|
-
def option?(name)
|
|
30
|
-
options.key?(name.to_s) || options.key?(name.to_sym)
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
def option(name)
|
|
34
|
-
options[name.to_s] || options[name.to_sym]
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def delete_option(name)
|
|
38
|
-
options.delete(name.to_s) || options.delete(name.to_sym)
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def set_option(name, value)
|
|
42
|
-
if options.key?(name.to_sym)
|
|
43
|
-
options[name.to_sym] = value
|
|
44
|
-
else
|
|
45
|
-
options[name.to_s] = value
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
# Files to reset: list of file objects to rewind when retrying the request
|
|
50
|
-
def execute_request_with_retries(req, files_to_reset = []) # rubocop:disable all
|
|
51
|
-
api.configuration.logger&.info("Request to the DeepL API: #{self}")
|
|
52
|
-
api.configuration.logger&.debug("Request details: #{details}")
|
|
53
|
-
loop do
|
|
54
|
-
resp = api.http_client.request(req)
|
|
55
|
-
validate_response!(resp)
|
|
56
|
-
return [req, resp]
|
|
57
|
-
rescue DeepL::Exceptions::Error => e
|
|
58
|
-
raise e unless should_retry?(resp, e, @backoff_timer.num_retries)
|
|
59
|
-
|
|
60
|
-
unless e.nil?
|
|
61
|
-
api.configuration.logger&.info("Encountered a retryable exception: #{e.message}")
|
|
62
|
-
end
|
|
63
|
-
api.configuration.logger&.info("Starting retry #{@backoff_timer.num_retries + 1} for " \
|
|
64
|
-
"request #{request} after sleeping for " \
|
|
65
|
-
"#{format('%.2f', @backoff_timer.time_until_deadline)}")
|
|
66
|
-
files_to_reset.each(&:rewind)
|
|
67
|
-
@backoff_timer.sleep_until_deadline
|
|
68
|
-
next
|
|
69
|
-
rescue Net::HTTPBadResponse, Net::HTTPServerError, Net::HTTPFatalError, Timeout::Error,
|
|
70
|
-
SocketError => e
|
|
71
|
-
unless e.nil?
|
|
72
|
-
api.configuration.logger&.info("Encountered a retryable exception: #{e.message}")
|
|
73
|
-
end
|
|
74
|
-
api.configuration.logger&.info("Starting retry #{@backoff_timer.num_retries + 1} for " \
|
|
75
|
-
"request #{request} after sleeping for " \
|
|
76
|
-
"#{format('%.2f', @backoff_timer.time_until_deadline)}")
|
|
77
|
-
files_to_reset.each(&:rewind)
|
|
78
|
-
@backoff_timer.sleep_until_deadline
|
|
79
|
-
next
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
def should_retry?(response, exception, num_retries)
|
|
84
|
-
return false if num_retries >= api.configuration.max_network_retries
|
|
85
|
-
return exception.should_retry? if response.nil?
|
|
86
|
-
|
|
87
|
-
response.is_a?(Net::HTTPTooManyRequests) || response.is_a?(Net::HTTPInternalServerError)
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
def post_request(payload)
|
|
91
|
-
http_headers = add_json_content_type(headers)
|
|
92
|
-
post_req = Net::HTTP::Post.new(uri.path, http_headers)
|
|
93
|
-
post_req.body = payload.merge(options).to_json
|
|
94
|
-
post_req
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
def post_request_with_file(form_data)
|
|
98
|
-
http_headers = add_multipart_form_content_type(headers)
|
|
99
|
-
post_req = Net::HTTP::Post.new(uri.request_uri, http_headers)
|
|
100
|
-
# options are passed in `form_data`
|
|
101
|
-
form_data += options.map { |key, value| [key.to_s, value.to_s] }
|
|
102
|
-
post_req.set_form(form_data, 'multipart/form-data')
|
|
103
|
-
post_req
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
def get_request # rubocop:disable Naming/AccessorMethodName
|
|
107
|
-
http_headers = add_json_content_type(headers)
|
|
108
|
-
get_req = Net::HTTP::Get.new(uri.path, http_headers)
|
|
109
|
-
get_req.body = options.to_json
|
|
110
|
-
get_req
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
def delete_request
|
|
114
|
-
http_headers = add_json_content_type(headers)
|
|
115
|
-
del_req = Net::HTTP::Delete.new(uri.path, http_headers)
|
|
116
|
-
del_req.body = options.to_json
|
|
117
|
-
del_req
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
def validate_response!(response)
|
|
121
|
-
return if response.is_a?(Net::HTTPSuccess)
|
|
122
|
-
|
|
123
|
-
raise Utils::ExceptionBuilder.new(response).build
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
def path
|
|
127
|
-
raise NotImplementedError
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
def url
|
|
131
|
-
"#{host}/#{api.configuration.version}/#{path}"
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
def uri
|
|
135
|
-
@uri ||= URI(url)
|
|
136
|
-
@uri
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
def host
|
|
140
|
-
api.configuration.host
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
def query_params
|
|
144
|
-
options
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
def headers
|
|
148
|
-
{ 'Authorization' => "DeepL-Auth-Key #{api.configuration.auth_key}",
|
|
149
|
-
'User-Agent' => api.configuration.user_agent }.merge(@additional_headers)
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
def add_json_content_type(headers_to_add_to)
|
|
153
|
-
headers_to_add_to['Content-Type'] = 'application/json'
|
|
154
|
-
headers_to_add_to
|
|
155
|
-
end
|
|
156
|
-
|
|
157
|
-
def add_multipart_form_content_type(headers_to_add_to)
|
|
158
|
-
headers_to_add_to['Content-Type'] = 'multipart/form-data'
|
|
159
|
-
headers_to_add_to
|
|
160
|
-
end
|
|
161
|
-
end
|
|
162
|
-
end
|
|
163
|
-
end
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
# Copyright 2024 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 Requests
|
|
8
|
-
module Document
|
|
9
|
-
class Download < Base
|
|
10
|
-
attr_reader :document_id, :document_key
|
|
11
|
-
|
|
12
|
-
def initialize(api, document_id, document_key, output_file)
|
|
13
|
-
super(api, {})
|
|
14
|
-
@document_id = document_id
|
|
15
|
-
@document_key = document_key
|
|
16
|
-
@output_file = output_file
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def request
|
|
20
|
-
payload = { document_key: document_key }
|
|
21
|
-
extract_file(*execute_request_with_retries(post_request(payload)))
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def details
|
|
25
|
-
"HTTP Headers: #{headers}\nPayload #{{ document_key: document_key }}"
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def to_s
|
|
29
|
-
"POST #{uri.request_uri}"
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
private
|
|
33
|
-
|
|
34
|
-
def extract_file(_request, response)
|
|
35
|
-
File.write(@output_file, response.body)
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def path
|
|
39
|
-
"document/#{document_id}/result"
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
end
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
# Copyright 2024 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 Requests
|
|
8
|
-
module Document
|
|
9
|
-
class GetStatus < Base
|
|
10
|
-
attr_reader :document_id, :document_key
|
|
11
|
-
|
|
12
|
-
def initialize(api, document_id, document_key, options = {}, additional_headers = {})
|
|
13
|
-
super(api, options, additional_headers)
|
|
14
|
-
@document_id = document_id
|
|
15
|
-
@document_key = document_key
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def request
|
|
19
|
-
payload = { document_key: document_key }
|
|
20
|
-
build_doc_translation_status(*execute_request_with_retries(post_request(payload)))
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def details
|
|
24
|
-
"HTTP Headers: #{headers}\nPayload #{{ document_key: document_key }}"
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def to_s
|
|
28
|
-
"POST #{uri.request_uri}"
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
private
|
|
32
|
-
|
|
33
|
-
def build_doc_translation_status(request, response)
|
|
34
|
-
document_translation_status = JSON.parse(response.body)
|
|
35
|
-
Resources::DocumentTranslationStatus.new(document_translation_status, request, response)
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def path
|
|
39
|
-
"document/#{document_id}"
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
end
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
# Copyright 2024 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 Requests
|
|
8
|
-
module Document
|
|
9
|
-
class Upload < Base
|
|
10
|
-
attr_reader :input_file_path, :source_lang, :target_lang, :filename
|
|
11
|
-
|
|
12
|
-
SUPPORTED_OPTIONS = %w[formality glossary_id output_format].freeze
|
|
13
|
-
|
|
14
|
-
def initialize(api, input_file_path, source_lang, target_lang, filename = nil, # rubocop:disable Metrics/ParameterLists
|
|
15
|
-
options = {}, additional_headers = {})
|
|
16
|
-
super(api, options, additional_headers)
|
|
17
|
-
@input_file_path = input_file_path
|
|
18
|
-
@source_lang = source_lang
|
|
19
|
-
@target_lang = target_lang
|
|
20
|
-
@filename = filename
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def request # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
|
|
24
|
-
input_file = File.open(input_file_path, 'rb')
|
|
25
|
-
form_data = [
|
|
26
|
-
['file', input_file], ['source_lang', source_lang],
|
|
27
|
-
['target_lang', target_lang]
|
|
28
|
-
]
|
|
29
|
-
filename_param = filename || File.basename(input_file_path)
|
|
30
|
-
form_data.push(['filename', filename_param]) unless filename_param.nil?
|
|
31
|
-
# Manually add options due to multipart/form-data request
|
|
32
|
-
SUPPORTED_OPTIONS.each do |option|
|
|
33
|
-
form_data.push([option, options[option]]) unless options[option].nil?
|
|
34
|
-
end
|
|
35
|
-
build_doc_handle(*execute_request_with_retries(post_request_with_file(form_data),
|
|
36
|
-
[input_file]))
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def details
|
|
40
|
-
"HTTP Headers: #{headers}\nPayload #{[
|
|
41
|
-
['file', "File at #{input_file_path} opened in binary mode"],
|
|
42
|
-
['source_lang', source_lang], ['target_lang', target_lang], ['filename', filename]
|
|
43
|
-
]}"
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def to_s
|
|
47
|
-
"POST #{uri.request_uri}"
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
private
|
|
51
|
-
|
|
52
|
-
def build_doc_handle(request, response)
|
|
53
|
-
parsed_response = JSON.parse(response.body)
|
|
54
|
-
Resources::DocumentHandle.new(parsed_response['document_id'],
|
|
55
|
-
parsed_response['document_key'], request, response)
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def path
|
|
59
|
-
'document'
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
end
|