Class: OpsManager::Api::Base
- Inherits:
-
Object
- Object
- OpsManager::Api::Base
- Includes:
- Logging
- Defined in:
- lib/ops_manager/api/base.rb
Instance Attribute Summary collapse
-
#silent ⇒ Object
readonly
Returns the value of attribute silent.
Instance Method Summary collapse
- #add_authentication(opts = {}) ⇒ Object
- #delete(endpoint, opts = {}) ⇒ Object
- #get(endpoint, opts = {}) ⇒ Object
- #http_for(uri) ⇒ Object
-
#initialize(opts = {}) ⇒ Base
constructor
A new instance of Base.
- #multipart_post(endpoint, opts = {}) ⇒ Object
- #post(endpoint, opts = {}) ⇒ Object
- #print_green(str) ⇒ Object
- #put(endpoint, opts = {}) ⇒ Object
- #say_green(str) ⇒ Object
- #uri_for(endpoint) ⇒ Object
Methods included from Logging
Constructor Details
#initialize(opts = {}) ⇒ Base
Returns a new instance of Base.
8 9 10 |
# File 'lib/ops_manager/api/base.rb', line 8 def initialize(opts = {}) @silent = opts[:silent] end |
Instance Attribute Details
#silent ⇒ Object (readonly)
Returns the value of attribute silent.
4 5 6 |
# File 'lib/ops_manager/api/base.rb', line 4 def silent @silent end |
Instance Method Details
#add_authentication(opts = {}) ⇒ Object
163 164 165 166 167 |
# File 'lib/ops_manager/api/base.rb', line 163 def add_authentication(opts={}) opts[:headers] ||= {} opts[:headers]['Authorization'] ||= opts end |
#delete(endpoint, opts = {}) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ops_manager/api/base.rb', line 126 def delete(endpoint, opts = {}) uri = uri_for(endpoint) http = http_for(uri) request = Net::HTTP::Delete.new(uri.request_uri) request.basic_auth(username, password) if self.respond_to?(:username) if opts.has_key?(:headers) opts.delete(:headers).each_pair do |k,v| request[k] = v end end http.request(request).tap do |res| logger.info("performing delete to #{uri} with opts: #{opts.inspect} res.code: #{res.code}") logger.info("post response body #{res.body}") end end |
#get(endpoint, opts = {}) ⇒ Object
18 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 45 46 47 48 49 50 51 52 |
# File 'lib/ops_manager/api/base.rb', line 18 def get(endpoint, opts = {}) uri = uri_for(endpoint) http = http_for(uri) request = Net::HTTP::Get.new(uri.request_uri) if opts.has_key?(:basic_auth) request.basic_auth( opts[:basic_auth][:username], opts[:basic_auth][:password]) end if opts.has_key?(:headers) opts.delete(:headers).each_pair do |k,v| request[k] = v end end if opts[:write_to] begin f = open(opts.fetch(:write_to), "wb") http.request(request) do |res| res.read_body do |segment| f.write(segment) end logger.info("performing get to #{uri} with opts: #{opts.inspect} res.code: #{res.code}") logger.info("get response body #{res.body}") end ensure f.close end else http.request(request).tap do |res| logger.info("performing get to #{uri} with opts: #{opts.inspect} res.code: #{res.code}") logger.info("get response body #{res.body}") end end end |
#http_for(uri) ⇒ Object
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/ops_manager/api/base.rb', line 152 def http_for(uri) Net::HTTP.new(uri.host, uri.port).tap do |http| http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.read_timeout = 1800 ctx = OpenSSL::SSL::SSLContext.new ctx.ssl_version = :TLSv1_2 end end |
#multipart_post(endpoint, opts = {}) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/ops_manager/api/base.rb', line 106 def multipart_post(endpoint, opts = {}) uri = uri_for(endpoint) http = http_for(uri) request = Net::HTTP::Post::Multipart.new(uri.request_uri, opts) request.basic_auth(username, password) if self.respond_to?(:username) if opts.has_key?(:headers) opts.delete(:headers).each_pair do |k,v| request[k] = v end end http.request(request).tap do |res| logger.info("performing multipart_post to #{uri} with opts: #{opts.inspect} res.code: #{res.code}") logger.info("post response body #{res.body}") end end |
#post(endpoint, opts = {}) ⇒ Object
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 |
# File 'lib/ops_manager/api/base.rb', line 54 def post(endpoint, opts= {}) uri = uri_for(endpoint) http = http_for(uri) request = Net::HTTP::Post.new(uri.request_uri) if opts.has_key?(:headers) opts.delete(:headers).each_pair do |k,v| request[k] = v end end body = opts[:body] || '' request.body= body res = http.request(request).tap do |res| logger.info("performing post to #{uri} with opts: #{opts.inspect} res.code: #{res.code}") logger.info("post response body #{res.body}") end if res.code == '302' get(res[ 'Location' ], opts) else res end end |
#print_green(str) ⇒ Object
173 174 175 |
# File 'lib/ops_manager/api/base.rb', line 173 def print_green(str) print str.green unless silent end |
#put(endpoint, opts = {}) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ops_manager/api/base.rb', line 80 def put(endpoint, opts ={}) uri = uri_for(endpoint) http = http_for(uri) request = Net::HTTP::Put.new(uri.request_uri) request.set_form_data(opts) request.basic_auth(username, password) if self.respond_to?(:username) if opts.has_key?(:headers) opts.delete(:headers).each_pair do |k,v| request[k] = v end end request['Content-Type'] = 'application/json' body = opts[:body] || '' request.body= body http.request(request).tap do |res| logger.info("performing put to #{uri} with opts: #{opts.inspect} res.code: #{res.code}") logger.info("put response body #{res.body}") end end |
#say_green(str) ⇒ Object
169 170 171 |
# File 'lib/ops_manager/api/base.rb', line 169 def say_green(str) puts str.green unless silent end |
#uri_for(endpoint) ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/ops_manager/api/base.rb', line 144 def uri_for(endpoint) if endpoint =~/^http/ URI.parse(endpoint) else URI.parse("https://#{target}#{endpoint}") end end |