Sha256: 05e73d34c22b4a21f383fbbf95ed66dc554b03601e7fcca9368e488619af9adc

Contents?: true

Size: 1.36 KB

Versions: 20

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module HTTPX
  module Plugins
    module GRPC
      # Encoding module for GRPC responses
      #
      # Can encode and decode grpc messages.
      module Message
        module_function

        # decodes a unary grpc response
        def unary(response)
          verify_status(response)

          decoder = Transcoder::GRPCEncoding.decode(response)

          decoder.call(response.to_s)
        end

        # lazy decodes a grpc stream response
        def stream(response, &block)
          return enum_for(__method__, response) unless block

          decoder = Transcoder::GRPCEncoding.decode(response)

          response.each do |frame|
            decoder.call(frame, &block)
          end

          verify_status(response)
        end

        def cancel(request)
          request.emit(:refuse, :client_cancellation)
        end

        # interprets the grpc call trailing metadata, and raises an
        # exception in case of error code
        def verify_status(response)
          # return standard errors if need be
          response.raise_for_status

          status = Integer(response.headers["grpc-status"])
          message = response.headers["grpc-message"]

          return if status.zero?

          response.close
          raise GRPCError.new(status, message, response.trailing_metadata)
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
httpx-1.3.4 lib/httpx/plugins/grpc/message.rb
httpx-1.3.3 lib/httpx/plugins/grpc/message.rb
httpx-1.3.2 lib/httpx/plugins/grpc/message.rb
httpx-1.3.1 lib/httpx/plugins/grpc/message.rb
httpx-1.3.0 lib/httpx/plugins/grpc/message.rb
httpx-1.2.6 lib/httpx/plugins/grpc/message.rb
httpx-1.2.4 lib/httpx/plugins/grpc/message.rb
httpx-1.2.3 lib/httpx/plugins/grpc/message.rb
httpx-1.2.2 lib/httpx/plugins/grpc/message.rb
httpx-1.2.1 lib/httpx/plugins/grpc/message.rb
httpx-1.2.0 lib/httpx/plugins/grpc/message.rb
httpx-1.1.5 lib/httpx/plugins/grpc/message.rb
httpx-1.1.4 lib/httpx/plugins/grpc/message.rb
httpx-1.1.3 lib/httpx/plugins/grpc/message.rb
httpx-1.1.2 lib/httpx/plugins/grpc/message.rb
httpx-1.1.1 lib/httpx/plugins/grpc/message.rb
httpx-1.1.0 lib/httpx/plugins/grpc/message.rb
httpx-1.0.2 lib/httpx/plugins/grpc/message.rb
httpx-1.0.1 lib/httpx/plugins/grpc/message.rb
httpx-1.0.0 lib/httpx/plugins/grpc/message.rb