Class: Skytap::Requester
- Inherits:
-
Object
- Object
- Skytap::Requester
- Defined in:
- lib/skytap/requester.rb
Defined Under Namespace
Classes: Response
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #auth_header ⇒ Object
- #base_headers ⇒ Object
-
#initialize(logger, username, api_token, base_url, http_format, verify_certs = true) ⇒ Requester
constructor
A new instance of Requester.
-
#request(method, url, options = {}) ⇒ Object
Raises on error code unless :raise => false is passed.
- #with_session {|http| ... } ⇒ Object
Constructor Details
#initialize(logger, username, api_token, base_url, http_format, verify_certs = true) ⇒ Requester
Returns a new instance of Requester.
41 42 43 44 45 46 47 48 |
# File 'lib/skytap/requester.rb', line 41 def initialize(logger, username, api_token, base_url, http_format, verify_certs=true) @logger = logger @username = username or raise Skytap::Error.new 'No username provided' @api_token = api_token or raise Skytap::Error.new 'No API token provided' @base_uri = URI.parse(base_url) or raise Skytap::Error.new 'No base URL provided' @format = "application/#{http_format}" or raise Skytap::Error.new 'No HTTP format provided' @verify_certs = verify_certs end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
39 40 41 |
# File 'lib/skytap/requester.rb', line 39 def format @format end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
39 40 41 |
# File 'lib/skytap/requester.rb', line 39 def logger @logger end |
Instance Method Details
#auth_header ⇒ Object
112 113 114 |
# File 'lib/skytap/requester.rb', line 112 def auth_header "Basic #{Base64.encode64(@username + ":" + @api_token)}".gsub("\n", '') end |
#base_headers ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/skytap/requester.rb', line 103 def base_headers headers = { 'Authorization' => auth_header, 'Content-Type' => format, 'Accept' => format, 'User-Agent' => "SkytapCLI/#{Skytap::VERSION}", } end |
#request(method, url, options = {}) ⇒ Object
Raises on error code unless :raise => false is passed.
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 96 97 98 99 100 101 |
# File 'lib/skytap/requester.rb', line 51 def request(method, url, = {}) = .symbolize_keys [:raise] = true unless .has_key?(:raise) with_session do |http| begin #TODO:NLA Move this into method if p = [:params] if p.is_a?(Hash) p = p.collect {|k,v| CGI.escape(k) + '=' + CGI.escape(v)}.join('&') end path, params = url.split('?', 2) if params params << '&' else params = '' end params << p url = [path, params].join('?') end body = [:body] || '' headers = base_headers.merge([:headers] || {}) logger.debug 'Request:'.color(:cyan).bright logger.debug "#{method} #{url}\n#{headers.collect {|k,v| "#{k}:#{v}"}.join("\n")}\n\n#{body}".color(:cyan) logger.debug '---'.color(:cyan).bright response = Response.new(logger, http.send_request(method, url, body, headers), :raise => [:raise]) rescue OpenSSL::SSL::SSLError => ex $stderr.puts <<-"EOF" An SSL error occurred (probably certificate verification failed). If you are pointed against an internal test environment, set "verify-certs: false" in your ~/.skytaprc file or use --no-verify-certs. If not, then you may be subject to a man-in-the-middle attack, or the web site's SSL certificate may have expired. The error was: #{ex} EOF exit(-1) rescue Net::HTTPServerException => ex logger.debug 'Response:'.color(:cyan).bright logger.info "Code: #{ex.response.code} #{ex.response.}".color(:red).bright logger.info ex.response.body raise end end end |
#with_session {|http| ... } ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/skytap/requester.rb', line 116 def with_session http = Net::HTTP.new(@base_uri.host, @base_uri.port) http.use_ssl = @base_uri.port == 443 || @base_uri.scheme == 'https' if http.use_ssl? # Allow cert-checking to be disabled, since internal test environments # have bad certs. if @verify_certs http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.ca_file = File.join(File.dirname(__FILE__), '..', '..', 'ca-bundle.crt') else http.verify_mode = OpenSSL::SSL::VERIFY_NONE end end yield http end |