Skip to content

Commit

Permalink
feat(grafeas): Provide methods to determine whether services are avai…
Browse files Browse the repository at this point in the history
…lable with the currently installed versioned client (#28535)
  • Loading branch information
gcf-owl-bot[bot] authored Jan 25, 2025
1 parent 3db336d commit 5deacb9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions grafeas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ service such as [Google Cloud Run](https://cloud.google.com/run), this generally
results in logs appearing alongside your application logs in the
[Google Cloud Logging](https://cloud.google.com/logging/) service.

Debug logging also requires that the versioned clients for this service be
sufficiently recent, released after about Dec 10, 2024. If logging is not
working, try updating the versioned clients in your bundle or installed gems:
[grafeas-v1](https://cloud.google.com/ruby/docs/reference/grafeas-v1/latest).

## Supported Ruby Versions

This library is supported on Ruby 2.7+.
Expand Down
31 changes: 31 additions & 0 deletions grafeas/lib/grafeas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ module Grafeas
# supported by that API version, and the corresponding gem is available, the
# appropriate versioned client will be returned.
#
# Raises an exception if the currently installed versioned client gem for the
# given API version does not support the Grafeas service.
# You can determine whether the method will succeed by calling
# {Grafeas.grafeas_available?}.
#
# ## About Grafeas
#
# [Grafeas](https://grafeas.io) API.
Expand Down Expand Up @@ -67,6 +72,32 @@ def self.grafeas version: :v1, &block
service_module = Grafeas.const_get(package_name).const_get(:Grafeas)
service_module.const_get(:Client).new(&block)
end

##
# Determines whether the Grafeas service is supported by the current client.
# If true, you can retrieve a client object by calling {Grafeas.grafeas}.
# If false, that method will raise an exception. This could happen if the given
# API version does not exist or does not support the Grafeas service,
# or if the versioned client gem needs an update to support the Grafeas service.
#
# @param version [::String, ::Symbol] The API version to connect to. Optional.
# Defaults to `:v1`.
# @return [boolean] Whether the service is available.
#
def self.grafeas_available? version: :v1
require "grafeas/#{version.to_s.downcase}"
package_name = Grafeas
.constants
.select { |sym| sym.to_s.downcase == version.to_s.downcase.tr("_", "") }
.first
return false unless package_name
service_module = Grafeas.const_get package_name
return false unless service_module.const_defined? :Grafeas
service_module = service_module.const_get :Grafeas
service_module.const_defined? :Client
rescue ::LoadError
false
end
end

helper_path = ::File.join __dir__, "grafeas", "helpers.rb"
Expand Down
1 change: 1 addition & 0 deletions grafeas/test/grafeas/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def logger
end

def test_grafeas_grpc
skip unless Grafeas.grafeas_available?
Gapic::ServiceStub.stub :new, DummyStub.new do
grpc_channel = GRPC::Core::Channel.new "localhost:8888", nil, :this_channel_is_insecure
client = Grafeas.grafeas do |config|
Expand Down

0 comments on commit 5deacb9

Please sign in to comment.