Module: Misty::HTTP::Request

Included in:
ClientPack
Defined in:
lib/misty/http/request.rb

Instance Method Summary collapse

Instance Method Details

#decode?(response) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
12
# File 'lib/misty/http/request.rb', line 4

def decode?(response)
  return false if response.body.nil? || response.body.empty?
  if @config.content_type != :json && response.code =~ /2??/ && !response.is_a?(Net::HTTPNoContent) \
    && !response.is_a?(Net::HTTPResetContent) && response.header['content-type'] \
    && response.header['content-type'].include?('application/json')
    return true
  end
  false
end

#http(request) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/misty/http/request.rb', line 14

def http(request)
  request['X-Auth-Token'] = @auth.get_token

  Misty::HTTP::NetHTTP.http_request(
    @uri, ssl_verify_mode: @options.ssl_verify_mode, log: @config.log
  ) do |connection|
    response = connection.request(request)
    response.body = JSON.parse(response.body) if decode?(response)
    response
  end
end

#http_copy(path, headers) ⇒ Object



32
33
34
35
36
# File 'lib/misty/http/request.rb', line 32

def http_copy(path, headers)
  @config.log.info(http_to_s(path, headers))
  request = Net::HTTP::Copy.new(path, headers)
  http(request)
end

#http_delete(path, headers) ⇒ Object



26
27
28
29
30
# File 'lib/misty/http/request.rb', line 26

def http_delete(path, headers)
  @config.log.info(http_to_s(path, headers))
  request = Net::HTTP::Delete.new(path, headers)
  http(request)
end

#http_get(path, headers) ⇒ Object



38
39
40
41
42
# File 'lib/misty/http/request.rb', line 38

def http_get(path, headers)
  @config.log.info(http_to_s(path, headers))
  request = Net::HTTP::Get.new(path, headers)
  http(request)
end

#http_head(path, headers) ⇒ Object



44
45
46
47
48
# File 'lib/misty/http/request.rb', line 44

def http_head(path, headers)
  @config.log.info(http_to_s(path, headers))
  request = Net::HTTP::Head.new(path, headers)
  http(request)
end

#http_options(path, headers) ⇒ Object



50
51
52
53
54
# File 'lib/misty/http/request.rb', line 50

def http_options(path, headers)
  @config.log.info(http_to_s(path, headers))
  request = Net::HTTP::Options.new(path, headers)
  http(request)
end

#http_patch(path, headers, data) ⇒ Object



56
57
58
59
60
61
# File 'lib/misty/http/request.rb', line 56

def http_patch(path, headers, data)
  @config.log.info(http_to_s(path, headers, data))
  request = Net::HTTP::Patch.new(path, headers)
  request.body = data
  http(request)
end

#http_post(path, headers, data) ⇒ Object



63
64
65
66
67
68
# File 'lib/misty/http/request.rb', line 63

def http_post(path, headers, data)
  @config.log.info(http_to_s(path, headers, data))
  request = Net::HTTP::Post.new(path, headers)
  request.body = data
  http(request)
end

#http_put(path, headers, data) ⇒ Object



70
71
72
73
74
75
# File 'lib/misty/http/request.rb', line 70

def http_put(path, headers, data)
  @config.log.info(http_to_s(path, headers, data))
  request = Net::HTTP::Put.new(path, headers)
  request.body = data
  http(request)
end

#http_to_s(path, headers, data = nil) ⇒ Object



77
78
79
80
# File 'lib/misty/http/request.rb', line 77

def http_to_s(path, headers, data = nil)
  log = "base_url='#{@uri.host}:#{@uri.port}', path='#{path}', header=#{headers}"
  log << ", data='#{data}'" if data
end