Class: NetworkManager
- Inherits:
-
Object
- Object
- NetworkManager
- Defined in:
- lib/wingify/packages/network_layer/manager/network_manager.rb
Class Method Summary collapse
Instance Method Summary collapse
- #attach_client(client = nil, retry_config = nil) ⇒ Object
- #create_request(request) ⇒ Object
- #get(request) ⇒ Object
- #get_client ⇒ Object
- #get_config ⇒ Object
- #get_retry_config ⇒ Object
-
#initialize(options = {}) ⇒ NetworkManager
constructor
A new instance of NetworkManager.
- #post(request) ⇒ Object
- #set_config(config) ⇒ Object
- #validate_retry_config(retry_config) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ NetworkManager
Returns a new instance of NetworkManager.
27 28 29 30 31 32 |
# File 'lib/wingify/packages/network_layer/manager/network_manager.rb', line 27 def initialize( = {}) @client = NetworkClient.new() @config = GlobalRequestModel.new(nil, {}, {}, {}) @should_use_threading = .key?(:enabled) ? [:enabled] : Constants::SHOULD_USE_THREADING @retry_config = nil end |
Class Method Details
.instance(options = {}) ⇒ Object
34 35 36 |
# File 'lib/wingify/packages/network_layer/manager/network_manager.rb', line 34 def self.instance( = {}) @instance ||= new() end |
Instance Method Details
#attach_client(client = nil, retry_config = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/wingify/packages/network_layer/manager/network_manager.rb', line 38 def attach_client(client = nil, retry_config = nil) # Only set retry configuration if it's not already initialized or if a new config is provided if !@retry_config || retry_config # Define default retry configuration default_retry_config = Constants::DEFAULT_RETRY_CONFIG.dup # Merge provided retry_config with defaults, giving priority to provided values merged_config = default_retry_config.merge(retry_config || {}) # Validate the merged configuration @retry_config = validate_retry_config(merged_config) end end |
#create_request(request) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/wingify/packages/network_layer/manager/network_manager.rb', line 112 def create_request(request) network_request = RequestHandler.new.create_request(request, @config) # Set retry config from network manager if not already set in request if @retry_config && (!network_request.get_retry_config || network_request.get_retry_config.nil?) network_request.set_retry_config(@retry_config.dup) end network_request end |
#get(request) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/wingify/packages/network_layer/manager/network_manager.rb', line 121 def get(request) begin = create_request(request) raise 'No URL found' if .get_url.nil? response = @client.get() response rescue => e return ResponseModel.new.set_error(e.) end end |
#get_client ⇒ Object
100 101 102 |
# File 'lib/wingify/packages/network_layer/manager/network_manager.rb', line 100 def get_client @client end |
#get_config ⇒ Object
108 109 110 |
# File 'lib/wingify/packages/network_layer/manager/network_manager.rb', line 108 def get_config @config end |
#get_retry_config ⇒ Object
52 53 54 |
# File 'lib/wingify/packages/network_layer/manager/network_manager.rb', line 52 def get_retry_config @retry_config ? @retry_config.dup : nil end |
#post(request) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/wingify/packages/network_layer/manager/network_manager.rb', line 134 def post(request) begin = create_request(request) raise 'No URL found' if .get_url.nil? response = @client.post() # Return the response response rescue => e return ResponseModel.new.set_error(e.) # Return error response end end |
#set_config(config) ⇒ Object
104 105 106 |
# File 'lib/wingify/packages/network_layer/manager/network_manager.rb', line 104 def set_config(config) @config = config end |
#validate_retry_config(retry_config) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/wingify/packages/network_layer/manager/network_manager.rb', line 56 def validate_retry_config(retry_config) validated_config = retry_config.dup is_invalid_config = false # Validate should_retry: should be a boolean value if !DataTypeUtil.is_boolean(validated_config[:should_retry]) validated_config[:should_retry] = Constants::DEFAULT_RETRY_CONFIG[:should_retry] is_invalid_config = true end # Validate max_retries: should be a non-negative integer and should not be less than 1 if !DataTypeUtil.is_number(validated_config[:max_retries]) || !validated_config[:max_retries].is_a?(Integer) || validated_config[:max_retries] < 1 validated_config[:max_retries] = Constants::DEFAULT_RETRY_CONFIG[:max_retries] is_invalid_config = true end # Validate initial_delay: should be a non-negative integer and should not be less than 1 if !DataTypeUtil.is_number(validated_config[:initial_delay]) || !validated_config[:initial_delay].is_a?(Integer) || validated_config[:initial_delay] < 1 validated_config[:initial_delay] = Constants::DEFAULT_RETRY_CONFIG[:initial_delay] is_invalid_config = true end # Validate backoff_multiplier: should be a non-negative integer and should not be less than 2 if !DataTypeUtil.is_number(validated_config[:backoff_multiplier]) || !validated_config[:backoff_multiplier].is_a?(Integer) || validated_config[:backoff_multiplier] < 2 validated_config[:backoff_multiplier] = Constants::DEFAULT_RETRY_CONFIG[:backoff_multiplier] is_invalid_config = true end if is_invalid_config LoggerService.log(LogLevelEnum::ERROR, "INVALID_RETRY_CONFIG", { retryConfig: validated_config.to_json, an: ApiEnum::INIT }) end is_invalid_config ? Constants::DEFAULT_RETRY_CONFIG.dup : validated_config end |