<commit_before>require 'sawyer'
require 'uri'

module Vx
  module ServiceConnector
    class GitlabV6
      Session = Struct.new(:endpoint, :private_token) do

        def get(url, options = {})
          wrap do
            res = agent.call :get, request_url(url), nil, query: options
            response! res
          end
        end

        def post(url, options = {})
          wrap do
            res = agent.call :post, request_url(url), options, nil
            response! res
          end
        end

        def delete(url, options = {})
          wrap do
            res = agent.call :delete, request_url(url), nil, query: options
            response! res
          end
        end

        def uri
          @uri ||= URI(endpoint)
        end

        private

          def wrap
            begin
              yield
            rescue Errno::ETIMEDOUT => e
              raise RequestError, e
            end
          end

          def response!(res)
            if (200..204).include?(res.status)
              res.data
            else
              raise RequestError, res.data.inspect
            end
          end

          def request_url(url)
            "/api/v3/#{url}"
          end

          def api_endpoint
            "#{endpoint}/api/v3"
          end

          def agent
            @agent ||= Sawyer::Agent.new(api_endpoint) do |http|
              http.headers['content-type']  = 'application/json'
              http.headers['accept']        = 'application/json'
              http.headers['PRIVATE-TOKEN'] = private_token
              http.ssl.verify = false
            end
          end
      end

    end
  end
end<commit_msg>add timeouts to faraday requests<commit_after>require 'sawyer'
require 'uri'

module Vx
  module ServiceConnector
    class GitlabV6
      Session = Struct.new(:endpoint, :private_token) do

        def get(url, options = {})
          wrap do
            res = agent.call :get, request_url(url), nil, query: options
            response! res
          end
        end

        def post(url, options = {})
          wrap do
            res = agent.call :post, request_url(url), options, nil
            response! res
          end
        end

        def delete(url, options = {})
          wrap do
            res = agent.call :delete, request_url(url), nil, query: options
            response! res
          end
        end

        def uri
          @uri ||= URI(endpoint)
        end

        private

          def wrap
            begin
              yield
            rescue Errno::ETIMEDOUT => e
              raise RequestError, e
            end
          end

          def response!(res)
            if (200..204).include?(res.status)
              res.data
            else
              raise RequestError, res.data.inspect
            end
          end

          def request_url(url)
            "/api/v3/#{url}"
          end

          def api_endpoint
            "#{endpoint}/api/v3"
          end

          def agent
            @agent ||= Sawyer::Agent.new(api_endpoint) do |http|
              http.headers['content-type']  = 'application/json'
              http.headers['accept']        = 'application/json'
              http.headers['PRIVATE-TOKEN'] = private_token
              http.ssl.verify               = false
              http.options[:timeout]        = 5
              http.options[:open_timeout]   = 5
            end
          end
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.08633093525179857, 'fraction_numerical': 0.0057553956834532375, 'mean_word_length': 1.2225063938618925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7478028', 'n_tokens_mistral': 927, 'n_tokens_neox': 907, 'n_words': 243}