<commit_before>require "net/http"
require "addressable/template"
require "addressable/uri"

module PvOutputWrapper
  class Request
    # Do not use OpenURI, apparent security risk.
    # Method names must be the same as corresponding pvoutput.org api path name,
    #   with optional underscores.

    # Include the scheme to prevent Addressable bug.
    # See {https://github.com/bblimke/webmock/issues/489a}
    HOST = 'http://www.pvoutput.org'

    def initialize(api_key, system_id)
      @headers = {
        'X-Pvoutput-Apikey' => api_key,
        'X-Pvoutput-Systemid' => system_id
      }
    end

    # @return [PvOutput::Response]
    def get_statistic(df: nil, dt: nil, c: nil, crdr: nil, sid: nil)
      params = binding.local_variables.map { |p| [p, binding.local_variable_get(p.to_s)] }.to_h
      params.delete_if { |_, v| v.nil? }
      method_name = __method__.to_s
      uri = construct_uri(method_name, params)
      PvOutputWrapper::Response.new(method_name, get_request(uri))
    end

    private

      # @param [String, URI] full uri including any params.
      def get_request(uri)
        connection = Net::HTTP.new(HOST, 80)
        connection.get(uri, @headers)
      end

      # @params [String] service name, [Hash] query params.
      # @return [Addressable::URI] pvoutput uri.
      def construct_uri(service, params={})
        service_name = service.delete "_"
        template = Addressable::Template.new(service_path(service_name))
        template.expand({"query" => params})
      end

      # @param [String] service path as defined by pvoutput.org.
      def service_path(service)
        uri_string = "#{HOST}/service/#{revision}/#{service}.jsp/{?query*}"
        Addressable::URI.parse(uri_string)
      end

      # @return [String] the api revision number.
      def revision(rev: 'r2')
        rev
      end
  end
end<commit_msg>Rescue get_request.<commit_after>require "net/http"
require "addressable/template"
require "addressable/uri"

module PvOutputWrapper
  class Request
    # Do not use OpenURI, apparent security risk.
    # Method names must be the same as corresponding pvoutput.org api path name,
    #   with optional underscores.

    # Include the scheme to prevent Addressable bug.
    # See {https://github.com/bblimke/webmock/issues/489a}
    HOST = 'http://www.pvoutput.org'

    def initialize(api_key, system_id)
      @headers = {
        'X-Pvoutput-Apikey' => api_key,
        'X-Pvoutput-Systemid' => system_id
      }
    end

    # @return [PvOutput::Response]
    def get_statistic(df: nil, dt: nil, c: nil, crdr: nil, sid: nil)
      params = binding.local_variables.map { |p| [p, binding.local_variable_get(p.to_s)] }.to_h
      params.delete_if { |_, v| v.nil? }
      method_name = __method__.to_s
      uri = construct_uri(method_name, params)
      PvOutputWrapper::Response.new(method_name, get_request(uri))
    end

    private

      # @param [String, URI] full uri including any params.
      def get_request(uri)
        retries = 2

        begin
          connection = Net::HTTP.new(HOST, 80)
          connection.get(uri, @headers)
        rescue StandardError, Timeout::Error => e
          PvOutputWrapper::Logger.logger.error(e)
          sleep 2
          retry if (retries -= 1) > 0
          connection.finish
          raise
        end
      end

      # @params [String] service name, [Hash] query params.
      # @return [Addressable::URI] pvoutput uri.
      def construct_uri(service, params={})
        service_name = service.delete "_"
        template = Addressable::Template.new(service_path(service_name))
        template.expand({"query" => params})
      end

      # @param [String] service path as defined by pvoutput.org.
      def service_path(service)
        uri_string = "#{HOST}/service/#{revision}/#{service}.jsp/{?query*}"
        Addressable::URI.parse(uri_string)
      end

      # @return [String] the api revision number.
      def revision(rev: 'r2')
        rev
      end
  end
end
{'fraction_non_alphanumeric': 0.11510611735330836, 'fraction_numerical': 0.003995006242197253, 'mean_word_length': 2.675229357798165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '341943', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1245, 'n_words': 312}