Class: LabClient::Client
- Inherits:
-
Object
- Object
- LabClient::Client
- Includes:
- ClientHelpers, ClientSetup, Logger
- Defined in:
- lib/labclient/client.rb,
lib/labclient/client/meta.rb
Overview
Variables / Meta
Instance Attribute Summary collapse
-
#delay ⇒ Object
include HTTParty.
-
#http ⇒ Object
include HTTParty.
-
#klass ⇒ Object
include HTTParty.
-
#link ⇒ Object
include HTTParty.
-
#path ⇒ Object
include HTTParty.
-
#resp ⇒ Object
include HTTParty.
-
#retries ⇒ Object
include HTTParty.
-
#settings ⇒ Object
include HTTParty.
Instance Method Summary collapse
-
#initialize(user_settings = nil) ⇒ Client
constructor
Default setup, pull in settings.
- #inspect ⇒ Object
-
#post_request_handlers ⇒ Object
Handling Details for after the request Debug,Retry, Instance Variables, Error Handling.
-
#process(resp) ⇒ Object
Assume we want LabStruct if @klass is ever nil.
-
#request(method, path, klass = nil, body = {}, dump_json = true) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
-
#subclasses ⇒ Object
Helper to make subclasses directly accessible.
Methods included from Logger
Methods included from ClientSetup
#fill_configuration, #prompt_for_token, #prompt_for_url, #setup_profile, #unspecified_defaults
Methods included from ClientHelpers
#api_methods, #base_url, #debug?, #debug_handler, #delay_factor, #help, #home_file, #profile, #quiet?, #retry_after, #retry_debug_headers, #retry_header, #retry_max, #retry_max?, #retry_update, #save_client, #save_path, #should_retry?
Constructor Details
#initialize(user_settings = nil) ⇒ Client
Default setup, pull in settings
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/labclient/client.rb', line 10 def initialize(user_settings = nil) @settings = user_settings setup_profile if user_settings&.key?(:profile) || ENV['LABCLIENT_PROFILE'] @settings ||= fill_configuration # Set Unspecified Defaults unspecified_defaults prompt_for_url if @settings[:url].blank? # Only prompt if explicitly set to nil prompt_for_token if @settings[:token].nil? # Initial Delay / Retry Value self.delay = 0 self.retries = 0 # Request Configuration self.http = HTTP.new(@settings) end |
Instance Attribute Details
#delay ⇒ Object
include HTTParty
6 7 8 |
# File 'lib/labclient/client/meta.rb', line 6 def delay @delay end |
#http ⇒ Object
include HTTParty
6 7 8 |
# File 'lib/labclient/client/meta.rb', line 6 def http @http end |
#klass ⇒ Object
include HTTParty
6 7 8 |
# File 'lib/labclient/client/meta.rb', line 6 def klass @klass end |
#link ⇒ Object
include HTTParty
6 7 8 |
# File 'lib/labclient/client/meta.rb', line 6 def link @link end |
#path ⇒ Object
include HTTParty
6 7 8 |
# File 'lib/labclient/client/meta.rb', line 6 def path @path end |
#resp ⇒ Object
include HTTParty
6 7 8 |
# File 'lib/labclient/client/meta.rb', line 6 def resp @resp end |
#retries ⇒ Object
include HTTParty
6 7 8 |
# File 'lib/labclient/client/meta.rb', line 6 def retries @retries end |
#settings ⇒ Object
include HTTParty
6 7 8 |
# File 'lib/labclient/client/meta.rb', line 6 def settings @settings end |
Instance Method Details
#inspect ⇒ Object
8 9 10 |
# File 'lib/labclient/client/meta.rb', line 8 def inspect "#<LabClient::Client url: \"#{@settings[:url]}\">" end |
#post_request_handlers ⇒ Object
Handling Details for after the request Debug,Retry, Instance Variables, Error Handling
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/labclient/client.rb', line 62 def post_request_handlers # Handle Retry Logic raise LabClient::Retry if should_retry? # Save Client save_client # Reset Delay/Retry Factor retry_update if resp.success? # Exit on Max Retries raise LabClient::Error.new(resp), resp.friendly_error if retry_max? raise LabClient::Error.new(resp), resp.friendly_error unless resp.success? # Drop in raw path save_path end |
#process(resp) ⇒ Object
Assume we want LabStruct if @klass is ever nil
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/labclient/client.rb', line 82 def process(resp) case resp.data when LabStruct klass ? klass.new(resp.data, resp, self) : resp.data when Array if klass.nil? PaginatedResponse.new(Klass, resp, self) else PaginatedResponse.new(klass, resp, self) end # Return Response/Status Object if there is no output otherwise when NilClass resp # Default handler / if klass defined store plain response in data else klass ? klass.new({ data: resp.data, response: resp }, resp, self) : resp end end |
#request(method, path, klass = nil, body = {}, dump_json = true) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/labclient/client.rb', line 32 def request(method, path, klass = nil, body = {}, dump_json = true) self.klass = klass self.resp = http.request(method, path, body, dump_json) self.path = path debug_handler if debug? post_request_handlers process resp rescue LabClient::Error => e logger.fatal('Request Failed', e.error_details) unless quiet? resp rescue LabClient::Retry self.retries += 1 # Assume Retry After by Default logger.debug('Retry After', value: retry_after, retry_debug_headers: retry_debug_headers) if debug? self.delay = retry_after if resp.headers.key? 'retry-after' self.delay += delay_factor logger.warn "Received #{resp.code}. Retry in #{delay}", limit: retry_max, retries: retries unless quiet? sleep delay unless ENV['LABCLIENT_TESTING'] retry end |
#subclasses ⇒ Object
Helper to make subclasses directly accessible
13 14 15 |
# File 'lib/labclient/client/meta.rb', line 13 def subclasses self.class.instance_variable_get(:@subclasses) end |