blpapi.AbstractSession
- class blpapi.AbstractSession(handle=None, dtor=None)
A common interface shared between publish and consumer sessions.
This class provides an abstract session which defines shared interface between publish and consumer requests for Bloomberg.
Sessions manage access to services either by requests and responses or subscriptions. A Session can dispatch events and replies in either a synchronous or asynchronous mode. The mode of a Session is determined when it is constructed and cannot be changed subsequently.
A Session is asynchronous if an
eventHandler
argument is supplied when it is constructed. ThenextEvent()
method may not be called. All incoming events are delivered to theeventHandler
supplied on construction.If supplied,
eventHandler
must be a callable object that takes two arguments: receivedEvent
and related session.A Session is synchronous if an
eventHandler
argument is not supplied when it is constructed. ThenextEvent()
method must be called to read incoming events.Several methods in Session take a
CorrelationId
parameter. The application may choose to supply its ownCorrelationId
values or allow the Session to create values. If the application supplies its ownCorrelationId
values it must manage their lifetime such that the same value is not reused for more than one operation at a time. The lifetime of aCorrelationId
begins when it is supplied in a method invoked on a Session and ends either when it is explicitly cancelled usingcancel()
orunsubscribe()
, when aRESPONSE
Event
(not aPARTIAL_RESPONSE
) containing it is received or when aSUBSCRIPTION_STATUS
Event
which indicates that the subscription it refers to has been terminated is received.When using an asynchronous Session, the application must be aware that because the callbacks are generated from another thread, they may be processed before the call which generates them has returned. For example, the
SESSION_STATUS
Event
generated by astartAsync()
may be processed beforestartAsync()
has returned (even thoughstartAsync()
itself will not block).This becomes more significant when Session generated
CorrelationId
s are in use. For example, if a call tosubscribe()
which returns a Session generatedCorrelationId
has not completed before the firstEvent
s which contain thatCorrelationId
arrive the application may not be able to interpret those events correctly. For this reason, it is preferable to use user generatedCorrelationId
s when using asynchronous Sessions. This issue does not arise when using a synchronous Session as long as the calls tosubscribe()
etc are made on the same thread as the calls tonextEvent()
.- cancel(correlationId)
Cancel request(s) with a single
correlationId
or a list.- Parameters
correlationId (
Union
[CorrelationId
,Sequence
[CorrelationId
]]) – Correlation id(s) associated with the request(s) to cancel
For all specified
correlationId
values that identify an ongoing request cancel that request.Once this call returns the specified
correlationId
will not be seen in any subsequentMessage
obtained from aMessageIterator
by callingnext()
. However, anyMessage
currently pointed to by aMessageIterator
whencancel()
is called is not affected even if it has the specifiedcorrelationId
. Also anyMessage
where a reference has been retained by the application may still contain thecorrelationId
. For these reasons, although technically an application is free to re-usecorrelationId
as soon as this method returns it is preferable not to aggressively re-use correlation IDs, particularly with an asynchronous Session.- Return type
- createIdentity()
Create an
Identity
which is valid but has not been authorized.- Returns
Identity which is valid but has not been authorized
- Return type
- generateAuthorizedIdentity(authOptions, correlationId=None)
Generates an authorized
Identity
with the specifiedauthOptions
andcorrelationId
.- Parameters
- Returns
Identifies the aforementioned events and the generated
Identity
.- Return type
If this is an asynchronous session then an
Event
may be delivered to the registeredEventHandler
beforegenerateAuthorizedIdentity()
has returned.One or more
Event.AUTHORIZATION_STATUS
events, zero or moreEvent.TOKEN_STATUS
events, and zero or moreEvent.SERVICE_STATUS
events are generated.The behavior is undefined if either
authOptions
orcorrelationId
isNone
.
- generateToken(correlationId=None, eventQueue=None, authId=None, ipAddress=None)
Generate a token to be used for authorization.
- Parameters
correlationId – Correlation id to be associated with the request
eventQueue – Event queue on which to receive Events related to this request
authId – The id used for authentication
ipAddress – IP of the machine used for authentication
- Returns
The correlation id used to identify the Events generated as a result of this call
- Return type
- Raises
InvalidArgumentException – If the authentication options in
SessionOptions
or the arguments to the function are invalid.
The
authId
andipAddress
must be provided together and can only be provided if the authentication mode isMANUAL
.
- getAuthorizedIdentity(correlationId=None)
Returns the authorized
Identity
associated withcorrelationId
. IfcorrelationId
is not given, returns the session identity.- Parameters
correlationId (
Optional
[CorrelationId
]) – Optional. Associated with anIdentity
.- Returns
the
Identity
associated withcorrelationId
.- Return type
- Raises
NotFoundException – If there is no
Identity
associated withcorrelationId
, if the associatedIdentity
is not authorized, or ifcorrelationId
is not given and the session identity is not authorized.
- getService(serviceName)
Return a
Service
object representing the service.- Parameters
serviceName (
str
) – Name of the service to retrieve- Returns
Service identified by the service name
- Return type
- Raises
InvalidStateException – If the service identified by the specified
serviceName
is not open already
The
serviceName
must contain a fully qualified service name. That is, it must be of the form//<namespace>/<service-name>
.
- openService(serviceName)
Open the service identified by the specified
serviceName
.- Parameters
serviceName (
str
) – Name of the service- Return type
- Returns
True
if the service is opened successfully,False
otherwise.
Attempt to open the service identified by the specified
serviceName
and block until the service is either opened successfully or has failed to be opened. ReturnTrue
if the service is opened successfully andFalse
if the service cannot be successfully opened.The
serviceName
must contain a fully qualified service name. That is, it must be of the form//<namespace>/<service-name>
.Before
openService()
returns aSERVICE_STATUS
Event
is generated. If this is an asynchronous Session then thisEvent
may be processed by the registeredeventHandler
beforeopenService()
has returned.
- openServiceAsync(serviceName, correlationId=None)
Begin the process to open the service and return immediately.
- Parameters
serviceName (
str
) – Name of the servicecorrelationId (
Optional
[CorrelationId
]) – Correlation id to associate with events generated as a result of this call
- Returns
The correlation id used to identify the Events generated as a result of this call
- Return type
Begin the process to open the service identified by the specified
serviceName
and return immediately. The optional specifiedcorrelationId
is used to trackEvent
s generated as a result of this call.The
serviceName
must contain a fully qualified service name. That is, it must be of the form//<namespace>/<service-name>
.The application must monitor events for a
SERVICE_STATUS
Event
which will be generated once the service has been successfully opened or the opening has failed.
- sendAuthorizationRequest(request, identity, correlationId=None, eventQueue=None)
Send the specified
authorizationRequest
.- Parameters
request – Authorization request to send
identity – Identity to update with the results
correlationId – Correlation id to associate with the request
eventQueue – Event queue on which the events related to this request will arrive
- Returns
The correlation id used to identify the Events generated as a result of this call
- Return type
Send the specified
authorizationRequest
and update the specifiedidentity
with the results. If the optionally specifiedcorrelationId
is supplied, it is used; otherwise create aCorrelationId
. The actualCorrelationId
used is returned. If the optionally specifiedeventQueue
is supplied allEvent
s relating to thisRequest
will arrive on thatEventQueue
.The underlying user information must remain valid until the Request has completed successfully or failed.
A successful request will generate zero or more
PARTIAL_RESPONSE
Message
s followed by exactly oneRESPONSE
Message
. Once the finalRESPONSE
Message
has been received the specifiedidentity
will have been updated to contain the users entitlement information and theCorrelationId
associated with the request may be re-used. If the request fails at any stage aREQUEST_STATUS
will be generated, the specifiedidentity
will not be modified and theCorrelationId
may be re-used.The
identity
supplied must have been returned from this Session’screateIdentity()
method and must not be the session identity.