Module: KineticSdk::Utils::KineticHttpUtils

Included in:
Agent, Bridgehub, Core, CustomHttp, Discussions, Filehub, Task, KineticHttp
Defined in:
lib/kinetic_sdk/utils/kinetic-http.rb,
lib/kinetic_sdk/utils/kinetic-http-headers.rb

Overview

The KineticHttpUtils module provides common HTTP methods, and returns a KineticHttpResponse object with all methods. The raw Net::HTTPResponse is available by calling the KineticSdk::Utils::KineticHttpResponse#response method.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_headers(username = @username, password = @password) ⇒ Hash

Provides a hash of default headers

Parameters:

  • username (String) (defaults to: @username)

    username to authenticate

  • password (String) (defaults to: @password)

    password associated to the username

Returns:

  • (Hash)

    Hash of headers

    • Accept: application/json
    • Authorization: Basic base64 hash of username and password if username is provided
    • Content-Type: application/json
    • User-Agent: Kinetic Ruby SDK VERSION


128
129
130
131
132
# File 'lib/kinetic_sdk/utils/kinetic-http-headers.rb', line 128

def self.default_headers(username=@username, password=@password)
  headers = self.header_accept_json.merge(self.header_content_json).merge(self.header_user_agent)
  headers.merge!(self.header_basic_auth(username, password)) unless username.nil?
  headers
end

.default_jwt_headers(token = @jwt) ⇒ Hash

Provides a hash of default headers with bearer auth instead of basic auth

Parameters:

  • token (String) (defaults to: @jwt)

    JSON Web Token (jwt)

Returns:

  • (Hash)

    Hash of headers

    • Accepts: application/json
    • Authorization: Bearer jwt
    • Content-Type: application/json
    • User-Agent: Kinetic Ruby SDK VERSION


142
143
144
145
146
# File 'lib/kinetic_sdk/utils/kinetic-http-headers.rb', line 142

def self.default_jwt_headers(token=@jwt)
  headers = self.header_accept_json.merge(self.header_content_json).merge(self.header_user_agent)
  headers.merge!(self.header_bearer_auth(token)) unless token.nil?
  headers
end

.header_accept_jsonHash

Provides a accepts header set to application/json

Returns:

  • (Hash)

    Accepts header set to application/json



84
85
86
# File 'lib/kinetic_sdk/utils/kinetic-http-headers.rb', line 84

def self.header_accept_json
  { "Accepts" => "application/json" }
end

.header_basic_auth(username = @username, password = @password) ⇒ Hash

Provides a basic authentication header

Parameters:

  • username (String) (defaults to: @username)

    username to authenticate

  • password (String) (defaults to: @password)

    password associated to the username

Returns:

  • (Hash)

    Authorization: Basic base64 hash of username and password



93
94
95
# File 'lib/kinetic_sdk/utils/kinetic-http-headers.rb', line 93

def self.header_basic_auth(username=@username, password=@password)
  { "Authorization" => "Basic #{Base64.encode64(username.to_s + ":" + password.to_s).gsub("\n", "")}" }
end

.header_bearer_auth(token = @jwt) ⇒ Hash

Provides a Bearer authentication header

Parameters:

  • token (String) (defaults to: @jwt)

    JSON Web Token (jwt)

Returns:

  • (Hash)

    Authorization: Bearer jwt



101
102
103
# File 'lib/kinetic_sdk/utils/kinetic-http-headers.rb', line 101

def self.header_bearer_auth(token=@jwt)
  { "Authorization" => "Bearer #{token}" }
end

.header_content_jsonHash

Provides a content-type header set to application/json

Returns:

  • (Hash)

    Content-Type header set to application/json



108
109
110
# File 'lib/kinetic_sdk/utils/kinetic-http-headers.rb', line 108

def self.header_content_json
  { "Content-Type" => "application/json" }
end

.header_user_agentHash

Provides a user-agent header set to Kinetic Ruby SDK

Returns:

  • (Hash)

    User-Agent header set to Kinetic Reuby SDK



115
116
117
# File 'lib/kinetic_sdk/utils/kinetic-http-headers.rb', line 115

def self.header_user_agent
  { "User-Agent" => "Kinetic Ruby SDK #{KineticSdk::VERSION}" }
end

Instance Method Details

#default_headers(username = @username, password = @password) ⇒ Hash

Provides a hash of default headers

Parameters:

  • username (String) (defaults to: @username)

    username to authenticate

  • password (String) (defaults to: @password)

    password associated to the username

Returns:

  • (Hash)

    Hash of headers

    • Accepts: application/json
    • Authorization: Basic base64 hash of username and password if username is provided
    • Content-Type: application/json
    • User-Agent: Kinetic Ruby SDK VERSION


56
57
58
59
60
# File 'lib/kinetic_sdk/utils/kinetic-http-headers.rb', line 56

