DeribitEx.SessionContext (deribit_ex v0.1.0)
View SourceManages session context and order state preservation during Deribit token operations.
This module tracks the state of sessions across token changes and provides functionality for preserving order state during session transitions. It handles:
- Session tracking during token exchange (switching subaccounts)
- Session tracking during token forking (creating named sessions)
- Order state preservation across session transitions
- Automatic channel resubscription after token changes
Integrates with both token management and order management to ensure continuity of operations during authentication changes.
Summary
Functions
Generates a unique session ID.
Invalidates a session during logout operations.
Creates a new SessionContext from authentication response data.
Creates a new SessionContext from a token exchange response.
Creates a new SessionContext from a token fork response.
Updates an existing SessionContext with refreshed token data.
Types
@type t() :: %DeribitEx.SessionContext{ access_token: String.t(), active: boolean(), created_at: integer(), expires_at: integer(), id: String.t(), prev_id: String.t() | nil, refresh_token: String.t(), scope: String.t() | nil, session_name: String.t() | nil, subject_id: integer() | nil, transition: transition_type() }
Represents a Deribit session with tracking information.
id
: Unique ID for this sessionprev_id
: ID of the previous session (for tracking transitions)created_at
: When this session was createdaccess_token
: Current access token for this sessionrefresh_token
: Current refresh token for this sessionexpires_at
: When the current token will expiretransition
: How this session was createdsubject_id
: Account ID for this session (from exchange_token)session_name
: Session name (from fork_token)scope
: Authentication permissions for this sessionactive
: Whether this session is currently active
@type transition_type() :: :exchange | :fork | :refresh | :initial
The session transition type:
:exchange
- Token exchange for switching subaccounts:fork
- Token fork for creating named sessions:refresh
- Normal token refresh within same session:initial
- Initial session creation
Functions
@spec generate_session_id() :: String.t()
Generates a unique session ID.
Uses a combination of timestamp and random bytes for uniqueness.
Invalidates a session during logout operations.
Parameters
current_session
- The session to invalidate
Returns
{:ok, session}
- Invalidated session
Creates a new SessionContext from authentication response data.
Used when first authenticating to create the initial session context.
Parameters
auth_data
- Authentication response from Deribit containing access_token, etc.opts
- Additional options for session creation
Returns
{:ok, session}
- A new session context
Creates a new SessionContext from a token exchange response.
Used when switching between subaccounts to track the session transition.
Parameters
current_session
- The current active sessionexchange_data
- Response from exchange_token operationsubject_id
- The ID of the subaccount being switched to
Returns
{:ok, session}
- A new session context with transition tracking
Creates a new SessionContext from a token fork response.
Used when creating a named session to track the session transition.
Parameters
current_session
- The current active sessionfork_data
- Response from fork_token operationsession_name
- The name of the new session being created
Returns
{:ok, session}
- A new session context with transition tracking
Updates an existing SessionContext with refreshed token data.
Used during normal token refresh operations to maintain session continuity.
Parameters
current_session
- The current active sessionrefresh_data
- Response from token refresh operation
Returns
{:ok, session}
- Updated session context with new token information