Class: SDM::MethodInterceptor

Inherits:
Object
  • Object
show all
Defined in:
lib/interceptors.rb

Overview

MethodInterceptor provides a generic hook system for modifying requests and responses before/after gRPC calls

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ MethodInterceptor

Returns a new instance of MethodInterceptor.



25
26
27
28
29
# File 'lib/interceptors.rb', line 25

def initialize(client)
  @client = client
  @before_hooks = {}
  @after_hooks = {}
end

Instance Method Details

#after(method_name, &block) ⇒ Object

Register a hook to run after a method call method_name: "ServiceName.MethodName" block: receives (service_instance, request, response) and should return modified response



41
42
43
# File 'lib/interceptors.rb', line 41

def after(method_name, &block)
  @after_hooks[method_name] = block
end

#before(method_name, &block) ⇒ Object

Register a hook to run before a method call method_name: "ServiceName.MethodName" (e.g., "ManagedSecrets.Create") block: receives (service_instance, request) and should return modified request



34
35
36
# File 'lib/interceptors.rb', line 34

def before(method_name, &block)
  @before_hooks[method_name] = block
end

#execute_after(method_name, service_instance, request, response) ⇒ Object

Execute after hooks for a method



53
54
55
56
57
# File 'lib/interceptors.rb', line 53

def execute_after(method_name, service_instance, request, response)
  hook = @after_hooks[method_name]
  return response unless hook
  hook.call(service_instance, request, response)
end

#execute_before(method_name, service_instance, request) ⇒ Object

Execute before hooks for a method



46
47
48
49
50
# File 'lib/interceptors.rb', line 46

def execute_before(method_name, service_instance, request)
  hook = @before_hooks[method_name]
  return request unless hook
  hook.call(service_instance, request)
end