def default_headers(username=@username, password=@password)
  headers = header_accept_json.merge(header_content_json).merge(header_user_agent)
  headers.merge!(header_basic_auth(username, password)) unless username.nil?
  headers
end

#default_jwt_headers(token = @jwt) ⇒ Hash

Provides a hash of default headers with bearer auth instead of basic auth

Parameters:

  • token (String) (defaults to: @jwt)

    JSON Web Token (jwt)

Returns:

  • (Hash)

    Hash of headers

    • Accepts: application/json
    • Authorization: Bearer jwt
    • Content-Type: application/json
    • User-Agent: Kinetic Ruby SDK VERSION


70
71
72
73
74
# File 'lib/kinetic_sdk/utils/kinetic-http-headers.rb', line 70

def default_jwt_headers(token=@jwt)
  headers = header_accept_json.merge(header_content_json).merge(header_user_agent)
  headers.merge!(header_bearer_auth(token)) unless token.nil?
  headers
end

#delete(url, headers = {}, http_options = @options) ⇒ KineticSdk::Utils::KineticHttpResponse

Send an HTTP DELETE request

Parameters:

  • url (String)

    url to send the request to

  • headers (Hash) (defaults to: {})

    hash of headers to send

  • http_options (Hash) (defaults to: @options)

    hash of http options

Options Hash (http_options):

  • :max_redirects (Fixnum)

    optional - max number of times to redirect

  • :gateway_retry_limit (Fixnum)

    optional - max number of times to retry a bad gateway

  • :gateway_retry_delay (Float)

    optional - number of seconds to delay before retrying a bad gateway

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
# File 'lib/kinetic_sdk/utils/kinetic-http.rb', line 33

def delete(url, headers={}, http_options=@options)
  # determine the http options
  redirect_limit = http_options[:max_redirects] || max_redirects
  gateway_retries = http_options[:gateway_retry_limit] || gateway_retry_limit
  gateway_delay = http_options[:gateway_retry_delay] || gateway_retry_delay

  # parse the URL
  uri = URI.parse(url)

  @logger.debug("DELETE #{uri}  #{headers.inspect}")

  # build the http object
  http = build_http(uri)
  # build the request
  request = Net::HTTP::Delete.new(uri.request_uri, headers)

  # send the request
  begin
    response = http.request(request)
    # handle the response
    case response
    # handle 302
    when Net::HTTPRedirection then
      if redirect_limit == -1
        @logger.info("HTTP response code: #{response.code}") unless @logger.debug?
        KineticHttpResponse.new(response)
      elsif redirect_limit == 0
        raise Net::HTTPFatalError.new("Too many redirects", response)
      else
        delete(response['location'], headers, http_options.merge({
          :max_redirects => redirect_limit - 1
        }))
      end
    # handle 502, 503, 504
    when Net::HTTPBadGateway, Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeOut then
      if gateway_retries == -1
        KineticHttpResponse.new(response)
      elsif gateway_retries == 0
        @logger.info("HTTP response: #{response.code} #{response.message}") unless @logger.debug?
        raise Net::HTTPFatalError.new("#{response.code} #{response.message}", response)
      else
        @logger.info("#{response.code} #{response.message}, retrying in #{gateway_delay} seconds")
        sleep(gateway_delay)
        delete(url, headers, http_options.merge({
          :gateway_retry_limit => gateway_retries - 1
        }))
      end
    when Net::HTTPUnknownResponse, NilClass then
      @logger.info("HTTP response code: 0") unless @logger.debug?
      e = Net::HTTPFatalError.new("Unknown response from server", response)
      KineticHttpResponse.new(e)
    else
      @logger.info("HTTP response code: #{response.code}") unless @logger.debug?
      KineticHttpResponse.new(response)
    end
  rescue Net::HTTPBadResponse => e
    @logger.info("HTTP bad response: #{e.inspect}") unless @logger.debug?
    KineticHttpResponse.new(e)
  rescue StandardError => e
    @logger.info("HTTP error: #{e.inspect}") unless @logger.debug?
    KineticHttpResponse.new(e)
  end
end

#encode(parameter) ⇒ String

Encode URI components

Parameters:

  • parameter (String)

    parameter value to encode

Returns:

  • (String)

    URL encoded parameter value



681
682
683
# File 'lib/kinetic_sdk/utils/kinetic-http.rb', line 681

def encode(parameter)
  ERB::Util.url_encode parameter
end

#gateway_retry_delayFloat

The amount of time in seconds to delay before retrying the request when a bad gateway response is encountered.

Can be passed in as an option when initializing the SDK with either the @options[:gateway_retry_delay] or @options['gateway_retry_delay'] key.

Expects a double [Float] value.

Returns:

  • (Float)

    default 1.0



