Module: Protocol::GRPC::Methods
- Defined in:
- lib/protocol/grpc/methods.rb
Overview
Provides utility methods for building and parsing gRPC-compatible HTTP requests.
Class Method Summary collapse
-
.build_headers(metadata: {}, timeout: nil, content_type: "application/grpc+proto") ⇒ Object
Build gRPC request headers.
-
.build_path(service, method) ⇒ Object
Build gRPC path from service and method.
-
.extract_metadata(headers) ⇒ Object
Extract metadata from gRPC headers.
-
.format_timeout(timeout) ⇒ Object
deprecated
Deprecated.
Use Header::Timeout.format instead.
-
.parse_path(path) ⇒ Object
Parse service and method from gRPC path.
-
.parse_timeout(value) ⇒ Object
deprecated
Deprecated.
Use Header::Timeout#to_seconds instead.
Class Method Details
.build_headers(metadata: {}, timeout: nil, content_type: "application/grpc+proto") ⇒ Object
Build gRPC request headers.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/protocol/grpc/methods.rb', line 36 def self.build_headers(metadata: {}, timeout: nil, content_type: "application/grpc+proto") headers = Protocol::HTTP::Headers.new(policy: Protocol::GRPC::HEADER_POLICY) headers["content-type"] = content_type headers["te"] = "trailers" if timeout # Coerced to proper format by header policy: headers["grpc-timeout"] = timeout end .each do |key, value| # Binary headers end with -bin and are base64 encoded: headers[key] = if key.end_with?("-bin") Base64.strict_encode64(value) else value.to_s end end headers end |
.build_path(service, method) ⇒ Object
Build gRPC path from service and method.
19 20 21 |
# File 'lib/protocol/grpc/methods.rb', line 19 def self.build_path(service, method) "/#{service}/#{method}" end |
.extract_metadata(headers) ⇒ Object
Extract metadata from gRPC headers.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/protocol/grpc/methods.rb', line 61 def self.(headers) = {} headers.to_h.each do |key, value| # Skip reserved headers: next if key.start_with?("grpc-") || key == "content-type" || key == "te" # Decode binary headers: if key.end_with?("-bin") if value.is_a?(String) value = Base64.strict_decode64(value) elsif value.is_a?(Array) value = value.map{|item| Base64.strict_decode64(item)} end else value end [key] = value end end |
.format_timeout(timeout) ⇒ Object
Deprecated.
Use Header::Timeout.format instead.
Format timeout for grpc-timeout header.
89 90 91 92 93 |
# File 'lib/protocol/grpc/methods.rb', line 89 def self.format_timeout(timeout) Kernel.warn("`Protocol::GRPC::Methods.format_timeout` is deprecated; use `Protocol::GRPC::Header::Timeout.format` instead.", uplevel: 1, category: :deprecated) if $VERBOSE Header::Timeout.format(timeout) end |
.parse_path(path) ⇒ Object
Parse service and method from gRPC path.
26 27 28 29 |
# File 'lib/protocol/grpc/methods.rb', line 26 def self.parse_path(path) parts = path.split("/") [parts[1], parts[2]] end |
.parse_timeout(value) ⇒ Object
Deprecated.
Use Header::Timeout#to_seconds instead.
Parse grpc-timeout header value.
99 100 101 102 103 104 105 106 107 |
# File 'lib/protocol/grpc/methods.rb', line 99 def self.parse_timeout(value) Kernel.warn("`Protocol::GRPC::Methods.parse_timeout` is deprecated; use `Protocol::GRPC::Header::Timeout#to_seconds` instead.", uplevel: 1, category: :deprecated) if $VERBOSE return nil unless value Header::Timeout.parse(value).to_seconds rescue ArgumentError return nil end |