Sha256: aba64880cd30c5dbb645bc65ac1c587b5cf9282b6b6c1020e8179670a4d99c6f

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Scim
  module Kit
    class Http
      attr_reader :driver, :retries

      def initialize(driver: Http.default_driver, retries: 3)
        @driver = driver
        @retries = retries
      end

      def get(uri)
        driver.with_retry(retries: retries) do |client|
          response = client.get(uri)
          ok?(response) ? JSON.parse(response.body, symbolize_names: true) : {}
        end
      rescue *Net::Hippie::CONNECTION_ERRORS => error
        Scim::Kit.logger.error(error)
        {}
      end

      def self.default_driver
        @default_driver ||= Net::Hippie::Client.new(headers: headers).tap do |http|
          http.logger = Scim::Kit.logger
          http.open_timeout = 1
          http.read_timeout = 5
          http.follow_redirects = 3
        end
      end

      def self.headers
        {
          'Accept' => 'application/scim+json',
          'Content-Type' => 'application/scim+json',
          'User-Agent' => "scim/kit #{Scim::Kit::VERSION}"
        }
      end

      private

      def ok?(response)
        response.is_a?(Net::HTTPSuccess)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scim-kit-0.5.1 lib/scim/kit/http.rb