Module: Diffend::Request

Defined in:
lib/diffend/request.rb

Overview

Module responsible for doing request to Diffend

Constant Summary collapse

CONNECTION_MESSAGE =

Message displayed when connection issue occured and we will retry

'We experienced a connection issue, retrying...'
CONNECTION_EXCEPTIONS =

List of connection exceptions

[
  Errno::ECONNRESET,
  Errno::ENETUNREACH,
  Errno::EHOSTUNREACH,
  Errno::ECONNREFUSED,
  SocketError
].freeze
TIMEOUT_MESSAGE =

Message displayed when timeout occured and we will retry

'We experienced a connection issue, retrying...'
TIMEOUT_EXCEPTIONS =

List of timeout exceptions

[
  Net::OpenTimeout,
  Net::ReadTimeout
].freeze
SERVER_ERROR_MESSAGE =

Message displayed when server issue occured and we will retry

'We experienced a server-side issue, retrying...'
SERVER_ERRORS =

List of server issues

500 - Internal Server Error 502 - Bad Gateway 503 - Service Unavailable 504 - Gateway Timeout

[500, 502, 503, 504].freeze
RETRIES =

Number of retries

3

Class Method Summary collapse

Class Method Details

.assign_auth(request, config) ⇒ Object

Assigns basic authorization if provided in the config

Parameters:

  • request (Net::HTTP::Post)

    prepared http post

  • config (Diffend::Config)


165
166
167
168
169
170
# File 'lib/diffend/request.rb', line 165

def assign_auth(request, config)
  return unless config.shareable_id
  return unless config.shareable_key

  request.basic_auth(config.shareable_id, config.shareable_key)
end

.assign_payload(request, payload) ⇒ Object

Assigns payload as json

Parameters:

  • request (Net::HTTP::Post)

    prepared http post

  • payload (Hash)

    with versions to check



176
177
178
# File 'lib/diffend/request.rb', line 176

def assign_payload(request, payload)
  request.body = JSON.dump(payload: payload)
end

.build_http(url) ⇒ Object

Builds http connection object

Parameters:

  • url (String)

    command endpoint url



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/diffend/request.rb', line 120

def build_http(url)
  uri = URI(url)

  Net::HTTP.start(
    uri.host,
    uri.port,
    use_ssl: uri.scheme == 'https',
    open_timeout: 15,
    read_timeout: 15
  ) { |http| yield(http, uri) }
end

.build_request(uri, request_method, config, payload) ⇒ Net::HTTP::Post, Net::HTTP::Put

Build http post request and assigns headers and payload

Parameters:

  • uri (URI::HTTPS)
  • request_method (Symbol)
  • config (Diffend::Config)
  • payload (Hash)

    with versions to check

Returns:

  • (Net::HTTP::Post, Net::HTTP::Put)


140
141
142
143
144
145
# File 'lib/diffend/request.rb', line 140

def build_request(uri, request_method, config, payload)
  pick_request_method(request_method)
    .new(uri.request_uri, HEADERS)
    .tap { |request| assign_auth(request, config) }
    .tap { |request| assign_payload(request, payload) }
end

.call(request_object) ⇒ Net::HTTPResponse

Execute request

Parameters:

Returns:

  • (Net::HTTPResponse)

    response from Diffend



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/diffend/request.rb', line 49

def call(request_object)
  retry_count ||= -1

  build_http(request_object.url) do |http, uri|
    response = http.request(
      build_request(
        uri,
        request_object.request_method,
        request_object.config,
        request_object.payload
      )
    )

    if SERVER_ERRORS.include?(response.code.to_i)
      raise Diffend::Errors::RequestServerError, response.code.to_i
    end

    response
  end
rescue Diffend::Errors::RequestServerError => e
  retry_count += 1

  retry if handle_retry(request_object.config, SERVER_ERROR_MESSAGE, retry_count)

  Diffend::HandleErrors::Report.call(
    exception: e,
    payload: request_object.payload,
    config: request_object.config,
    message: :request_error
  )
rescue *CONNECTION_EXCEPTIONS => e
  retry_count += 1

  retry if handle_retry(request_object.config, CONNECTION_MESSAGE, retry_count)

  Diffend::HandleErrors::Report.call(
    exception: e,
    payload: request_object.payload,
    config: request_object.config,
    message: :request_error
  )
rescue *TIMEOUT_EXCEPTIONS => e
  retry_count += 1

  retry if handle_retry(request_object.config, TIMEOUT_MESSAGE, retry_count)

  Diffend::HandleErrors::Report.call(
    exception: e,
    payload: request_object.payload,
    config: request_object.config,
    message: :request_error
  )
end

.exponential_backoff(retry_count) ⇒ Object



180
181
182
# File 'lib/diffend/request.rb', line 180

def exponential_backoff(retry_count)
  2**(retry_count + 1)
end

.handle_retry(config, message, retry_count) ⇒ Object

Handle retry

Parameters:

  • config (Diffend::Config)
  • message (String)

    message we want to display

  • retry_count (Integer)


108
109
110
111
112
113
114
115
# File 'lib/diffend/request.rb', line 108

def handle_retry(config, message, retry_count)
  return false if retry_count == RETRIES

  config.logger.warn(message)
  sleep(exponential_backoff(retry_count))

  retry_count < RETRIES
end

.pick_request_method(request_method) ⇒ Net::HTTP::Post, Net::HTTP::Put

Pick request method

Parameters:

  • request_method (Symbol)

Returns:

  • (Net::HTTP::Post, Net::HTTP::Put)


152
153
154
155
156
157
158
159
# File 'lib/diffend/request.rb', line 152

def pick_request_method(request_method)
  case request_method
  when :post
    Net::HTTP::Post
  when :put
    Net::HTTP::Put
  end
end