# frozen_string_literal: true require_relative "qas_dock_gem/version" require_relative "qas_dock_gem/services/aws_services" require_relative "qas_dock_gem/services/redis_services" module QasDockGem class RequestsAPI require "httparty" require "logger" def initialize @logger = Logger.new(STDOUT) end def initialize_headers(token, headers) headers_default = {"cache-control": "no-cache", "content-type": "application/json"} #seta o campo authorization caso o token seja diferente de nulo headers_default["Authorization"] = token unless token.nil? #define os headers parametrizados como um Hash caso seja nulo headers = {} if headers.nil? #realiza o merge dos headers defaults + os headers parametrizados headers_default.merge!(headers) #remove o campo content-type caso não seja informado headers_default.delete(:"content-type") if headers_default[:"content-type"] == "" or headers_default[:"content-type"].nil? #retorna os headers JSON.parse(headers_default.to_json) end def initialize_request(params) formated_params = init_request_body(params[:token], params[:payload], params[:headers]) if params.include?(:payload) formated_params = init_request_query(params[:token], params[:query], params[:headers]) if params.include?(:query) formated_params = init_request_basic_auth(params[:token], params[:basic_auth], params[:headers]) if params.include?(:basic_auth) #retorna os params formatados para a request formated_params end def init_request_body(token, payload, headers) final_headers = initialize_headers(token, headers) payload = {} if payload.nil? @logger.info("Payload: #{payload.to_json}") @logger.info("Headers: #{final_headers}") { :headers => final_headers, :body => payload.to_json } end def init_request_basic_auth(token, basic_auth, headers) final_headers = initialize_headers(token, headers) payload = {} if payload.nil? @logger.info("Headers: #{final_headers}") { :headers => final_headers, :basic_auth => basic_auth } end def init_request_query(token, query, headers) final_headers = initialize_headers(token, headers) query = {} if query.nil? @logger.info("Query: #{query}") @logger.info("Headers: #{final_headers}") { :headers => final_headers, :query => query } end def execute_post(params) formated_params = initialize_request(params) @logger.info("Realizando POST") @logger.info("URI: #{params[:uri]}") request = HTTParty.post(params[:uri], formated_params) @logger.info("URI Final: #{request.request.last_uri}") @logger.info("POST realizado \n") request end def execute_get(params) formated_params = initialize_request(params) @logger.info("Realizando GET") @logger.info("URI: #{params[:uri]}") request = HTTParty.get(params[:uri], formated_params) @logger.info("URI Final: #{request.request.last_uri}") @logger.info("GET realizado \n") request end def execute_patch(params) formated_params = initialize_request(params) @logger.info("Realizando PATCH") @logger.info("URI: #{params[:uri]}") request = HTTParty.patch(params[:uri], formated_params) @logger.info("URI Final: #{request.request.last_uri}") @logger.info("PATCH realizado \n") request end def execute_put(params) formated_params = initialize_request(params) @logger.info("Realizando PUT") @logger.info("URI: #{params[:uri]}") request = HTTParty.put(params[:uri], formated_params) @logger.info("URI Final: #{request.request.last_uri}") @logger.info("PUT realizado \n") request end def execute_delete(params) formated_params = initialize_request(params) @logger.info("Realizando DELETE") @logger.info("URI: #{params[:uri]}") request = HTTParty.put(params[:uri], formated_params) @logger.info("URI Final: #{request.request.last_uri}") @logger.info("DELETE realizado \n") request end end end