Module: Aws::AwsBaseInterface
- Included in:
- SqsInterface
- Defined in:
- lib/awsbase/awsbase.rb
Constant Summary collapse
- DEFAULT_SIGNATURE_VERSION =
'2'- @@caching =
false
Instance Attribute Summary collapse
-
#aws_access_key_id ⇒ Object
readonly
Current aws_access_key_id.
-
#cache ⇒ Object
readonly
Cache.
-
#connection ⇒ Object
readonly
HttpConnection instance.
-
#last_errors ⇒ Object
Last AWS errors list (used by AWSErrorHandler).
-
#last_request ⇒ Object
readonly
Last HTTP request object.
-
#last_request_id ⇒ Object
Returns Amazons request ID for the latest request.
-
#last_response ⇒ Object
readonly
Last HTTP response object.
-
#logger ⇒ Object
Logger object.
-
#params ⇒ Object
Initial params hash.
-
#signature_version ⇒ Object
readonly
Signature version (all services except s3).
Class Method Summary collapse
Instance Method Summary collapse
-
#cache_hits?(function, response, do_raise = :raise) ⇒ Boolean
Check if the aws function response hits the cache or not.
-
#caching? ⇒ Boolean
Returns
trueif the describe_xxx responses are being cached. -
#init(service_info, aws_access_key_id, aws_secret_access_key, params = {}) ⇒ Object
:nodoc:.
-
#multi_thread ⇒ Object
Return
trueif this instance works in multi_thread mode andfalseotherwise. -
#on_exception(options = {:raise=>true, :log=>true}) ⇒ Object
:nodoc:.
-
#request_cache_or_info(method, link, parser_class, benchblock, use_cache = true) ⇒ Object
:nodoc:.
-
#request_info_impl(connection, benchblock, request, parser, &block) ⇒ Object
:nodoc:.
- #signed_service_params(aws_secret_access_key, service_hash, http_verb = nil, host = nil, service = nil) ⇒ Object
- #update_cache(function, hash) ⇒ Object
Instance Attribute Details
#aws_access_key_id ⇒ Object (readonly)
Current aws_access_key_id
162 163 164 |
# File 'lib/awsbase/awsbase.rb', line 162 def aws_access_key_id @aws_access_key_id end |
#cache ⇒ Object (readonly)
Cache
178 179 180 |
# File 'lib/awsbase/awsbase.rb', line 178 def cache @cache end |
#connection ⇒ Object (readonly)
HttpConnection instance
176 177 178 |
# File 'lib/awsbase/awsbase.rb', line 176 def connection @connection end |
#last_errors ⇒ Object
Last AWS errors list (used by AWSErrorHandler)
168 169 170 |
# File 'lib/awsbase/awsbase.rb', line 168 def last_errors @last_errors end |
#last_request ⇒ Object (readonly)
Last HTTP request object
164 165 166 |
# File 'lib/awsbase/awsbase.rb', line 164 def last_request @last_request end |
#last_request_id ⇒ Object
Returns Amazons request ID for the latest request
170 171 172 |
# File 'lib/awsbase/awsbase.rb', line 170 def last_request_id @last_request_id end |
#last_response ⇒ Object (readonly)
Last HTTP response object
166 167 168 |
# File 'lib/awsbase/awsbase.rb', line 166 def last_response @last_response end |
#logger ⇒ Object
Logger object
172 173 174 |
# File 'lib/awsbase/awsbase.rb', line 172 def logger @logger end |
#params ⇒ Object
Initial params hash
174 175 176 |
# File 'lib/awsbase/awsbase.rb', line 174 def params @params end |
#signature_version ⇒ Object (readonly)
Signature version (all services except s3)
180 181 182 |
# File 'lib/awsbase/awsbase.rb', line 180 def signature_version @signature_version end |
Class Method Details
.caching ⇒ Object
154 155 156 |
# File 'lib/awsbase/awsbase.rb', line 154 def self.caching @@caching end |
.caching=(caching) ⇒ Object
157 158 159 |
# File 'lib/awsbase/awsbase.rb', line 157 def self.caching=(caching) @@caching = caching end |
Instance Method Details
#cache_hits?(function, response, do_raise = :raise) ⇒ Boolean
Check if the aws function response hits the cache or not. If the cache hits:
-
raises an
AwsNoChangeexception ifdo_raise==:raise. -
returnes parsed response from the cache if it exists or
trueotherwise.
If the cache miss or the caching is off then returns false.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/awsbase/awsbase.rb', line 236 def cache_hits?(function, response, do_raise=:raise) result = false if caching? function = function.to_sym # get rid of requestId (this bad boy was added for API 2008-08-08+ and it is uniq for every response) response = response.sub(%r{<requestId>.+?</requestId>}, '') response_md5 =Digest::MD5.hexdigest(response).to_s # check for changes unless @cache[function] && @cache[function][:response_md5] == response_md5 # well, the response is new, reset cache data update_cache(function, {:response_md5 => response_md5, :timestamp => Time.now, :hits => 0, :parsed => nil}) else # aha, cache hits, update the data and throw an exception if needed @cache[function][:hits] += 1 if do_raise == :raise raise(AwsNoChange, "Cache hit: #{function} response has not changed since "+ "#{@cache[function][:timestamp].strftime('%Y-%m-%d %H:%M:%S')}, "+ "hits: #{@cache[function][:hits]}.") else result = @cache[function][:parsed] || true end end end result end |
#caching? ⇒ Boolean
Returns true if the describe_xxx responses are being cached
227 228 229 |
# File 'lib/awsbase/awsbase.rb', line 227 def caching? @params.key?(:cache) ? @params[:cache] : @@caching end |
#init(service_info, aws_access_key_id, aws_secret_access_key, params = {}) ⇒ Object
:nodoc:
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/awsbase/awsbase.rb', line 182 def init(service_info, aws_access_key_id, aws_secret_access_key, params={}) #:nodoc: @params = params raise AwsError.new("AWS access keys are required to operate on #{service_info[:name]}") \ if aws_access_key_id.blank? || aws_secret_access_key.blank? @aws_access_key_id = aws_access_key_id @aws_secret_access_key = aws_secret_access_key # if the endpoint was explicitly defined - then use it if @params[:endpoint_url] @params[:server] = URI.parse(@params[:endpoint_url]).host @params[:port] = URI.parse(@params[:endpoint_url]).port @params[:service] = URI.parse(@params[:endpoint_url]).path @params[:protocol] = URI.parse(@params[:endpoint_url]).scheme @params[:region] = nil else @params[:server] ||= service_info[:default_host] @params[:server] = "#{@params[:region]}.#{@params[:server]}" if @params[:region] @params[:port] ||= service_info[:default_port] @params[:service] ||= service_info[:default_service] @params[:protocol] ||= service_info[:default_protocol] end if !@params[:multi_thread].nil? && @params[:connection_mode].nil? # user defined this @params[:connection_mode] = @params[:multi_thread] ? :per_thread : :single end # @params[:multi_thread] ||= defined?(AWS_DAEMON) @params[:connection_mode] ||= :default @params[:connection_mode] = :per_request if @params[:connection_mode] == :default @logger = @params[:logger] @logger = RAILS_DEFAULT_LOGGER if !@logger && defined?(RAILS_DEFAULT_LOGGER) @logger = Logger.new(STDOUT) if !@logger @logger.info "New #{self.class.name} using #{@params[:connection_mode].to_s}-connection mode" @error_handler = nil @cache = {} @signature_version = (params[:signature_version] || DEFAULT_SIGNATURE_VERSION).to_s end |
#multi_thread ⇒ Object
Return true if this instance works in multi_thread mode and false otherwise.
275 276 277 |
# File 'lib/awsbase/awsbase.rb', line 275 def multi_thread @params[:multi_thread] end |
#on_exception(options = {:raise=>true, :log=>true}) ⇒ Object
:nodoc:
269 270 271 272 |
# File 'lib/awsbase/awsbase.rb', line 269 def on_exception(={:raise=>true, :log=>true}) # :nodoc: raise if $!.is_a?(AwsNoChange) AwsError::on_aws_exception(self, ) end |
#request_cache_or_info(method, link, parser_class, benchblock, use_cache = true) ⇒ Object
:nodoc:
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/awsbase/awsbase.rb', line 349 def request_cache_or_info(method, link, parser_class, benchblock, use_cache=true) #:nodoc: # We do not want to break the logic of parsing hence will use a dummy parser to process all the standard # steps (errors checking etc). The dummy parser does nothig - just returns back the params it received. # If the caching is enabled and hit then throw AwsNoChange. # P.S. caching works for the whole images list only! (when the list param is blank) # check cache response, params = request_info(link, DummyParser.new) cache_hits?(method.to_sym, response.body) if use_cache parser = parser_class.new(:logger => @logger) benchblock.xml.add!{ parser.parse(response, params) } result = block_given? ? yield(parser) : parser.result # update parsed data update_cache(method.to_sym, :parsed => result) if use_cache result end |
#request_info_impl(connection, benchblock, request, parser, &block) ⇒ Object
:nodoc:
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/awsbase/awsbase.rb', line 279 def request_info_impl(connection, benchblock, request, parser, &block) #:nodoc: @connection = connection @last_request = request[:request] @last_response = nil response=nil blockexception = nil if(block != nil) # TRB 9/17/07 Careful - because we are passing in blocks, we get a situation where # an exception may get thrown in the block body (which is high-level # code either here or in the application) but gets caught in the # low-level code of HttpConnection. The solution is not to let any # exception escape the block that we pass to HttpConnection::request. # Exceptions can originate from code directly in the block, or from user # code called in the other block which is passed to response.read_body. benchblock.service.add! do responsehdr = @connection.request(request) do |response| ######### begin @last_response = response if response.is_a?(Net::HTTPSuccess) @error_handler = nil response.read_body(&block) else @error_handler = AWSErrorHandler.new(self, parser, :errors_list => self.class.amazon_problems) unless @error_handler check_result = @error_handler.check(request) if check_result @error_handler = nil return check_result end raise AwsError.new(@last_errors, @last_response.code, @last_request_id) end rescue Exception => e blockexception = e end end ######### #OK, now we are out of the block passed to the lower level if(blockexception) raise blockexception end benchblock.xml.add! do parser.parse(responsehdr) end return parser.result end else benchblock.service.add!{ response = @connection.request(request) } # check response for errors... @last_response = response if response.is_a?(Net::HTTPSuccess) @error_handler = nil benchblock.xml.add! { parser.parse(response) } return parser.result else @error_handler = AWSErrorHandler.new(self, parser, :errors_list => self.class.amazon_problems) unless @error_handler check_result = @error_handler.check(request) if check_result @error_handler = nil return check_result end raise AwsError.new(@last_errors, @last_response.code, @last_request_id) end end rescue @error_handler = nil raise end |
#signed_service_params(aws_secret_access_key, service_hash, http_verb = nil, host = nil, service = nil) ⇒ Object
217 218 219 220 221 222 223 224 |
# File 'lib/awsbase/awsbase.rb', line 217 def signed_service_params(aws_secret_access_key, service_hash, http_verb=nil, host=nil, service=nil ) case signature_version.to_s when '2' AwsUtils::sign_request_v2(aws_secret_access_key, service_hash, http_verb, host, service) else raise AwsError.new("Unknown signature version (#{signature_version.to_s}) requested") end end |
#update_cache(function, hash) ⇒ Object
265 266 267 |
# File 'lib/awsbase/awsbase.rb', line 265 def update_cache(function, hash) (@cache[function.to_sym] ||= {}).merge!(hash) if caching? end |