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
eventHandlerargument is supplied when it is constructed. ThenextEvent()method may not be called. All incoming events are delivered to theeventHandlersupplied on construction.If supplied,
eventHandlermust be a callable object that takes two arguments: receivedEventand related session.A Session is synchronous if an
eventHandlerargument is not supplied when it is constructed. ThenextEvent()method must be called to read incoming events.Several methods in Session take a
CorrelationIdparameter. The application may choose to supply its ownCorrelationIdvalues or allow the Session to create values. If the application supplies its ownCorrelationIdvalues it must manage their lifetime such that the same value is not reused for more than one operation at a time. The lifetime of aCorrelationIdbegins when it is supplied in a method invoked on a Session and ends either when it is explicitly cancelled usingcancel()orunsubscribe(), when aRESPONSEEvent(not aPARTIAL_RESPONSE) containing it is received or when aSUBSCRIPTION_STATUSEventwhich 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_STATUSEventgenerated by astartAsync()may be processed beforestartAsync()has returned (even thoughstartAsync()itself will not block).This becomes more significant when Session generated
CorrelationIds are in use. For example, if a call tosubscribe()which returns a Session generatedCorrelationIdhas not completed before the firstEvents which contain thatCorrelationIdarrive the application may not be able to interpret those events correctly. For this reason, it is preferable to use user generatedCorrelationIds 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
correlationIdor a list.- Parameters
correlationId (
Union[CorrelationId,Sequence[CorrelationId]]) – Correlation id(s) associated with the request(s) to cancel
For all specified
correlationIdvalues that identify an ongoing request cancel that request.Once this call returns the specified
correlationIdwill not be seen in any subsequentMessageobtained from aMessageIteratorby callingnext(). However, anyMessagecurrently pointed to by aMessageIteratorwhencancel()is called is not affected even if it has the specifiedcorrelationId. Also anyMessagewhere a reference has been retained by the application may still contain thecorrelationId. For these reasons, although technically an application is free to re-usecorrelationIdas 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
Identitywhich 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
Identitywith the specifiedauthOptionsandcorrelationId.- Parameters
- Returns
Identifies the aforementioned events and the generated
Identity.- Return type
If this is an asynchronous session then an
Eventmay be delivered to the registeredEventHandlerbeforegenerateAuthorizedIdentity()has returned.One or more
Event.AUTHORIZATION_STATUSevents, zero or moreEvent.TOKEN_STATUSevents, and zero or moreEvent.SERVICE_STATUSevents are generated.The behavior is undefined if either
authOptionsorcorrelationIdisNone.
- 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
SessionOptionsor the arguments to the function are invalid.
The
authIdandipAddressmust be provided together and can only be provided if the authentication mode isMANUAL.
- getAuthorizedIdentity(correlationId=None)
Returns the authorized
Identityassociated withcorrelationId. IfcorrelationIdis not given, returns the session identity.- Parameters
correlationId (
Optional[CorrelationId]) – Optional. Associated with anIdentity.- Returns
the
Identityassociated withcorrelationId.- Return type
- Raises
NotFoundException – If there is no
Identityassociated withcorrelationId, if the associatedIdentityis not authorized, or ifcorrelationIdis not given and the session identity is not authorized.
- getService(serviceName)
Return a
Serviceobject 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
serviceNameis not open already
The
serviceNamemust 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
Trueif the service is opened successfully,Falseotherwise.
Attempt to open the service identified by the specified
serviceNameand block until the service is either opened successfully or has failed to be opened. ReturnTrueif the service is opened successfully andFalseif the service cannot be successfully opened.The
serviceNamemust contain a fully qualified service name. That is, it must be of the form//<namespace>/<service-name>.Before
openService()returns aSERVICE_STATUSEventis generated. If this is an asynchronous Session then thisEventmay be processed by the registeredeventHandlerbeforeopenService()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
serviceNameand return immediately. The optional specifiedcorrelationIdis used to trackEvents generated as a result of this call.The
serviceNamemust 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_STATUSEventwhich 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
authorizationRequestand update the specifiedidentitywith the results. If the optionally specifiedcorrelationIdis supplied, it is used; otherwise create aCorrelationId. The actualCorrelationIdused is returned. If the optionally specifiedeventQueueis supplied allEvents relating to thisRequestwill 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_RESPONSEMessages followed by exactly oneRESPONSEMessage. Once the finalRESPONSEMessagehas been received the specifiedidentitywill have been updated to contain the users entitlement information and theCorrelationIdassociated with the request may be re-used. If the request fails at any stage aREQUEST_STATUSwill be generated, the specifiedidentitywill not be modified and theCorrelationIdmay be re-used.The
identitysupplied must have been returned from this Session’screateIdentity()method and must not be the session identity.