lib/dapr/client.rb in dapr-0.1.25 vs lib/dapr/client.rb in dapr-0.2.2

- old
+ new

@@ -9,17 +9,19 @@ module Rubyists module Dapr # The namespace for the Dapr client module Client include SemanticLogger::Loggable - DAPR_PORT = ENV.fetch('DAPR_GRPC_PORT', '5001') + DAPR_PORT = ENV.fetch('DAPR_GRPC_PORT', nil) DAPR_URI = ENV.fetch('DAPR_GRPC_HOST', 'localhost') DAPR_STUB = ::Dapr::Proto::Runtime::V1::Dapr::Stub - def self.client - logger.info "Creating Dapr client for #{DAPR_URI}:#{DAPR_PORT}" - DAPR_STUB.new("#{DAPR_URI}:#{DAPR_PORT}", :this_channel_is_insecure) + def self.client(dapr_port: DAPR_PORT, dapr_uri: DAPR_URI) + return DummyClient.new if dapr_port.nil? + + logger.info "Creating Dapr client for #{dapr_uri}:#{dapr_port}" + DAPR_STUB.new("#{dapr_uri}:#{dapr_port}", :this_channel_is_insecure) end def self.singleton @singleton ||= client end @@ -28,9 +30,30 @@ Rubyists::Dapr::Client.client end def singleton @singleton ||= Rubyists::Dapr::Client.singleton + end + + # Make a dummy client that responds to every method with a warning and the called method signature + class DummyClient + include SemanticLogger::Loggable + + def initialize(*_) + logger.warn 'Dapr is not available (no DAPR_GRPC_PORT), using dummy client' + end + + def method_missing(method_name, *, &) + self.class.define_method(method_name) do |*args, &block| + logger.warn 'Dapr is not available (no DAPR_GRPC_PORT), using dummy client' + { method_name:, args:, block: } + end + send(method_name, *, &) + end + + def respond_to_missing?(_method_name, _include_private = false) + true + end end end end end