diff --git a/google-cloud-ai_platform/lib/google/cloud/ai_platform.rb b/google-cloud-ai_platform/lib/google/cloud/ai_platform.rb index 294cfb834d86..2d49e3065a35 100644 --- a/google-cloud-ai_platform/lib/google/cloud/ai_platform.rb +++ b/google-cloud-ai_platform/lib/google/cloud/ai_platform.rb @@ -2012,6 +2012,76 @@ def self.schedule_service_available? version: :v1, transport: :grpc false end + ## + # Create a new client object for SessionService. + # + # By default, this returns an instance of + # [Google::Cloud::AIPlatform::V1::SessionService::Client](https://cloud.google.com/ruby/docs/reference/google-cloud-ai_platform-v1/latest/Google-Cloud-AIPlatform-V1-SessionService-Client) + # for a gRPC client for version V1 of the API. + # However, you can specify a different API version by passing it in the + # `version` parameter. If the SessionService service is + # supported by that API version, and the corresponding gem is available, the + # appropriate versioned client will be returned. + # You can also specify a different transport by passing `:rest` or `:grpc` in + # the `transport` parameter. + # + # Raises an exception if the currently installed versioned client gem for the + # given API version does not support the given transport of the SessionService service. + # You can determine whether the method will succeed by calling + # {Google::Cloud::AIPlatform.session_service_available?}. + # + # ## About SessionService + # + # The service that manages Vertex Session related resources. + # + # @param version [::String, ::Symbol] The API version to connect to. Optional. + # Defaults to `:v1`. + # @param transport [:grpc, :rest] The transport to use. Defaults to `:grpc`. + # @return [::Object] A client object for the specified version. + # + def self.session_service version: :v1, transport: :grpc, &block + require "google/cloud/ai_platform/#{version.to_s.downcase}" + + package_name = Google::Cloud::AIPlatform + .constants + .select { |sym| sym.to_s.downcase == version.to_s.downcase.tr("_", "") } + .first + service_module = Google::Cloud::AIPlatform.const_get(package_name).const_get(:SessionService) + service_module = service_module.const_get(:Rest) if transport == :rest + service_module.const_get(:Client).new(&block) + end + + ## + # Determines whether the SessionService service is supported by the current client. + # If true, you can retrieve a client object by calling {Google::Cloud::AIPlatform.session_service}. + # If false, that method will raise an exception. This could happen if the given + # API version does not exist or does not support the SessionService service, + # or if the versioned client gem needs an update to support the SessionService service. + # + # @param version [::String, ::Symbol] The API version to connect to. Optional. + # Defaults to `:v1`. + # @param transport [:grpc, :rest] The transport to use. Defaults to `:grpc`. + # @return [boolean] Whether the service is available. + # + def self.session_service_available? version: :v1, transport: :grpc + require "google/cloud/ai_platform/#{version.to_s.downcase}" + package_name = Google::Cloud::AIPlatform + .constants + .select { |sym| sym.to_s.downcase == version.to_s.downcase.tr("_", "") } + .first + return false unless package_name + service_module = Google::Cloud::AIPlatform.const_get package_name + return false unless service_module.const_defined? :SessionService + service_module = service_module.const_get :SessionService + if transport == :rest + return false unless service_module.const_defined? :Rest + service_module = service_module.const_get :Rest + end + service_module.const_defined? :Client + rescue ::LoadError + false + end + ## # Create a new client object for SpecialistPoolService. # diff --git a/google-cloud-ai_platform/test/google/cloud/ai_platform/client_test.rb b/google-cloud-ai_platform/test/google/cloud/ai_platform/client_test.rb index c5b7783ad1c8..e8bbb73789bc 100644 --- a/google-cloud-ai_platform/test/google/cloud/ai_platform/client_test.rb +++ b/google-cloud-ai_platform/test/google/cloud/ai_platform/client_test.rb @@ -629,6 +629,27 @@ def test_schedule_service_rest end end + def test_session_service_grpc + skip unless Google::Cloud::AIPlatform.session_service_available? transport: :grpc + Gapic::ServiceStub.stub :new, DummyStub.new do + grpc_channel = GRPC::Core::Channel.new "localhost:8888", nil, :this_channel_is_insecure + client = Google::Cloud::AIPlatform.session_service transport: :grpc do |config| + config.credentials = grpc_channel + end + assert_kind_of Google::Cloud::AIPlatform::V1::SessionService::Client, client + end + end + + def test_session_service_rest + skip unless Google::Cloud::AIPlatform.session_service_available? transport: :rest + Gapic::Rest::ClientStub.stub :new, DummyStub.new do + client = Google::Cloud::AIPlatform.session_service transport: :rest do |config| + config.credentials = :dummy_credentials + end + assert_kind_of Google::Cloud::AIPlatform::V1::SessionService::Rest::Client, client + end + end + def test_specialist_pool_service_grpc skip unless Google::Cloud::AIPlatform.specialist_pool_service_available? transport: :grpc Gapic::ServiceStub.stub :new, DummyStub.new do