Module: HTTPX::Plugins::GRPC::Message

Defined in:
lib/httpx/plugins/grpc/message.rb

Overview

Encoding module for GRPC responses

Can encode and decode grpc messages.

Class Method Summary collapse

Class Method Details

.cancel(request) ⇒ Object



34
35
36
# File 'lib/httpx/plugins/grpc/message.rb', line 34

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

.stream(response, &block) ⇒ Object

lazy decodes a grpc stream response



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/httpx/plugins/grpc/message.rb', line 22

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

.unary(response) ⇒ Object

decodes a unary grpc response



13
14
15
16
17
18
19
# File 'lib/httpx/plugins/grpc/message.rb', line 13

def unary(response)
  verify_status(response)

  decoder = Transcoder::GRPCEncoding.decode(response)

  decoder.call(response.to_s)
end

.verify_status(response) ⇒ Object

interprets the grpc call trailing metadata, and raises an exception in case of error code

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/httpx/plugins/grpc/message.rb', line 40

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.)
end