<commit_before>require 'openssl'
require 'net/https'

module Scalarm
  module ServiceCore
    class InformationService

      ## TODO: implement, when general secrets/config access will be done
      # def self.create_from_config
      #   service_url = Rails.application.secrets.information_service_url
      #   username = Rails.application.secrets.information_service_user
      #   password = Rails.application.secrets.information_service_pass
      #   development = !!Rails.application.secrets.information_service_development
      #
      #   self.new(service_url, username, passord, development)
      # end

      def initialize(service_url, username, password, development=false)
        @service_url = service_url
        @username = username
        @password = password
        @development = development


        scheme = @development ? 'http' : 'https'

        @uri = URI("#{scheme}://#{username}:#{password}@#{service_url}")
      end

      def register_service(service, host, port)
        Logger.info("InformationService: Registering #{service} at address '#{host}:#{port}'")
        code, body = send_request(service, {address: "#{host}:#{port}"})

        if code == '200'
          response = JSON.parse(body)
          puts response.inspect
          if response['status'] == 'ok'
            return nil, response['msg']
          else
            return 'error', response['msg']
          end
        else
          return 'error', code
        end
      end

      def deregister_service(service, host, port)
        code, body = send_request(service, {address: "#{host}:#{port}"}, method: 'DELETE')

        if code == '200'
          response = JSON.parse(body)
          if response['status'] == 'ok'
            return nil, response['msg']
          else
            return 'error', response['msg']
          end
        else
          return 'error', code
        end
      end

      def get_list_of(service)
        code, body = send_request(service)

        if code == '200'
          JSON.parse(body)
        else
          []
        end
      end

      def send_request(request, data = nil, opts = {})
        Logger.info("[InformationService]: sending #{request} request at '#{@uri}")
        resource = @uri.path + '/' + request

        req = if data.nil?
                Net::HTTP::Get.new(resource)
              else
                if opts.include?(:method) and opts[:method] == 'DELETE'
                  Net::HTTP::Delete.new(resource)
                else
                  Net::HTTP::Post.new(resource)
                end
              end

        if not req.is_a?(Net::HTTP::Get)
          req.basic_auth(@username, @password)
          req.set_form_data(data) unless data.nil?
        end

        if @uri.scheme == 'http'
          ssl_options = {}
        else
          ssl_options = { use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE }
        end

        begin
          response = Net::HTTP.start(@uri.host, @uri.port, ssl_options) { |http| http.request(req) }
          #puts "#{Time.now} --- response from Information Service is #{response.code} #{response.body}"
          return response.code, response.body
        rescue => e
          Logger.error("[InformationService] Exception occurred: #{e.to_s}")
          Logger.error(e.backtrace.join("\n\t"))

          raise
        end

        return nil, nil
      end

      def sample_public_url(service)
        (get_list_of(service) or []).sample
      end

    end
  end
end
<commit_msg>SCAL-1225: logging only scheme, host and resource to information service<commit_after>require 'openssl'
require 'net/https'

module Scalarm
  module ServiceCore
    class InformationService

      ## TODO: implement, when general secrets/config access will be done
      # def self.create_from_config
      #   service_url = Rails.application.secrets.information_service_url
      #   username = Rails.application.secrets.information_service_user
      #   password = Rails.application.secrets.information_service_pass
      #   development = !!Rails.application.secrets.information_service_development
      #
      #   self.new(service_url, username, passord, development)
      # end

      def initialize(service_url, username, password, development=false)
        @service_url = service_url
        @username = username
        @password = password
        @development = development


        scheme = @development ? 'http' : 'https'

        @uri = URI("#{scheme}://#{username}:#{password}@#{service_url}")
      end

      def register_service(service, host, port)
        Logger.info("InformationService: Registering #{service} at address '#{host}:#{port}'")
        code, body = send_request(service, {address: "#{host}:#{port}"})

        if code == '200'
          response = JSON.parse(body)
          puts response.inspect
          if response['status'] == 'ok'
            return nil, response['msg']
          else
            return 'error', response['msg']
          end
        else
          return 'error', code
        end
      end

      def deregister_service(service, host, port)
        code, body = send_request(service, {address: "#{host}:#{port}"}, method: 'DELETE')

        if code == '200'
          response = JSON.parse(body)
          if response['status'] == 'ok'
            return nil, response['msg']
          else
            return 'error', response['msg']
          end
        else
          return 'error', code
        end
      end

      def get_list_of(service)
        code, body = send_request(service)

        if code == '200'
          JSON.parse(body)
        else
          []
        end
      end

      def send_request(request, data = nil, opts = {})
        resource = @uri.path + '/' + request

        Logger.info("[InformationService] sending request at '#{@uri.scheme}://#{@uri.host}#{resource}")

        req = if data.nil?
                Net::HTTP::Get.new(resource)
              else
                if opts.include?(:method) and opts[:method] == 'DELETE'
                  Net::HTTP::Delete.new(resource)
                else
                  Net::HTTP::Post.new(resource)
                end
              end

        if not req.is_a?(Net::HTTP::Get)
          req.basic_auth(@username, @password)
          req.set_form_data(data) unless data.nil?
        end

        if @uri.scheme == 'http'
          ssl_options = {}
        else
          ssl_options = { use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE }
        end

        begin
          response = Net::HTTP.start(@uri.host, @uri.port, ssl_options) { |http| http.request(req) }
          #puts "#{Time.now} --- response from Information Service is #{response.code} #{response.body}"
          return response.code, response.body
        rescue => e
          Logger.error("[InformationService] Exception occurred: #{e.to_s}")
          Logger.error(e.backtrace.join("\n\t"))

          raise
        end

        return nil, nil
      end

      def sample_public_url(service)
        (get_list_of(service) or []).sample
      end

    end
  end
end

{'fraction_non_alphanumeric': 0.11808585503166784, 'fraction_numerical': 0.003096410978184377, 'mean_word_length': 2.0251170710940825, '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': '4931620', 'n_tokens_mistral': 2004, 'n_tokens_neox': 1940, 'n_words': 464}