Module: Misty::HTTP::Request

Included in:
Openstack::ServicePack
Defined in:
lib/misty/http/request.rb

Constant Summary collapse

DECODE_TO_JSON =
["application/json", "application/octet-stream"].freeze

Instance Method Summary collapse

Instance Method Details

#decode?(response) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
# File 'lib/misty/http/request.rb', line 7

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

#decode_to_json?(content_type) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
# File 'lib/misty/http/request.rb', line 18

def decode_to_json?(content_type)
  DECODE_TO_JSON.each do |type|
    return true if content_type.include?(type)
  end
  false
end

#http(request) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/misty/http/request.rb', line 25

def http(request)
  request['X-Auth-Token'] = @token.get

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

#http_copy(path, headers) ⇒ Object



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

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

#http_delete(path, headers) ⇒ Object



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

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

#http_get(path, headers) ⇒ Object



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

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

#http_head(path, headers) ⇒ Object



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

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

#http_options(path, headers) ⇒ Object



61
62
63
64
65
# File 'lib/misty/http/request.rb', line 61

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

#http_patch(path, headers, data) ⇒ Object



67
68
69
70
71
72
# File 'lib/misty/http/request.rb', line 67

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

#http_post(path, headers, data) ⇒ Object



74
75
76
77
78
79
# File 'lib/misty/http/request.rb', line 74

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

#http_put(path, headers, data) ⇒ Object



81
82
83
84
85
86
# File 'lib/misty/http/request.rb', line 81

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

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



88
89
90
91
92
# File 'lib/misty/http/request.rb', line 88

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