Sha256: e2954b4cd661b39ec21334b517ade39f2e1aee09b0c2e5474a1d37ee2ff7925b

Contents?: true

Size: 1.39 KB

Versions: 6

Compression:

Stored size: 1.39 KB

Contents

require 'net/http'
require 'open-uri'

module TerraspaceBundler::Mod::Http
  module Concern
    include TB::Util::Logging

    def http_request(url, auth_domain: nil)
      uri = URI(url)
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = uri.scheme == "https"
      # Total time will be 40s = 20 x 2
      http.max_retries = 1 # Default is already 1, just  being explicit
      http.read_timeout = 20 # Sites that dont return in 20 seconds are considered down
      request = Net::HTTP::Get.new(uri)

      if auth_domain
        path = "#{ENV['HOME']}/.terraform.d/credentials.tfrc.json"
        if File.exist?(path)
          data = JSON.load(IO.read(path))
          token = data['credentials'][auth_domain]['token']
          request.add_field 'Authorization', "Bearer #{token}"
        else
          auth_error_exit!
        end
      end

      begin
         http.request(request) # response
      rescue Net::OpenTimeout => e # internal ELB but VPC is not configured for Lambda function
        http_request_error_message(e)
      rescue Exception => e
        # Net::ReadTimeout - too slow
        # Errno::ECONNREFUSED - completely down
        # SocketError - improper url "dsfjsl" instead of example.com
        http_request_error_message(e)
      end
    end

    def http_request_error_message(e)
      logger.info "ERROR: #{e.message}\n#{e.message}".color(:red)
      exit 1
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
terraspace-bundler-0.5.0 lib/terraspace_bundler/mod/http/concern.rb
terraspace-bundler-0.4.4 lib/terraspace_bundler/mod/http/concern.rb
terraspace-bundler-0.4.3 lib/terraspace_bundler/mod/http/concern.rb
terraspace-bundler-0.4.2 lib/terraspace_bundler/mod/http/concern.rb
terraspace-bundler-0.4.1 lib/terraspace_bundler/mod/http/concern.rb
terraspace-bundler-0.4.0 lib/terraspace_bundler/mod/http/concern.rb