class Hoodoo::Client::Endpoint::AutoSession

This endpoint wraps something which does actual communication but requires a session; it maintains a valid session for that wrapped endpoint automatically. It implements the following model:

Since it wraps another endpoint and requires Caller information to be able to build sessions, instantiation requirements are rather unusual

Public Instance Methods

create( body_hash, query_hash = nil ) click to toggle source

See Hoodoo::Client::Endpoint#create.

# File lib/hoodoo/client/endpoint/endpoints/auto_session.rb, line 94
def create( body_hash, query_hash = nil )
  return auto_retry( :create, body_hash, query_hash )
end
delete( ident, query_hash = nil ) click to toggle source

See Hoodoo::Client::Endpoint#delete.

# File lib/hoodoo/client/endpoint/endpoints/auto_session.rb, line 106
def delete( ident, query_hash = nil )
  return auto_retry( :delete, ident, query_hash )
end
list( query_hash = nil ) click to toggle source

See Hoodoo::Client::Endpoint#list.

# File lib/hoodoo/client/endpoint/endpoints/auto_session.rb, line 82
def list( query_hash = nil )
  return auto_retry( :list, query_hash )
end
show( ident, query_hash = nil ) click to toggle source

See Hoodoo::Client::Endpoint#show.

# File lib/hoodoo/client/endpoint/endpoints/auto_session.rb, line 88
def show( ident, query_hash = nil )
  return auto_retry( :show, ident, query_hash )
end
update( ident, body_hash, query_hash = nil ) click to toggle source

See Hoodoo::Client::Endpoint#update.

# File lib/hoodoo/client/endpoint/endpoints/auto_session.rb, line 100
def update( ident, body_hash, query_hash = nil )
  return auto_retry( :update, ident, body_hash, query_hash )
end

Protected Instance Methods

configure_with( resource, version, options ) click to toggle source

See Hoodoo::Client::Endpoint#configure_with.

Configuration option keys which must be supplied are:

caller_id

The UUID of the Caller instance to be used for session creation.

caller_secret

The authentication secret of the Caller instance to be used for session creation.

+session_endpoint

A Hooodo::Client::Endpoint subclass which can be used for talking to the Session endpoint (for obvious reasons!).

discovery_result

A Hoodoo::Services::Discovery::ForRemote instance describing the required, remotely available resource endpoint.

The pattern for creating and using this instance is:

  • Discover the location of the remote resource.

  • Use the discovery result to build an appropriate Endpoint subclass instance, e.g. Hoodoo::Client::Endpoint::HTTP.

  • Create a Hoodoo::Services::Discovery::ForRemote instance which describes the above endpoint via the wrapped_endpoint option.

  • Build an instance of this auto-session Endpoint subclass, giving it the above object as the discovery_result.

  • Use this endpoint in the normal fashion. All the special mechanics of session management are handled here.

# File lib/hoodoo/client/endpoint/endpoints/auto_session.rb, line 71
def configure_with( resource, version, options )
  @caller_id        = options[ :caller_id        ]
  @caller_secret    = options[ :caller_secret    ]
  @session_endpoint = options[ :session_endpoint ]
  @wrapped_endpoint = @discovery_result.wrapped_endpoint
end

Private Instance Methods

acquire_session_for( action ) click to toggle source

Acquire a session using the configured session endpoint. If this fails, the failure result is returned. If it seems to succeed but a session ID cannot be found, an internal 'generic.malformed' result is generated and returned.

The returned data uses an appropriate response class for the action at hand - an augmented array for lists, else an augmented hash. It can be returned directly up to the calling layer.

Returns :success if all goes well; session_id will be updated.

action

As given to auto_retry.

# File lib/hoodoo/client/endpoint/endpoints/auto_session.rb, line 167
def acquire_session_for( action )

  session_creation_result = @session_endpoint.create(
    'caller_id'             => @caller_id,
    'authentication_secret' => @caller_secret
  )

  if session_creation_result.platform_errors.has_errors?
    data = response_class_for( action ).new
    data.platform_errors.merge!( session_creation_result.platform_errors )
    return data
  end

  @session_endpoint.session_id = session_creation_result[ 'id' ]

  if @session_endpoint.session_id.nil? || @session_endpoint.session_id.empty?
    data = response_class_for( action ).new
    data.platform_errors.add_error(
      'generic.malformed',
      'message' => 'Received bad session description from Session endpoint despite "200" response code'
    )

    return data
  else
    @wrapped_endpoint.session_id = @session_endpoint.session_id
    return :success
  end
end
auto_retry( action, *args ) click to toggle source

Try to perform an action through the wrapped endpoint, acquiring a session first if need be or if necessary reacquiring a session and retrying the request.

action

The name of the method to call in the wrapped endpoint

  • see Hoodoo::Services::Middleware::ALLOWED_ACTIONS.

*args

Any other arguments to pass to action.

# File lib/hoodoo/client/endpoint/endpoints/auto_session.rb, line 121
def auto_retry( action, *args )

  copy_updated_options_to( @wrapped_endpoint )

  # We use the session endpoint as a session ID cache, in essence,
  # storing the acquired ID there and passing it into the wrapped
  # endpoint for the 'real' calls.

  if @session_endpoint.session_id.nil?
    session_creation_result = acquire_session_for( action )
    return session_creation_result unless session_creation_result.equal?( :success )
  else
    @wrapped_endpoint.session_id = @session_endpoint.session_id
  end

  result      = @wrapped_endpoint.send( action, *args )
  bad_session = result.platform_errors.errors.find do | error_hash |
                  error_hash[ 'code' ] == 'platform.invalid_session'
                end

  if bad_session.nil?
    return result
  else
    session_creation_result = acquire_session_for( action )

    if session_creation_result.equal?( :success )
      return @wrapped_endpoint.send( action, *args )
    else
      return session_creation_result
    end
  end
end