module Sentofu::Http
Constants
- PROXY_REX
Public Class Methods
fetch_token(credentials=nil)
click to toggle source
# File lib/sentofu/http.rb, line 10 def fetch_token(credentials=nil) cs = narrow_credentials(credentials) a = Base64.encode64("#{cs.id}:#{cs.secret}").strip u = URI(Sentofu.auth_uri) req = Net::HTTP::Post.new(u.path) req.add_field('Content-Type', 'application/json') req.add_field('Authorization', a) req.body = JSON.dump( grant_type: 'password', username: cs.user, password: cs.pass) Sentofu::Token.new(request(u, req)) end
get(uri, token=nil)
click to toggle source
# File lib/sentofu/http.rb, line 28 def get(uri, token=nil) u = URI(uri) request(u, make_get_req(u, token)) end
get_and_parse(uri, token=nil)
click to toggle source
# File lib/sentofu/http.rb, line 89 def get_and_parse(uri, token=nil) res = get(uri, token) fail( "#{WEBrick::HTTPStatus.reason_phrase(res.code) rescue ''} - " + "#{res._uri.to_s}" ) unless res.header['content-type'].match?(/^application\/json(;|$)/) JSON.parse(res.body) .merge!( _uri: res._uri.to_s, _headers: res._headers, _code: res.code.to_i, _elapsed: res._elapsed, _proxy: res._proxy) rescue => err { uri: uri.to_s, code: (res.code.to_i rescue nil), headers: (res._headers rescue nil), message: (WEBrick::HTTPStatus.reason_phrase(res.code) rescue nil), error_class: err.class.to_s, error_message: err.message, error_trace: err.backtrace, body: ((res.body.index('</html>') ? '<html>' : res.body) rescue nil) } end
make_net_http(uri)
click to toggle source
# File lib/sentofu/http.rb, line 35 def make_net_http(uri) http = if pm = PROXY_REX.match(ENV['sentofu_http_proxy'] || '') port = pm[8] ? pm[8].to_i : nil port ||= 443 if pm[1] && pm[1] == 'https://' #p [ pm[6], port, pm[3], pm[5] ] Net::HTTP.new( uri.host, uri.port, pm[6], port, # proxy host and port pm[3], pm[5]) # proxy user and pass else Net::HTTP.new( uri.host, uri.port) end # Nota Bene: # even if ENV['sentofu_http_proxy'], ENV['http_proxy'] could kick in if uri.scheme == 'https' http.use_ssl = true http.verify_mode = Sentofu.ssl_verify_mode end http end
request(uri, req)
click to toggle source
# File lib/sentofu/http.rb, line 66 def request(uri, req) t0 = monow http = make_net_http(uri) #http.set_debug_output($stderr) res = http.request(req) class << res attr_accessor :_uri, :_elapsed, :_proxy def _headers; each_header.inject({}) { |h, (k, v)| h[k] = v; h }; end end res._uri = uri.to_s res._elapsed = monow - t0 res._proxy = detail_proxy(http) fail "request returned a #{res.class} and not a Net::HTTPResponse" \ unless res.is_a?(Net::HTTPResponse) res end
Protected Class Methods
detail_proxy(http)
click to toggle source
# File lib/sentofu/http.rb, line 155 def detail_proxy(http) if http.proxy_address { address: http.proxy_address, port: http.proxy_port, user: http.proxy_user } else nil end end
load_credentials(fname='.sentifi-credentials.yaml')
click to toggle source
# File lib/sentofu/http.rb, line 150 def load_credentials(fname='.sentifi-credentials.yaml') OpenStruct.new(YAML.load(File.read(fname))) end
make_get_req(uri, token)
click to toggle source
# File lib/sentofu/http.rb, line 122 def make_get_req(uri, token) u = [ uri.path, uri.query ].compact.join('?') u = '/' if u.length < 1 req = Net::HTTP::Get.new(u) req.instance_eval { @header.clear } def req.set_header(k, v); @header[k] = [ v ]; end req.set_header('User-Agent', Sentofu.user_agent) req.set_header('Accept', 'application/json') req.set_header('Authorization', token.header_value) if token req end
monow()
click to toggle source
# File lib/sentofu/http.rb, line 120 def monow; Process.clock_gettime(Process::CLOCK_MONOTONIC); end
narrow_credentials(o)
click to toggle source
# File lib/sentofu/http.rb, line 139 def narrow_credentials(o) case o when OpenStruct then o when Hash then OpenStruct.new(o) when NilClass then load_credentials when String then load_credentials(o) else fail ArgumentError.new( "no credentials in a #{o.class}") end end