744
745
746
747
748
749
750
751
# File 'lib/kinetic_sdk/utils/kinetic-http.rb', line 744

def gateway_retry_delay
  delay = @options &&
  (
    @options[:gateway_retry_delay] ||
    @options['gateway_retry_delay']
  )
  delay.nil? ? 1.0 : delay.to_f
end

#gateway_retry_limitFixnum

The maximum number of times to retry on a bad gateway response.

Can be passed in as an option when initializing the SDK with either the @options[:gateway_retry_limit] or @options['gateway_retry_limit'] key.

Expects an integer [Fixnum] value. Setting to -1 will disable retries on a bad gateway response.

Returns:

  • (Fixnum)

    default -1



725
726
727
728
729
730
731
732
# File 'lib/kinetic_sdk/utils/kinetic-http.rb', line 725

def gateway_retry_limit
  limit = @options &&
  (
    @options[:gateway_retry_limit] ||
    @options['gateway_retry_limit']
  )
  limit.nil? ? -1 : limit.to_i
end

#get(url, params = {}, headers = {}, http_options = @options) ⇒ KineticSdk::Utils::KineticHttpResponse

Send an HTTP GET request

Parameters:

  • url (String)

    url to send the request to

  • params (Hash) (defaults to: {})

    Query parameters that are added to the URL, such as +include+

  • headers (Hash) (defaults to: {})

    hash of headers to send

  • http_options (Hash) (defaults to: @options)

    hash of http options

Options Hash (http_options):

  • :max_redirects (Fixnum)

    optional - max number of times to redirect

  • :gateway_retry_limit (Fixnum)

    optional - max number of times to retry a bad gateway

  • :gateway_retry_delay (Float)

    optional - number of seconds to delay before retrying a bad gateway

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/kinetic_sdk/utils/kinetic-http.rb', line 107

def get(url, params={}, headers={}, http_options=@options)
  # determine the http options
  redirect_limit = http_options[:max_redirects] || max_redirects
  gateway_retries = http_options[:gateway_retry_limit] || gateway_retry_limit
  gateway_delay = http_options[:gateway_retry_delay] || gateway_retry_delay

  # parse the URL
  uri = URI.parse(url)
  # add URL parameters
  uri.query = URI.encode_www_form(params)

  @logger.debug("GET #{uri}  #{headers.inspect}")

  # build the http object
  http = build_http(uri)
  # build the request
  request = Net::HTTP::Get.new(uri.request_uri, headers)

  # send the request
  begin
    response = http.request(request)
    # handle the response
    case response
    # handle 302
    when Net::HTTPRedirection then
      if redirect_limit == -1
        @logger.info("HTTP response code: #{response.code}") unless @logger.debug?
        KineticHttpResponse.new(response)
      elsif redirect_limit == 0
        raise Net::HTTPFatalError.new("Too many redirects", response)
      else
        get(response['location'], params, headers, http_options.merge({
          :max_redirects => redirect_limit - 1
        }))
      end
    # handle 502, 503, 504
    when Net::HTTPBadGateway, Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeOut then
      if gateway_retries == -1
        KineticHttpResponse.new(response)
      elsif gateway_retries == 0
        @logger.info("HTTP response: #{response.code} #{response.message}") unless @logger.debug?
        raise Net::HTTPFatalError.new("#{response.code} #{response.message}", response)
      else
        @logger.info("#{response.code} #{response.message}, retrying in #{gateway_delay} seconds")
        sleep(gateway_delay)
        get(url, params, headers, http_options.merge({
          :gateway_retry_limit => gateway_retries - 1
        }))
      end
    when Net::HTTPUnknownResponse, NilClass then
      @logger.info("HTTP response code: 0") unless @logger.debug?
      e = Net::HTTPFatalError.new("Unknown response from server", response)
      KineticHttpResponse.new(e)
    else
      @logger.info("HTTP response code: #{response.code}") unless @logger.debug?
      KineticHttpResponse.new(response)
    end
  rescue Net::HTTPBadResponse => e
    @logger.info("HTTP bad response: #{e.inspect}") unless @logger.debug?
    KineticHttpResponse.new(e)
  rescue StandardError => e
    @logger.info("HTTP error: #{e.inspect}") unless @logger.debug?
    KineticHttpResponse.new(e)
  end
end

#head(url, params = {}, headers = {}, http_options = @options) ⇒ KineticSdk::Utils::KineticHttpResponse

Send an HTTP HEAD request

Parameters:

  • url (String)

    url to send the request to

  • params (Hash) (defaults to: {})

    Query parameters that are added to the URL, such as +include+

  • headers (Hash) (defaults to: {})

    hash of headers to send

  • http_options (Hash) (defaults to: @options)

    hash of http options

