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



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

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



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



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

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, 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 (OpenStruct)

    Diffend config

  • payload (Hash)

    with versions to check

Returns:

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


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

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



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

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(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(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(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(message, retry_count) ⇒ Object

Handle retry

Parameters:

  • message (String)

    message we want to display

  • retry_count (Integer)


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

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

.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)


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

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