@@ -93,11 +93,49 @@ def each
9393 # Base service for all APIs. Not to be used directly.
9494 #
9595 class BaseService
96+ ##
97+ # A substitution string for the universe domain in an endpoint template
98+ # @return [String]
99+ #
100+ ENDPOINT_SUBSTITUTION = "$UNIVERSE_DOMAIN$" . freeze
101+
96102 include Logging
97103
104+ # Universe domain
105+ # @return [String]
106+ attr_reader :universe_domain
107+
108+ # Set the universe domain.
109+ # If the root URL was set with a universe domain substitution, it is
110+ # updated to reflect the new universe domain.
111+ #
112+ # @param new_ud [String,nil] The new universe domain, or nil to use the Google Default Universe
113+ def universe_domain = new_ud
114+ new_ud ||= ENV [ "GOOGLE_CLOUD_UNIVERSE_DOMAIN" ] || "googleapis.com"
115+ if @root_url_template
116+ @root_url = @root_url_template . gsub ENDPOINT_SUBSTITUTION , new_ud
117+ end
118+ @universe_domain = new_ud
119+ end
120+
98121 # Root URL (host/port) for the API
99122 # @return [Addressable::URI]
100- attr_accessor :root_url
123+ attr_reader :root_url
124+
125+ # Set the root URL.
126+ # If the given url includes a universe domain substitution, it is
127+ # resolved in the current universe domain
128+ #
129+ # @param url_or_template [String] The URL, which can include a universe domain substitution
130+ def root_url = url_or_template
131+ if url_or_template . include? ENDPOINT_SUBSTITUTION
132+ @root_url_template = url_or_template
133+ @root_url = url_or_template . gsub ENDPOINT_SUBSTITUTION , universe_domain
134+ else
135+ @root_url_template = nil
136+ @root_url = url_or_template
137+ end
138+ end
101139
102140 # Additional path prefix for all API methods
103141 # @return [Addressable::URI]
@@ -136,7 +174,9 @@ class BaseService
136174 # @param [String,Addressable::URI] base_path
137175 # Additional path prefix for all API methods
138176 # @api private
139- def initialize ( root_url , base_path , client_name : nil , client_version : nil )
177+ def initialize ( root_url , base_path , client_name : nil , client_version : nil , universe_domain : nil )
178+ @root_url_template = nil
179+ self . universe_domain = universe_domain
140180 self . root_url = root_url
141181 self . base_path = base_path
142182 self . client_name = client_name || 'google-api-ruby-client'
0 commit comments