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
].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 (OpenStruct) —

    Diffend config



144
145
146
147
148
149
150
# File 'lib/diffend/request.rb', line 144

def assign_auth(request, config)
  return unless 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



156
157
158
# File 'lib/diffend/request.rb', line 156

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



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/diffend/request.rb', line 113

def build_http(url)
  uri = URI(url)

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

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

Build http post request and assigns headers and payload

Parameters:

  • uri (URI::HTTPS)
  • config (OpenStruct) —

    Diffend config

  • payload (Hash) —

    with versions to check

Returns:

  • (Net::HTTP::Post)


133
134
135
136
137
138
# File 'lib/diffend/request.rb', line 133

def build_request(uri, config, payload)
  Net::HTTP::Post
    .new(uri.request_uri, HEADERS)
    .tap { |request| assign_auth(request, config) }
    .tap { |request| assign_payload(request, payload) }
end

.call(config, endpoint_url, payload) ⇒ Net::HTTPResponse

Execute request

Parameters:

  • config (OpenStruct) —

    diffend config

  • endpoint_url (String)
  • payload (Hash)

Returns:

  • (Net::HTTPResponse) —

    response from Diffend



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
# File 'lib/diffend/request.rb', line 50

def call(config, endpoint_url, payload)
  retry_count ||= -1

  build_http(endpoint_url) do |http, uri|
    response = http.request(build_request(uri, config, 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(SERVER_ERROR_MESSAGE, retry_count)

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

  retry if handle_retry(CONNECTION_MESSAGE, retry_count)

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

  retry if handle_retry(TIMEOUT_MESSAGE, retry_count)

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

.exponential_backoff(retry_count) ⇒ Object



160
161
162
# File 'lib/diffend/request.rb', line 160

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

.handle_retry(message, retry_count) ⇒ Object

Handle retry

Parameters:

  • message (String) —

    message we want to display

  • retry_count (Integer)


101
102
103
104
105
106
107
108
# File 'lib/diffend/request.rb', line 101

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

  Bundler.ui.error(message)
  sleep(exponential_backoff(retry_count))

  retry_count < RETRIES
end