Options Hash (http_options):

  • :max_redirects (Fixnum)

    optional - max number of times to redirect

  • :gateway_retry_limit (Fixnum)

    optional - max number of times to retry a bad gateway

  • :gateway_retry_delay (Float)

    optional - number of seconds to delay before retrying a bad gateway

Returns:



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/kinetic_sdk/utils/kinetic-http.rb', line 183

def head(url, params={}, headers={}, http_options=@options)
  # determine the http options
  redirect_limit = http_options[:max_redirects] || max_redirects
  gateway_retries = http_options[:gateway_retry_limit] || gateway_retry_limit
  gateway_delay = http_options[:gateway_retry_delay] || gateway_retry_delay

  # parse the URL
  uri = URI.parse(url)
  # add URL parameters
  uri.query = URI.encode_www_form(params)

  @logger.debug("HEAD #{uri}  #{headers.inspect}")

  # build the http object
  http = build_http(uri)
  # build the request
  request = Net::HTTP::Head.new(uri.request_uri, headers)

  # send the request
  begin
    response = http.request(request)
    # handle the response
    case response
    when Net::HTTPRedirection then
      if redirect_limit == -1
        @logger.info("HTTP response code: #{response.code}") unless @logger.debug?
        KineticHttpResponse.new(response)
      elsif redirect_limit == 0
        raise Net::HTTPFatalError.new("Too many redirects", response)
      else
        head(response['location'], params, headers, http_options.merge({
          :max_redirects => redirect_limit - 1
        }))
      end
    # handle 502, 503, 504
    when Net::HTTPBadGateway, Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeOut then
      if gateway_retries == -1
        KineticHttpResponse.new(response)
      elsif gateway_retries == 0
        @logger.info("HTTP response: #{response.code} #{response.message}") unless @logger.debug?
        raise Net::HTTPFatalError.new("#{response.code} #{response.message}", response)
      else
        @logger.info("#{response.code} #{response.message}, retrying in #{gateway_delay} seconds")
        sleep(gateway_delay)
        head(url, params, headers, http_options.merge({
          :gateway_retry_limit => gateway_retries - 1
        }))
      end
    when Net::HTTPUnknownResponse, NilClass then
      @logger.info("HTTP response code: 0") unless @logger.debug?
      e = Net::HTTPFatalError.new("Unknown response from server", response)
      KineticHttpResponse.new(e)
    else
      @logger.info("HTTP response code: #{response.code}") unless @logger.debug?
      KineticHttpResponse.new(response)
    end
  rescue Net::HTTPBadResponse => e
    @logger.info("HTTP bad response: #{e.inspect}") unless @logger.debug?
    KineticHttpResponse.new(e)
  rescue StandardError => e
    @logger.info("HTTP error: #{e.inspect}") unless @logger.debug?
    KineticHttpResponse.new(e)
  end
end

#header_accept_jsonHash

Provides an Accept header set to application/json

Returns:

  • (Hash)

    Accept header set to application/json



12
13
14
# File 'lib/kinetic_sdk/utils/kinetic-http-headers.rb', line 12

def header_accept_json
  { "Accept" => "application/json" }
end

#header_basic_auth(username = @username, password = @password) ⇒ Hash

Provides a basic authentication header

Parameters:

  • username (String) (defaults to: @username)

    username to authenticate

  • password (String) (defaults to: @password)

    password associated to the username

Returns:

  • (Hash)

    Authorization: Basic base64 hash of username and password



21
22
23
# File 'lib/kinetic_sdk/utils/kinetic-http-headers.rb', line 21

def header_basic_auth(username=@username, password=@password)
  { "Authorization" => "Basic #{Base64.encode64(username.to_s + ":" + password.to_s).gsub("\n", "")}" }
end

#header_bearer_auth(token = @jwt) ⇒ Hash

Provides a Bearer authentication header

Parameters:

  • token (String) (defaults to: @jwt)

    JSON Web Token (jwt)

Returns:

  • (Hash)

    Authorization: Bearer jwt



29
30
31
# File 'lib/kinetic_sdk/utils/kinetic-http-headers.rb', line 29

def header_bearer_auth(token=@jwt)
  { "Authorization" => "Bearer #{token}" }
end

#header_content_jsonHash

Provides a content-type header set to application/json

Returns:

  • (Hash)

    Content-Type header set to application/json



36
37
38
# File 'lib/kinetic_sdk/utils/kinetic-http-headers.rb', line 36

def header_content_json
  { "Content-Type" => "application/json" }
end

#header_user_agentHash

Provides a user-agent header set to Kinetic Ruby SDK

Returns:

  • (Hash)

    User-Agent header set to Kinetic Reuby SDK



43
44
45
# File 'lib/kinetic_sdk/utils/kinetic-http-headers.rb', line 43

def header_user_agent
  { "User-Agent" => "Kinetic Ruby SDK #{KineticSdk::VERSION}" }
