module AppManager class Client module Connection def get(path, options = {}) request :get, path, options end def post(path, options = {}) request :post, path, options end def put(path, options = {}) request :put, path, options end def delete(path, options = {}) request :delete, path, options end private def request(http_method, path, options) failsafe_or_caching_done = false if http_method.to_s == 'get' && !path.include?("/get-status") && !path.include?("/sync-charge") response = response_from_cache_for_api(http_method, path, options) failsafe_or_caching_done = true else time_out = Rails.env.development? ? 120 : 120 begin response = self.class.send(http_method, path, { body: options, timeout: time_out }) rescue Net::ReadTimeout => error # Handle timeout error here Rails.logger.info "Net::ReadTimeout: Request timed out. Please try again." response = response_from_failsafe(path, options) rescue StandardError => e # Handle other errors Rails.logger.info "Error: #{e.message}" response = response_from_failsafe(path, options) end end if path.include? "/get-status" data = response elsif path.include? "/sync-charge" data = response.parsed_response data = parse_data(response.parsed_response) else if response.class.to_s == "HTTParty::Response" && (response.code.to_s.start_with?('2') or response.code.to_s.start_with?('4')) && (response.code.to_s != '429') data = response.parsed_response data = parse_data(response.parsed_response) # data = response_from_failsafe(path,options) #uncomment to test failsafe return data elsif ((response.class == Hash) && response.has_key?("message")) data = parse_data(response) return data else if failsafe_or_caching_done == true data = response return data else data = response_from_failsafe(path,options) return data end end end end def response_from_cache_for_api(http_method, path, options) cache_key = "app-manager-cache/#{path}" if fetch_static_cached_response(cache_key).present? return fetch_static_cached_response(cache_key) end begin response = self.class.send(http_method, path, { body: options, timeout: 120 }) if response.class.to_s == "HTTParty::Response" && (response.code.to_s.start_with?('2') or response.code.to_s.start_with?('4')) && (response.code.to_s != '429') Rails.cache.write(cache_key, response, expires_in: AppManager.configuration.expires_in) else response = response_from_failsafe(path,options) end rescue Net::ReadTimeout => error response = response_from_failsafe(path,options) rescue StandardError => error response = response_from_failsafe(path, options) rescue Exception => e response = response_from_failsafe(path,options) end return response end def fetch_static_cached_response(cache_key) if Rails.cache.read(cache_key).present? return Rails.cache.read(cache_key) end end def parse_data(original_data) return unless original_data if (original_data.class == Hash) && original_data.key?("headers") && original_data.key?("footers") && original_data.key?("modal") return {"banners" => original_data} else return original_data end end def response_from_failsafe(path,options) params = Rack::Utils.parse_query URI(path).query path = path.delete_prefix('/') path = path[/[^?]+/] @fs = AppManager::FailSafe.new case path when "static-contents" return @fs.get_local_app_structures when 'plans' return @fs.get_local_plans(params) when 'plan' return @fs.get_local_plan(params) when 'store-charge' return @fs.store_local_charge(params,options) when 'cancel-charge' return @fs.store_cancel_charge(params,options) when 'get-remaining-days' return @fs.get_local_remaining_days(params,options) when 'get-charge' return @fs.get_local_charge(params,options) when 'has-plan' return @fs.get_local_has_plan(params, options) when 'discount' return @fs.get_local_discount(params, options) when 'use-discount' return @fs.store_discount_used(params,options) when 'get-related-discounted-plans' return @fs.get_local_related_discounted_plans(params,options) else return nil end end end end end