class Hpe3parSdk::HTTPJSONRestClient
Constants
- CONTENT_TYPE
- SESSION_COOKIE_NAME
- USER_AGENT
Attributes
api_url[RW]
http_log_debug[RW]
log_level[RW]
logger[RW]
secure[RW]
session_key[RW]
suppress_ssl_warnings[RW]
timeout[RW]
Public Class Methods
new(api_url, secure = false, http_log_debug = false, suppress_ssl_warnings = false, timeout = nil)
click to toggle source
# File lib/Hpe3parSdk/http.rb, line 28 def initialize(api_url, secure = false, http_log_debug = false, suppress_ssl_warnings = false, timeout = nil) @api_url = api_url @secure = secure @http_log_debug = http_log_debug @suppress_ssl_warnings = suppress_ssl_warnings @timeout = timeout @session_key = nil HTTParty::Logger.add_formatter('custom', CustomHTTPFormatter) @httparty_log_level = :info @httparty_log_format = :custom set_debug_flag end
Public Instance Methods
authenticate(user, password, _optional = nil)
click to toggle source
# File lib/Hpe3parSdk/http.rb, line 50 def authenticate(user, password, _optional = nil) begin @session_key = nil info = {:user => user, :password => password} auth_url = '/credentials' headers, body = post(auth_url, body: info) @session_key = body['key'] rescue => ex Util.log_exception(ex, caller_locations(1, 1)[0].label) end end
delete(url, **kwargs)
click to toggle source
# File lib/Hpe3parSdk/http.rb, line 101 def delete(url, **kwargs) headers, payload = get_headers_and_payload(kwargs) response = HTTParty.delete(api_url + url, headers: headers, verify: secure, logger: Hpe3parSdk.logger, log_level: @httparty_log_level, log_format: @httparty_log_format) process_response(response) end
get(url, **kwargs)
click to toggle source
# File lib/Hpe3parSdk/http.rb, line 69 def get(url, **kwargs) headers, payload = get_headers_and_payload(kwargs) response = HTTParty.get(api_url + url, headers: headers, verify: secure, logger: Hpe3parSdk.logger, log_level: @httparty_log_level, log_format: @httparty_log_format) process_response(response) end
get_headers_and_payload(**kwargs)
click to toggle source
# File lib/Hpe3parSdk/http.rb, line 144 def get_headers_and_payload(**kwargs) if session_key kwargs['headers'] = kwargs.fetch('headers', {}) kwargs['headers'][SESSION_COOKIE_NAME] = session_key end kwargs['headers'] = kwargs.fetch('headers', {}) kwargs['headers']['User-Agent'] = USER_AGENT kwargs['headers']['Accept'] = CONTENT_TYPE if kwargs.key?(:body) kwargs['headers']['Content-Type'] = CONTENT_TYPE kwargs[:body] = kwargs[:body].to_json payload = kwargs[:body] else payload = nil end [kwargs['headers'], payload] end
log_exception(exception, caller_location)
click to toggle source
# File lib/Hpe3parSdk/http.rb, line 124 def log_exception(exception, caller_location) formatted_stack_trace = exception.backtrace .map { |line| "\t\tfrom #{line}" } .join($/) err_msg = "(#{caller_location}) #{exception}#{$/} #{formatted_stack_trace}" Hpe3parSdk.logger.error(err_msg) end
post(url, **kwargs)
click to toggle source
# File lib/Hpe3parSdk/http.rb, line 79 def post(url, **kwargs) headers, payload = get_headers_and_payload(kwargs) response = HTTParty.post(api_url + url, headers: headers, body: payload, verify: secure, logger: Hpe3parSdk.logger, log_level: @httparty_log_level, log_format: @httparty_log_format) process_response(response) end
process_response(response)
click to toggle source
# File lib/Hpe3parSdk/http.rb, line 111 def process_response(response) headers = response.headers body = response.parsed_response if response.code != 200 if !body.nil? && body.key?('code') && body.key?('desc') exception = Hpe3parSdk.exception_from_response(response, body) raise exception end end [headers, body] end
put(url, **kwargs)
click to toggle source
# File lib/Hpe3parSdk/http.rb, line 90 def put(url, **kwargs) headers, payload = get_headers_and_payload(kwargs) response = HTTParty.put(api_url + url, headers: headers, body: payload, verify: secure, logger: Hpe3parSdk.logger, log_level: @httparty_log_level, log_format: @httparty_log_format) process_response(response) end
set_debug_flag()
click to toggle source
This turns on/off http request/response debugging output to console
# File lib/Hpe3parSdk/http.rb, line 43 def set_debug_flag if @http_log_debug @httparty_log_level = :debug @httparty_log_format = :curl end end
set_url(api_url)
click to toggle source
# File lib/Hpe3parSdk/http.rb, line 64 def set_url(api_url) # should be http://<Server:Port>/api/v1 @api_url = api_url.chomp('/') end
unauthenticate()
click to toggle source
# File lib/Hpe3parSdk/http.rb, line 132 def unauthenticate # delete the session on the 3Par unless @session_key.nil? begin delete('/credentials/%s' % session_key) @session_key = nil rescue => ex Util.log_exception(ex, caller_locations(1, 1)[0].label) end end end