end

#max_redirectsFixnum

The maximum number of times to follow redirects.

Can be passed in as an option when initializing the SDK with either the @options[:max_redirects] or @options['max_redirects'] key.

Expects an integer [Fixnum] value. Setting to 0 will disable redirects.

Returns:

  • (Fixnum)

    default 5



706
707
708
709
710
711
712
713
# File 'lib/kinetic_sdk/utils/kinetic-http.rb', line 706

def max_redirects
  limit = @options &&
  (
    @options[:max_redirects] ||
    @options['max_redirects']
  )
  limit.nil? ? 5 : limit.to_i
end

#mimetype(file) ⇒ Array

Determines the mime-type of a file

Parameters:

  • file (File | String)

    file or filename to detect

Returns:

  • (Array)

    MIME::Type of the file



689
690
691
692
693
694
695
# File 'lib/kinetic_sdk/utils/kinetic-http.rb', line 689

def mimetype(file)
  mime_type = MIME::Types.type_for(file.class == File ? File.basename(file) : file)
  if mime_type.size == 0
    mime_type = MIME::Types['text/plain'] 
  end
  mime_type
end

#patch(url, data = {}, headers = {}, http_options = @options) ⇒ KineticSdk::Utils::KineticHttpResponse

Send an HTTP PATCH request

Parameters:

  • url (String)

    url to send the request to

  • data (Hash) (defaults to: {})

    the payload to send with the request

  • headers (Hash) (defaults to: {})

    hash of headers to send

  • http_options (Hash) (defaults to: @options)

    hash of http options

Options Hash (http_options):

  • :max_redirects (Fixnum)

    optional - max number of times to redirect

  • :gateway_retry_limit (Fixnum)

    optional - max number of times to retry a bad gateway

  • :gateway_retry_delay (Float)

    optional - number of seconds to delay before retrying a bad gateway

Returns:



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/kinetic_sdk/utils/kinetic-http.rb', line 258

def patch(url, data={}, headers={}, http_options=@options)
  # determine the http options
  redirect_limit = http_options[:max_redirects] || max_redirects
  gateway_retries = http_options[:gateway_retry_limit] || gateway_retry_limit
  gateway_delay = http_options[:gateway_retry_delay] || gateway_retry_delay

  # parse the URL
  uri = URI.parse(url)

  @logger.debug("PATCH #{uri}  #{headers.inspect}")

  # unless the data is already a string, assume JSON and convert to string
  data = data.to_json unless data.is_a? String
  # build the http object
  http = build_http(uri)
  # build the request
  request = Net::HTTP::Patch.new(uri.request_uri, headers)
  request.body = data

  # send the request
  begin
    response = http.request(request)
    # handle the response
    case response
    when Net::HTTPRedirection then
      if redirect_limit == -1
        @logger.info("HTTP response code: #{response.code}") unless @logger.debug?
        KineticHttpResponse.new(response)
      elsif redirect_limit == 0
        raise Net::HTTPFatalError.new("Too many redirects", response)
      else
        patch(response['location'], data, headers, http_options.merge({
          :max_redirects => redirect_limit - 1
        }))
      end
    # handle 502, 503, 504
    when Net::HTTPBadGateway, Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeOut then
      if gateway_retries == -1
        KineticHttpResponse.new(response)
      elsif gateway_retries == 0
        @logger.info("HTTP response: #{response.code} #{response.message}") unless @logger.debug?
        raise Net::HTTPFatalError.new("#{response.code} #{response.message}", response)
      else
        @logger.info("#{response.code} #{response.message}, retrying in #{gateway_delay} seconds")
        sleep(gateway_delay)
        patch(url, data, headers, http_options.merge({
          :gateway_retry_limit => gateway_retries - 1
        }))
      end
    when Net::HTTPUnknownResponse, NilClass then
      @logger.info("HTTP response code: 0") unless @logger.debug?
      e = Net::HTTPFatalError.new("Unknown response from server", response)
      KineticHttpResponse.new(e)
    else
      @logger.info("HTTP response code: #{response.code}") unless @logger.debug?
      KineticHttpResponse.new(response)
    end
  rescue Net::HTTPBadResponse => e
    @logger.info("HTTP bad response: #{e.inspect}") unless @logger.debug?
    KineticHttpResponse.new(e)
  rescue StandardError => e
    @logger.info("HTTP error: #{e.inspect}") unless @logger.debug?
    KineticHttpResponse.new(e)
  end
end

#post(url, data = {}, headers = {}, http_options = @options) ⇒ KineticSdk::Utils::KineticHttpResponse

Send an HTTP POST request

Parameters:

  • url (String)

    url to send the request to

  • data (Hash) (defaults to: {})

    the payload to send with the request

  • headers (Hash) (defaults to: {})

    hash of headers to send

  • http_options (Hash) (defaults to: @options)

    hash of http options

