Top Level Namespace

Defined Under Namespace

Modules: Orch Classes: String

Constant Summary collapse

HTTP_ERRORS =
[
  EOFError,
  Errno::ECONNRESET,
  Errno::EINVAL,
  Net::HTTPBadResponse,
  Net::HTTPHeaderSyntaxError,
  Net::ProtocolError,
  Timeout::Error,
  Errno::ECONNREFUSED,
  Errno::ETIMEDOUT
]
JSON_HEADERS =
{"Content-Type" => "application/json",
"Accept" => "application/json"}
CURRENT_SPEC_VERSION =
1.0

Instance Method Summary collapse

Instance Method Details

#exit_with_msg(err_msg) ⇒ Object



130
131
132
133
# File 'lib/util.rb', line 130

def exit_with_msg(err_msg)
  STDERR.puts err_msg
  exit 1
end

#http_delete(url_list, path, headers) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/util.rb', line 99

def http_delete(url_list, path, headers)
  lastErr = nil

  url_list.split(';').each do |url|
    uri = URI(url)

    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Delete.new(path, headers)
    if uri.user
      request.basic_auth URI.unescape(uri.user), URI.unescape(uri.password)
    end
    begin
      # response = http.delete(path, headers)
      response = http.start {|http| http.request(request) }
    rescue *HTTP_ERRORS => error
      STDERR.puts "Failed http_delete to #{url}: #{error} (#{error.class})"
      lastErr = error
      next
    end

    return response    
  end

  http_fault(lastErr)
end

#http_fault(error) ⇒ Object



125
126
127
128
# File 'lib/util.rb', line 125

def http_fault(error)
  STDERR.puts "Fatal networking error talking to framework: #{error.to_s}"
  exit 1
end

#http_get(url_list, path, headers) ⇒ Object



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
# File 'lib/util.rb', line 73

def http_get(url_list, path, headers)
  lastErr = nil

  url_list.split(';').each do |url|
    uri = URI(url)

    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Get.new(path, headers)
    if uri.user
      request.basic_auth URI.unescape(uri.user), URI.unescape(uri.password)
    end
    begin
      #response = http.get(path, headers)
      response = http.start {|http| http.request(request) }
    rescue *HTTP_ERRORS => error
      STDERR.puts "Failed http_get to #{url}: #{error} (#{error.class})"
      lastErr = error
      next
    end

    return response    
  end

  http_fault(lastErr)
end

#http_post(url_list, path, body, headers) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/util.rb', line 19

def http_post(url_list, path, body, headers)
  lastErr = nil

  url_list.split(';').each do |url|
    uri = URI(url)

    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Post.new(path, headers)
    request.body = body
    if uri.user
      request.basic_auth URI.unescape(uri.user), URI.unescape(uri.password)
    end
    begin
      #response = http.post(path, body, headers)
      response = http.start {|http| http.request(request) }
    rescue *HTTP_ERRORS => error
      STDERR.puts "Failed http_post to #{url}: #{error} (#{error.class})"
      lastErr = error
      next
    end

    return response    
  end

  http_fault(lastErr)
end

#http_put(url_list, path, body, headers) ⇒ Object



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
# File 'lib/util.rb', line 46

def http_put(url_list, path, body, headers)
  lastErr = nil

  url_list.split(';').each do |url|
    uri = URI(url)

    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Put.new(path, headers)
    request.body = body
    if uri.user
      request.basic_auth URI.unescape(uri.user), URI.unescape(uri.password)
    end
    begin
      #response = http.put(path, body, headers)
      response = http.start {|http| http.request(request) }
    rescue *HTTP_ERRORS => error
      STDERR.puts "Failed http_put to #{url}: #{error} (#{error.class})"
      lastErr = error
      next
    end

    return response    
  end

  http_fault(lastErr)
end