<commit_before>require 'active_remote/serializers/protobuf'

module ActiveRemote
  module RPC
    extend ActiveSupport::Concern

    included do
      include Serializers::Protobuf
    end

    module ClassMethods

      # Execute an RPC call to the remote service and return the raw response.
      #
      def remote_call(rpc_method, request_args)
        remote = self.new
        remote.execute(rpc_method, request_args)
      end

      # Return a protobuf request object for the given rpc request.
      #
      def request(rpc_method, request_args)
        return request_args unless request_args.is_a?(Hash)

        message_class = request_type(rpc_method)
        fields = fields_from_attributes(message_class, request_args)
        message_class.new(fields)
      end

      # Return the class applicable to the request for the given rpc method.
      #
      def request_type(rpc_method)
        service_class.rpcs[rpc_method].request_type
      end
    end

    # Invoke an RPC call to the service for the given rpc method.
    #
    def execute(rpc_method, request_args)
      @last_request = request(rpc_method, request_args)

      _service_class.client.__send__(rpc_method, @last_request) do |c|

        # In the event of service failure, raise the error.
        c.on_failure do |error|
          raise ActiveRemoteError, error.message
        end

        # In the event of service success, assign the response.
        c.on_success do |response|
          @last_response = response
        end
      end

      @last_response
    end

    # Execute an RPC call to the remote service and return the raw response.
    #
    def remote_call(rpc_method, request_args)
      self.execute(rpc_method, request_args)<commit_msg>Execute RPC calls using .rpc instead of instances<commit_after>require 'active_remote/serializers/protobuf'

module ActiveRemote
  module RPC
    extend ActiveSupport::Concern

    included do
      include Serializers::Protobuf
    end

    module ClassMethods

      # Execute an RPC call to the remote service and return the raw response.
      #
      def remote_call(rpc_method, request_args)
        rpc.execute(rpc_method, request_args)
      end

      # Return a protobuf request object for the given rpc request.
      #
      def request(rpc_method, request_args)
        return request_args unless request_args.is_a?(Hash)

        message_class = request_type(rpc_method)
        fields = fields_from_attributes(message_class, request_args)
        message_class.new(fields)
      end

      # Return the class applicable to the request for the given rpc method.
      #
      def request_type(rpc_method)
        service_class.rpcs[rpc_method].request_type
      end

      # TODO: Make this a first class citizen instead of embedding it inside
      # the remote class
      def rpc
        self.new
      end
    end

    # Invoke an RPC call to the service for the given rpc method.
    #
    def execute(rpc_method, request_args)
      @last_request = request(rpc_method, request_args)

      _service_class.client.__send__(rpc_method, @last_request) do |c|

        # In the event of service failure, raise the error.
        c.on_failure do |error|
          raise ActiveRemoteError, error.message
        end

        # In the event of service success, assign the response.
        c.on_success do |response|
          @last_response = response
        end
      end

      @last_response
    end

    # Execute an RPC call to the remote service and return the raw response.
    #
    def remote_call(rpc_method, request_args)
      rpc.execute(rpc_method, request_args)
    end

    def rpc
      self.class.rpc
{'fraction_non_alphanumeric': 0.05470876428960261, 'fraction_numerical': 0.0, 'mean_word_length': 2.5370548604427334, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6477569', 'n_tokens_mistral': 1039, 'n_tokens_neox': 1009, 'n_words': 329}