Options Hash (http_options):

  • :max_redirects (Fixnum)

    optional - max number of times to redirect

  • :gateway_retry_limit (Fixnum)

    optional - max number of times to retry a bad gateway

  • :gateway_retry_delay (Float)

    optional - number of seconds to delay before retrying a bad gateway

Returns:



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/kinetic_sdk/utils/kinetic-http.rb', line 334

def post(url, data={}, headers={}, http_options=@options)
  # determine the http options
  redirect_limit = http_options[:max_redirects] || max_redirects
  gateway_retries = http_options[:gateway_retry_limit] || gateway_retry_limit
  gateway_delay = http_options[:gateway_retry_delay] || gateway_retry_delay

  # parse the URL
  uri = URI.parse(url)

  @logger.debug("POST #{uri}  #{headers.inspect}")

  # unless the data is already a string, assume JSON and convert to string
  data = data.to_json unless data.is_a? String
  # build the http object
  http = build_http(uri)
  # build the request
  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.body = data

  # send the request
  begin
    response = http.request(request)
    # handle the response
    case response
    when Net::HTTPRedirection then
      if redirect_limit == -1
        @logger.info("HTTP response code: #{response.code}") unless @logger.debug?
        KineticHttpResponse.new(response)
      elsif redirect_limit == 0
        raise Net::HTTPFatalError.new("Too many redirects", response)
      else
        post(response['location'], data, headers, http_options.merge({
          :max_redirects => redirect_limit - 1
        }))
      end
    # handle 502, 503, 504
    when Net::HTTPBadGateway, Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeOut then
      if gateway_retries == -1
        KineticHttpResponse.new(response)
      elsif gateway_retries == 0
        @logger.info("HTTP response: #{response.code} #{response.message}") unless @logger.debug?
        raise Net::HTTPFatalError.new("#{response.code} #{response.message}", response)
      else
        @logger.info("#{response.code} #{response.message}, retrying in #{gateway_delay} seconds")
        sleep(gateway_delay)
        post(url, data, headers, http_options.merge({
          :gateway_retry_limit => gateway_retries - 1
        }))
      end
    when Net::HTTPUnknownResponse, NilClass then
      @logger.info("HTTP response code: 0") unless @logger.debug?
      e = Net::HTTPFatalError.new("Unknown response from server", response)
      KineticHttpResponse.new(e)
    else
      @logger.info("HTTP response code: #{response.code}") unless @logger.debug?
      KineticHttpResponse.new(response)
    end
  rescue Net::HTTPBadResponse => e
    @logger.info("HTTP bad response: #{e.inspect}") unless @logger.debug?
    KineticHttpResponse.new(e)
  rescue StandardError => e
    @logger.info("HTTP error: #{e.inspect}") unless @logger.debug?
    KineticHttpResponse.new(e)
  end
end

#post_multipart(url, data = {}, headers = {}, http_options = @options) ⇒ KineticSdk::Utils::KineticHttpResponse

Send a Multipart HTTP POST request

Parameters:

  • url (String)

    url to send the request to

  • data (Hash) (defaults to: {})

    payload to send with the request

  • headers (Hash) (defaults to: {})

    hash of headers to send

  • http_options (Hash) (defaults to: @options)

    hash of http options

Options Hash (http_options):

  • :max_redirects (Fixnum)

    optional - max number of times to redirect

  • :gateway_retry_limit (Fixnum)

    optional - max number of times to retry a bad gateway

  • :gateway_retry_delay (Float)

    optional - number of seconds to delay before retrying a bad gateway

Returns:



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/kinetic_sdk/utils/kinetic-http.rb', line 410

