Class: Radfish::Core::BaseClient
- Inherits:
-
Object
- Object
- Radfish::Core::BaseClient
- Includes:
- Debuggable
- Defined in:
- lib/radfish/core/base_client.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#host_header ⇒ Object
readonly
Returns the value of attribute host_header.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#retry_count ⇒ Object
Returns the value of attribute retry_count.
-
#retry_delay ⇒ Object
Returns the value of attribute retry_delay.
-
#use_ssl ⇒ Object
readonly
Returns the value of attribute use_ssl.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
-
#verbosity ⇒ Object
Returns the value of attribute verbosity.
-
#verify_ssl ⇒ Object
readonly
Returns the value of attribute verify_ssl.
Instance Method Summary collapse
- #authenticated_request(method, path, **options) ⇒ Object
- #base_url ⇒ Object
- #http_delete(path, **options) ⇒ Object
-
#http_get(path, **options) ⇒ Object
Delegate HTTP methods to the client.
- #http_patch(path, **options) ⇒ Object
- #http_post(path, **options) ⇒ Object
- #http_put(path, **options) ⇒ Object
-
#initialize(host:, username:, password:, port: 443, use_ssl: true, verify_ssl: false, retry_count: 3, retry_delay: 1, host_header: nil, **options) ⇒ BaseClient
constructor
A new instance of BaseClient.
- #login ⇒ Object
- #logout ⇒ Object
- #redfish_version ⇒ Object
- #service_root ⇒ Object
-
#vendor ⇒ Object
Vendor-specific methods to be overridden.
- #with_retries(max_retries = nil, initial_delay = nil, error_classes = nil) ⇒ Object
Methods included from Debuggable
Constructor Details
#initialize(host:, username:, password:, port: 443, use_ssl: true, verify_ssl: false, retry_count: 3, retry_delay: 1, host_header: nil, **options) ⇒ BaseClient
Returns a new instance of BaseClient.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/radfish/core/base_client.rb', line 13 def initialize(host:, username:, password:, port: 443, use_ssl: true, verify_ssl: false, retry_count: 3, retry_delay: 1, host_header: nil, **) @host = host @username = username @password = password @port = port @use_ssl = use_ssl @verify_ssl = verify_ssl @host_header = host_header @verbosity = 0 @retry_count = retry_count @retry_delay = retry_delay # Store any vendor-specific options @options = # Create the HTTP client @http_client = HttpClient.new( host: host, port: port, use_ssl: use_ssl, verify_ssl: verify_ssl, username: username, password: password, verbosity: verbosity, retry_count: retry_count, retry_delay: retry_delay ) end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
10 11 12 |
# File 'lib/radfish/core/base_client.rb', line 10 def host @host end |
#host_header ⇒ Object (readonly)
Returns the value of attribute host_header.
10 11 12 |
# File 'lib/radfish/core/base_client.rb', line 10 def host_header @host_header end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
10 11 12 |
# File 'lib/radfish/core/base_client.rb', line 10 def password @password end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
10 11 12 |
# File 'lib/radfish/core/base_client.rb', line 10 def port @port end |
#retry_count ⇒ Object
Returns the value of attribute retry_count.
11 12 13 |
# File 'lib/radfish/core/base_client.rb', line 11 def retry_count @retry_count end |
#retry_delay ⇒ Object
Returns the value of attribute retry_delay.
11 12 13 |
# File 'lib/radfish/core/base_client.rb', line 11 def retry_delay @retry_delay end |
#use_ssl ⇒ Object (readonly)
Returns the value of attribute use_ssl.
10 11 12 |
# File 'lib/radfish/core/base_client.rb', line 10 def use_ssl @use_ssl end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
10 11 12 |
# File 'lib/radfish/core/base_client.rb', line 10 def username @username end |
#verbosity ⇒ Object
Returns the value of attribute verbosity.
11 12 13 |
# File 'lib/radfish/core/base_client.rb', line 11 def verbosity @verbosity end |
#verify_ssl ⇒ Object (readonly)
Returns the value of attribute verify_ssl.
10 11 12 |
# File 'lib/radfish/core/base_client.rb', line 10 def verify_ssl @verify_ssl end |
Instance Method Details
#authenticated_request(method, path, **options) ⇒ Object
109 110 111 |
# File 'lib/radfish/core/base_client.rb', line 109 def authenticated_request(method, path, **) raise NotImplementedError, "Subclass must implement #authenticated_request" end |
#base_url ⇒ Object
43 44 45 |
# File 'lib/radfish/core/base_client.rb', line 43 def base_url @http_client.base_url end |
#http_delete(path, **options) ⇒ Object
69 70 71 |
# File 'lib/radfish/core/base_client.rb', line 69 def http_delete(path, **) @http_client.delete(path, **) end |
#http_get(path, **options) ⇒ Object
Delegate HTTP methods to the client
53 54 55 |
# File 'lib/radfish/core/base_client.rb', line 53 def http_get(path, **) @http_client.get(path, **) end |
#http_patch(path, **options) ⇒ Object
65 66 67 |
# File 'lib/radfish/core/base_client.rb', line 65 def http_patch(path, **) @http_client.patch(path, **) end |
#http_post(path, **options) ⇒ Object
57 58 59 |
# File 'lib/radfish/core/base_client.rb', line 57 def http_post(path, **) @http_client.post(path, **) end |
#http_put(path, **options) ⇒ Object
61 62 63 |
# File 'lib/radfish/core/base_client.rb', line 61 def http_put(path, **) @http_client.put(path, **) end |
#login ⇒ Object
101 102 103 |
# File 'lib/radfish/core/base_client.rb', line 101 def login raise NotImplementedError, "Subclass must implement #login" end |
#logout ⇒ Object
105 106 107 |
# File 'lib/radfish/core/base_client.rb', line 105 def logout raise NotImplementedError, "Subclass must implement #logout" end |
#redfish_version ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/radfish/core/base_client.rb', line 113 def redfish_version response = authenticated_request(:get, "/redfish/v1") if response.status == 200 data = JSON.parse(response.body) data["RedfishVersion"] else raise Error, "Failed to get Redfish version: #{response.status}" end end |
#service_root ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/radfish/core/base_client.rb', line 123 def service_root response = authenticated_request(:get, "/redfish/v1") if response.status == 200 JSON.parse(response.body) else raise Error, "Failed to get service root: #{response.status}" end end |
#vendor ⇒ Object
Vendor-specific methods to be overridden
97 98 99 |
# File 'lib/radfish/core/base_client.rb', line 97 def vendor raise NotImplementedError, "Subclass must implement #vendor" end |
#with_retries(max_retries = nil, initial_delay = nil, error_classes = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/radfish/core/base_client.rb', line 73 def with_retries(max_retries = nil, initial_delay = nil, error_classes = nil) max_retries ||= @retry_count initial_delay ||= @retry_delay error_classes ||= [StandardError] retries = 0 begin yield rescue *error_classes => e retries += 1 if retries <= max_retries delay = initial_delay * (retries ** 1.5).to_i debug "RETRY: #{e.} - Attempt #{retries}/#{max_retries}, waiting #{delay}s", 1, :yellow sleep delay retry else debug "MAX RETRIES REACHED: #{e.} after #{max_retries} attempts", 1, :red raise e end end end |