Class: DeepL::Requests::Base

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

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Modules: ResponseFormatGuard Classes: BodilessPost

Constant Summary collapse

MAX_GLOSSARY_IDS =

Maximum number of glossaries that may be provided via the glossary_ids option.

5
RETRYABLE_NETWORK_ERRORS =

Transient network failures, which are retried: DNS (SocketError), timeouts (Net::OpenTimeout and Net::ReadTimeout are Timeout::Errors), refused or reset connections, broken pipes and unreachable hosts (SystemCallError), and connections closed early.

[Timeout::Error, SocketError, SystemCallError, EOFError].freeze
NETWORK_ERRORS =

Network failures without an HTTP response from the API. TLS failures, malformed responses and proxy failures (Net::ProtocolError, raised when a proxy refuses CONNECT, e.g. with 407) are not transient and are raised without retrying.

[*RETRYABLE_NETWORK_ERRORS, Net::HTTPBadResponse,
OpenSSL::SSL::SSLError, Net::ProtocolError].freeze
RESPONSE_FORMAT_ERRORS =

Errors raised while building the result from a received response: the response has an unexpected format.

[JSON::ParserError, NoMethodError, TypeError, KeyError].freeze
TECHNICAL_HEADERS =

Headers that describe the request the SDK builds; a caller's value is dropped.

%w[content-type content-length transfer-encoding host].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, options = {}, additional_headers = {}) ⇒ Base

Options set to nil are unset and not sent.



79
80
81
82
83
84
85
86
# File 'lib/deepl/requests/base.rb', line 79

def initialize(api, options = {}, additional_headers = {})
  @api = api
  self.class.validate_options!(options)
  @options = options.compact
  @additional_headers = additional_headers
  @num_retries = 0
  @backoff_timer = Utils::BackoffTimer.new
end

Instance Attribute Details

#api ⇒ Object (readonly)

Returns the value of attribute api.



76
77
78
# File 'lib/deepl/requests/base.rb', line 76

def api
  @api
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



76
77
78
# File 'lib/deepl/requests/base.rb', line 76

def options
  @options
end

#response ⇒ Object (readonly)

Returns the value of attribute response.



76
77
78
# File 'lib/deepl/requests/base.rb', line 76

def response
  @response
end

Class Method Details

.accepted_options ⇒ Object



61
62
63
# File 'lib/deepl/requests/base.rb', line 61

def self.accepted_options
  @accepted_options || []
end

.accepts_options(*names) ⇒ Object

Declares the options a request class accepts besides extra_body_parameters. Any other option raises ArgumentError before a request is sent.



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

def self.accepts_options(*names)
  @accepted_options = names.map(&:to_s).freeze
end

.inherited(subclass) ⇒ Object



50
51
52
53
# File 'lib/deepl/requests/base.rb', line 50

def self.inherited(subclass)
  super
  subclass.prepend(ResponseFormatGuard)
end

.validate_options!(options) ⇒ Object

Raises:

  • (ArgumentError) —

    If options has an option the class does not accept.



66
67
68
69
70
71
72
73
74
# File 'lib/deepl/requests/base.rb', line 66

def self.validate_options!(options)
  accepted = accepted_options + ['extra_body_parameters']
  unknown = options.keys.reject { |name| accepted.include?(name.to_s) }
  return if unknown.empty?

  names = unknown.map { |name| "`#{name}`" }.join(', ')
  raise ArgumentError, "Unknown option#{'s' if unknown.size > 1} #{names}; use " \
                       '`extra_body_parameters` to send fields the SDK does not model'
end

Instance Method Details

#details ⇒ Object



92
93
94
# File 'lib/deepl/requests/base.rb', line 92

def details
  "HTTP Headers #{loggable_headers}"
end

#request ⇒ Object

Raises:

  • (NotImplementedError)


88
89
90
# File 'lib/deepl/requests/base.rb', line 88

def request
  raise NotImplementedError
end