def post_multipart(url, data={}, headers={}, http_options=@options)
  # determine the http options
  redirect_limit = http_options[:max_redirects] || max_redirects
  gateway_retries = http_options[:gateway_retry_limit] || gateway_retry_limit
  gateway_delay = http_options[:gateway_retry_delay] || gateway_retry_delay

  # the Content-Type header is handled automoatically by Net::HTTP::Post::Multipart
  headers.delete_if { |k,v| k.to_s.downcase == "content-type" }

  @logger.debug("POST #{url}  #{headers.inspect}  multi-part form content")

  # parse the URL
  uri = URI.parse(url)

  # prepare the payload
  payload = data.inject({}) do |h,(k,v)| 
    if v.class == File
      h[k] = UploadIO.new(v, mimetype(v).first, File.basename(v))
    elsif v.class == Array
      h[k] = v.inject([]) do |files, part|
        if part.class == File
          files << UploadIO.new(part, mimetype(part).first, File.basename(part))
        end
      end
    else
      h[k] = v
    end
    h
  end

  # build the http object
  http = build_http(uri)
  # build the request
  request = Net::HTTP::Post::Multipart.new(uri.request_uri, payload)
  headers.each { |k,v| request.add_field(k, v) }
  # send the request
  begin
    response = http.request(request)
    # handle the response
    case response
    when Net::HTTPRedirection then
      if redirect_limit == -1
        @logger.info("HTTP response code: #{response.code}") unless @logger.debug?
        KineticHttpResponse.new(response)
      elsif redirect_limit == 0
        raise Net::HTTPFatalError.new("Too many redirects", response)
      else
        post_multipart(response['location'], data, headers, http_options.merge({
          :max_redirects => redirect_limit - 1
        }))
      end
    # handle 502, 503, 504
    when Net::HTTPBadGateway, Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeOut then
      if gateway_retries == -1
        KineticHttpResponse.new(response)
      elsif gateway_retries == 0
        @logger.info("HTTP response: #{response.code} #{response.message}") unless @logger.debug?
        raise Net::HTTPFatalError.new("#{response.code} #{response.message}", response)
      else
        @logger.info("#{response.code} #{response.message}, retrying in #{gateway_delay} seconds")
        sleep(gateway_delay)
        post_multipart(url, data, headers, http_options.merge({
          :gateway_retry_limit => gateway_retries - 1
        }))
      end
    when Net::HTTPUnknownResponse, NilClass then
      @logger.info("HTTP response code: 0") unless @logger.debug?
      e = Net::HTTPFatalError.new("Unknown response from server", response)
      KineticHttpResponse.new(e)
    else
      @logger.info("HTTP response code: #{response.code}") unless @logger.debug?
      KineticHttpResponse.new(response)
    end
  rescue Net::HTTPBadResponse => e
    @logger.info("HTTP bad response: #{e.inspect}") unless @logger.debug?
    KineticHttpResponse.new(e)
  rescue StandardError => e
    @logger.info("HTTP error: #{e.inspect}") unless @logger.debug?
    KineticHttpResponse.new(e)
  end
end

#put(url, data = {}, headers = {}, http_options = @options) ⇒ KineticSdk::Utils::KineticHttpResponse

Send an HTTP PUT request

Parameters:

  • url (String)

    url to send the request to

  • data (Hash) (defaults to: {})

    payload to send with the request

  • headers (Hash) (defaults to: {})

    hash of headers to send

  • http_options (Hash) (defaults to: @options)

    hash of http options

Options Hash (http_options):

  • :max_redirects (Fixnum)

    optional - max number of times to redirect

  • :gateway_retry_limit (Fixnum)

    optional - max number of times to retry a bad gateway

  • :gateway_retry_delay (Float)

    optional - number of seconds to delay before retrying a bad gateway

Returns:



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/kinetic_sdk/utils/kinetic-http.rb', line 502

def put(url, data={}, headers={}, http_options=@options)
  # determine the http options
  redirect_limit = http_options[:max_redirects] || max_redirects
  gateway_retries = http_options[:gateway_retry_limit] || gateway_retry_limit
  gateway_delay = http_options[:gateway_retry_delay] || gateway_retry_delay

  # parse the URL
  uri = URI.parse(url)

  @logger.debug("PUT #{uri}  #{headers.inspect}")

  # unless the data is already a string, assume JSON and convert to string
  data = data.to_json unless data.is_a? String
  # build the http object
  http = build_http(uri)
  # build the request
  request = Net::HTTP::Put.new(uri.request_uri, headers)
  request.body = data

  # send the request
  begin
    response = http.request(request)
    # handle the response
    case response
    when Net::HTTPRedirection then
      if redirect_limit == -1
        @logger.info("HTTP response code: #{response.code}") unless @logger.debug?
        KineticHttpResponse.new(response)
      elsif redirect_limit == 0
        raise Net::HTTPFatalError.new("Too many redirects", response)
      else
        put(response['location'], data, headers, http_options.merge({
          :max_redirects => redirect_limit - 1
        }))
      end
    # handle 502, 503, 504
    when Net::HTTPBadGateway, Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeOut then
      if gateway_retries == -1
        KineticHttpResponse.new(response)
      elsif gateway_retries == 0
        @logger.info("HTTP response: #{response.code} #{response.message}") unless @logger.debug?
        raise Net::HTTPFatalError.new("#{response.code} #{response.message}", response)
      else
        @logger.info("#{response.code} #{response.message}, retrying in #{gateway_delay} seconds")
        sleep(gateway_delay)
        put(url, data, headers, http_options.merge({
          :gateway_retry_limit => gateway_retries - 1
        }))
      end
    when Net::HTTPUnknownResponse, NilClass then
      @logger.info("HTTP response code: 0") unless @logger.debug?
      e = Net::HTTPFatalError.new("Unknown response from server", response)
      KineticHttpResponse.new(e)
    else
      @logger.info("HTTP response code: #{response.code}") unless @logger.debug?
      KineticHttpResponse.new(response)
    end
  rescue Net::HTTPBadResponse => e
    @logger.info("HTTP bad response: #{e.inspect}") unless @logger.debug?
    KineticHttpResponse.new(e)
  rescue StandardError => e
    @logger.info("HTTP error: #{e.inspect}") unless @logger.debug?
    KineticHttpResponse.new(e)
  end
