Module: Pwnix::Api::Client::HTTPartyTime
- Included in:
- ApiClient, ApiConfig, ClientManager, ConsoleClient, Cookbook, Hardware, Logs, Mode, Network, Node, Os, Service, Shell, ShellReceiverConfig, Sms, Ssh, System, Utility
- Defined in:
- lib/pwnix-api-client/api_client.rb
Class Method Summary collapse
Instance Method Summary collapse
- #clear_errors ⇒ Object
- #errors ⇒ Object
- #has_errors? ⇒ Boolean
- #last_error ⇒ Object
- #safe_api_call(endpoint, params = {}) ⇒ Object
Class Method Details
.included(base) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/pwnix-api-client/api_client.rb', line 61 def self.included(base) base.base_uri Pwnix::Api::Client::Config.instance['api_base_uri'] base.basic_auth Pwnix::Api::Client::Config.instance['api_username'], Pwnix::Api::Client::Config.instance['api_password'] base.debug_output base.format :json @@errors = ErrorQueue.instance end |
Instance Method Details
#clear_errors ⇒ Object
83 84 85 |
# File 'lib/pwnix-api-client/api_client.rb', line 83 def clear_errors @@errors.queue = [] end |
#errors ⇒ Object
75 76 77 |
# File 'lib/pwnix-api-client/api_client.rb', line 75 def errors @@errors.queue end |
#has_errors? ⇒ Boolean
71 72 73 |
# File 'lib/pwnix-api-client/api_client.rb', line 71 def has_errors? !@@errors.queue.empty? end |
#last_error ⇒ Object
79 80 81 |
# File 'lib/pwnix-api-client/api_client.rb', line 79 def last_error @@errors.queue.last end |
#safe_api_call(endpoint, params = {}) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/pwnix-api-client/api_client.rb', line 87 def safe_api_call(endpoint,params={}) begin response = self.class.post(endpoint,{:body => params}) case response.code when 200 # TODO - everything should return a hash parsed_response = response.parsed_response['result'] if parsed_response.is_a? Hash if parsed_response['error'] @@errors.queue.push "Handled exception: #{parsed_response['error']}" end end return parsed_response when 404 @@errors.queue.push "404 - not found! #{endpoint} #{parsed_response}" when 500...600 @@errors.queue.push "500 - server exception! #{response}" end rescue Exception => e @@errors.queue.push "Client error: #{e}" return nil end end |