<commit_before>require 'lokalebasen_settings_client/version'
require 'timeout'
require 'faraday'
require 'json'

module LokalebasenSettingsClient
  class BackendError < RuntimeError; end
  class TimeoutError < RuntimeError; end

  # Caching client for lokalebase settings
  class CachingClient
    attr_writer :timeout, :cache_time, :reraise_error

    def initialize(url, site_key)
      @url = url
      @site_key = site_key

      # Default values
      @timeout = 0.5
      @cache_time = 60 * 60 # 1 Hour
      @reraise_error = true
    end

    def healthy?
      health_check.status == 200
    end

    def get
      update_cache unless cache_valid?
      @cache[:value]
    end

    private

    def update_cache
      Timeout.timeout(@timeout, TimeoutError) do
        @cache = {
          value: json,
          expires: Time.now + @cache_time
        }
      end
    rescue Exception => e
      @cache[:expires] = Time.now + @cache_time if @cache.is_a?(Hash)
      Airbrake.notify(e) if defined?(Airbrake)
      raise e if @reraise_error
    end

    def cache_valid?
      !@cache.nil? && !@cache[:expires].nil? && @cache[:expires] > Time.now
    end

    def health_check
      client.get('/health_check')
    end

    def json
      response = fetch
      fail(BackendError, response.body) unless response.status == 200
      JSON.parse(response.body)
    end

    def fetch
      client.get("/api/#{@site_key}")
    end

    def client
      @client ||= Faraday.new(@url) do |faraday|
        faraday.adapter :excon
      end
    end
  end
end<commit_msg>Extract HTTP stuff into Client<commit_after>require 'lokalebasen_settings_client/version'
require 'timeout'
require 'faraday'
require 'json'

module LokalebasenSettingsClient
  class BackendError < RuntimeError; end
  class TimeoutError < RuntimeError; end

  class Client
    attr_reader :settings_service_url

    def initialize(settings_service_url)
      @settings_service_url = settings_service_url
    end

    def client
      @client ||= Faraday.new(settings_service_url) do |faraday|
        faraday.adapter :excon
      end
    end
  end

  # Caching client for lokalebase settings
  class CachingClient
    attr_writer :timeout, :cache_time, :reraise_error

    def initialize(url, site_key)
      @url = url
      @site_key = site_key

      # Default values
      @timeout = 0.5
      @cache_time = 60 * 60 # 1 Hour
      @reraise_error = true
    end

    def healthy?
      health_check.status == 200
    end

    def get
      update_cache unless cache_valid?
      @cache[:value]
    end

    private

    def update_cache
      Timeout.timeout(@timeout, TimeoutError) do
        @cache = {
          value: json,
          expires: Time.now + @cache_time
        }
      end
    rescue Exception => e
      @cache[:expires] = Time.now + @cache_time if @cache.is_a?(Hash)
      Airbrake.notify(e) if defined?(Airbrake)
      raise e if @reraise_error
    end

    def cache_valid?
      !@cache.nil? && !@cache[:expires].nil? && @cache[:expires] > Time.now
    end

    def health_check
      client.get('/health_check')
    end

    def json
      response = fetch
      fail(BackendError, response.body) unless response.status == 200
      JSON.parse(response.body)
    end

    def fetch
      client.get("/api/#{@site_key}")
    end

    def client
      Client.new(@url).client
    end

  end
end
{'fraction_non_alphanumeric': 0.09005019191024506, 'fraction_numerical': 0.007676409802184824, 'mean_word_length': 2.478439425051335, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3084849', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1055, 'n_words': 271}