end

#redirect_url(url, params = {}, headers = {}, http_options = @options) ⇒ String

Determine the final redirect location

Parameters:

  • url (String)

    url to send the request to

  • params (Hash) (defaults to: {})

    Query parameters that are added to the URL, such as +include+

  • headers (Hash) (defaults to: {})

    hash of headers to send

  • http_options (Hash) (defaults to: @options)

    hash of http options

Options Hash (http_options):

  • :max_redirects (Fixnum)

    optional - max number of times to redirect

  • :gateway_retry_limit (Fixnum)

    optional - max number of times to retry a bad gateway

  • :gateway_retry_delay (Float)

    optional - number of seconds to delay before retrying a bad gateway

Returns:

  • (String)

    redirection url, or url if there is no redirection



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'lib/kinetic_sdk/utils/kinetic-http.rb', line 578

def redirect_url(url, params={}, headers={}, http_options=@options)
  # determine the http options
  redirect_limit = http_options[:max_redirects] || max_redirects
  gateway_retries = http_options[:gateway_retry_limit] || gateway_retry_limit
  gateway_delay = http_options[:gateway_retry_delay] || gateway_retry_delay

  # parse the URL
  uri = URI.parse(url)
  # add URL parameters
  uri.query = URI.encode_www_form(params)

  # build the http object
  http = build_http(uri)
  # build the request
  request = Net::HTTP::Head.new(uri.request_uri, headers)

  # send the request
  response = http.request(request)
  # handle the response
  case response
  when Net::HTTPRedirection then
    if redirect_limit > 0
      url = response['location']
      head(response['location'], params, headers, http_options.merge({
        :max_redirects => redirect_limit - 1
      }))
    end
  # handle 502, 503, 504
  when Net::HTTPBadGateway, Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeOut then
    if gateway_retries == -1
      KineticHttpResponse.new(response)
    elsif gateway_retries == 0
        @logger.info("HTTP response: #{response.code} #{response.message}") unless @logger.debug?
      raise Net::HTTPFatalError.new("#{response.code} #{response.message}", response)
    else
      @logger.info("#{response.code} #{response.message}, retrying in #{gateway_delay} seconds")
      sleep(gateway_delay)
      redirect_url(url, params, headers, http_options.merge({
        :gateway_retry_limit => gateway_retries - 1
      }))
    end
  end
  url
end

#stream_download_to_file(file_path, url, params = {}, headers = {}, http_options = @options) ⇒ Object

Download attachment from a URL and save to file.

Streams the download to limit memory consumption. The user account utilizing the SDK must have write access to the file path.

Parameters:

  • url (String)

    url to send the request to

  • params (Hash) (defaults to: {})

    Query parameters that are added to the URL, such as +include+

  • headers (Hash) (defaults to: {})

    hash of headers to send

  • http_options (Hash) (defaults to: @options)

    hash of http options

Options Hash (http_options):

  • :max_redirects (Fixnum)

    optional - max number of times to redirect

  • :gateway_retry_limit (Fixnum)

    optional - max number of times to retry a bad gateway

  • :gateway_retry_delay (Float)

    optional - number of seconds to delay before retrying a bad gateway



635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/kinetic_sdk/utils/kinetic-http.rb', line 635

def stream_download_to_file(file_path, url, params={}, headers={}, http_options=@options)
  # Determine if redirection is involved
  url = redirect_url(url, params, headers, http_options)
  # parse the URL
  uri = URI.parse(url)
  
  @logger.debug("Streaming Download #{uri}  #{headers.inspect}")
  
  # build the http object
  http = build_http(uri)
  
  # prepare the download
  file = nil
  file_name = File.basename(file_path)
  response_code = nil
  message = nil
  begin
    # stream the attachment
    http.request_get(uri.request_uri, headers) do |response|
      response_code = response.code
      if response_code == "200"
        file = File.open(file_path, "wb")
        response.read_body { |chunk| file.write(chunk) }
      else
        message = response.body
        break
      end
    end
    if response_code == "200"
      @logger.info("Exported file attachment: #{file_name} to #{file_path}")
    else
      @logger.error("Failed to export file attachment \"#{file_name}\": #{message}")
    end
  rescue StandardError => e
    @logger.error("Failed to export file attachment \"#{file_name}\": (#{e})")
  ensure
    file.close() unless file.nil?
  end
  message
end