Esempio n. 1
0
def index():
    try:
        if app.sessionForRequests is None:
            app.sessionForRequests = openBloombergSession()
        if app.sessionForSubscriptions is None:
            app.sessionForSubscriptions = openBloombergSession()
            app.allSubscriptions = {}
    except Exception as e:
        handleBrokenSession(app, e)
        traceback.print_exc()
        return respond500(e)
    try:
        securities = request.values.getlist('security') or []
        fields = request.values.getlist('field') or []
    except Exception as e:
        traceback.print_exc()
        return respond400(e)

    try:
        payload = json.dumps(
            requestLatest(app.sessionForRequests, securities,
                          fields)).encode()
    except Exception as e:
        handleBrokenSession(app, e)
        traceback.print_exc()
        return respond500(e)

    response = Response(payload, status=200, mimetype='application/json')
    response.headers['Access-Control-Allow-Origin'] = allowCORS(
        request.headers.get('Origin'))
    return response
Esempio n. 2
0
def index():
    try:
        if app.sessionForRequests is None:
            app.sessionForRequests = openBloombergSession()
        if app.sessionForSubscriptions is None:
            app.sessionForSubscriptions = openBloombergSession()
            app.allSubscriptions = {}
    except Exception as e:
        handleBrokenSession(app, e)
        traceback.print_exc()
        return respond500(e)
    try:
        securities = request.args.getlist('security') or []
        eventTypes = request.args.getlist('eventType') or []
        startDateTime = dateutil.parser.parse(
            request.args.get('startDateTime'))
        endDateTime = dateutil.parser.parse(request.args.get('endDateTime'))
    except Exception as e:
        traceback.print_exc()
        return respond400(e)

    try:
        payload = json.dumps(
            requestIntraday(app.sessionForRequests, securities, eventTypes,
                            startDateTime, endDateTime)).encode()
    except Exception as e:
        handleBrokenSession(app, e)
        traceback.print_exc()
        return respond500(e)

    response = Response(payload, status=200, mimetype='application/json')
    response.headers['Access-Control-Allow-Origin'] = allowCORS(
        request.headers.get('Origin'))
    return response
Esempio n. 3
0
def index():
    try:
        if app.sessionForRequests is None:
            app.sessionForRequests = openBloombergSession()
        if app.sessionForSubscriptions is None:
            app.sessionForSubscriptions = openBloombergSession()
            app.allSubscriptions = {}
    except Exception as e:
        handleBrokenSession(app, e)
        traceback.print_exc()
        return respond500(e)
    try:
        securities = request.values.getlist('security') or []
        fields = request.values.getlist('field') or []
        startDate = request.values.get('startDate')
        endDate = request.values.get('endDate')
    except Exception as e:
        traceback.print_exc()
        return respond400(e)

    etag = generateEtag({
        "securities": securities,
        "fields": fields,
        "startDate": startDate,
        "endDate": endDate
    })
    if request.headers.get('If-None-Match') == etag:
        response = Response("", status=304, mimetype='application/json')
        response.headers['Access-Control-Allow-Origin'] = allowCORS(
            request.headers.get('Origin'))
        return response
    try:
        payload = json.dumps(
            requestHistorical(app.sessionForRequests, securities, fields,
                              startDate, endDate)).encode()
    except Exception as e:
        handleBrokenSession(app, e)
        traceback.print_exc()
        return respond500(e)

    response = Response(payload, status=200, mimetype='application/json')
    response.headers['Etag'] = etag
    response.headers['Cache-Control'] = "max-age=86400, must-revalidate"
    response.headers['Vary'] = "Origin"
    response.headers['Access-Control-Allow-Origin'] = allowCORS(
        request.headers.get('Origin'))
    return response
Esempio n. 4
0
def unsubscribeAll():
    try:
        if app.sessionForSubscriptions is None:
            app.sessionForSubscriptions = openBloombergSession()
            app.allSubscriptions = {}
    except Exception as e:
        handleBrokenSession(app, e)
        traceback.print_exc()
        return respond500(e)

    return doUnsubscribe(list(app.allSubscriptions.keys()))
Esempio n. 5
0
def main(port=6659):
    wireUpBlpapiImplementation(blpapi)

    server = None
    try:
        try:
            app.sessionForRequests = openBloombergSession()
            app.sessionForSubscriptions = openBloombergSession()
            app.allSubscriptions = {}
        except:
            traceback.print_exc()
        socketio.start_background_task(
            lambda: handleSubscriptions(app, socketio))
        socketio.run(app, port=port)
    except KeyboardInterrupt:
        print("Ctrl+C received, exiting...")
    finally:
        if app.sessionForRequests is not None:
            app.sessionForRequests.stop()
        if app.sessionForSubscriptions is not None:
            app.sessionForSubscriptions.stop()
        if server is not None:
            server.socket.close()
Esempio n. 6
0
def handleSubscriptions(app, socketio):
    eventHandler = SubscriptionEventHandler(app, socketio)
    while True:
        try:
            if app.sessionForSubscriptions is None:
                app.sessionForSubscriptions = openBloombergSession()
                app.allSubscriptions = {}

            event = app.sessionForSubscriptions.nextEvent(500)
            eventHandler.processEvent(event, app.sessionForSubscriptions)
        except Exception as e:
            traceback.print_exc()
            handleBrokenSession(app, e)
            socketio.sleep(1)
        finally:
            socketio.sleep()
Esempio n. 7
0
def unsubscribe():
    try:
        if app.sessionForSubscriptions is None:
            app.sessionForSubscriptions = openBloombergSession()
            app.allSubscriptions = {}
    except Exception as e:
        handleBrokenSession(app, e)
        traceback.print_exc()
        return respond500(e)

    try:
        securities = request.values.getlist('security') or []
    except Exception as e:
        traceback.print_exc()
        return respond400(e)

    return doUnsubscribe(securities)
Esempio n. 8
0
def index():
    try:
        if app.sessionForSubscriptions is None:
            app.sessionForSubscriptions = openBloombergSession()
            app.allSubscriptions = {}
    except Exception as e:
        handleBrokenSession(app, e)
        traceback.print_exc()
        return respond500(e)
    try:
        securities = request.values.getlist('security') or []
        fields = request.values.getlist('field') or []
        interval = request.values.get('interval') or "2.0"
    except Exception as e:
        traceback.print_exc()
        return respond400(e)

    try:
        _, sessionRestarted = openBloombergService(app.sessionForSubscriptions,
                                                   "//blp/mktdata")
        if sessionRestarted:
            app.allSubscriptions = {}
        subscriptionList = blpapi.SubscriptionList()
        resubscriptionList = blpapi.SubscriptionList()
        for security in securities:
            correlationId = blpapi.CorrelationId(sys.intern(security))
            if not security in app.allSubscriptions:
                app.allSubscriptions[security] = list(fields)
                subscriptionList.add(security, app.allSubscriptions[security],
                                     "interval=" + interval, correlationId)
                recordBloombergHits("subscribe", len(fields))
            else:
                app.allSubscriptions[security] += fields
                app.allSubscriptions[security] = list(
                    set(app.allSubscriptions[security]))
                resubscriptionList.add(security,
                                       app.allSubscriptions[security],
                                       "interval=" + interval, correlationId)
                recordBloombergHits("resubscribe", len(fields))

        if subscriptionList.size() != 0:
            app.sessionForSubscriptions.subscribe(subscriptionList)

        if resubscriptionList.size() != 0:
            try:
                app.sessionForSubscriptions.resubscribe(resubscriptionList)
            except Exception as e:
                traceback.print_exc()
                recordBloombergHits("unsubscribe",
                                    resubscriptionList.size() * 3)
                app.sessionForSubscriptions.unsubscribe(resubscriptionList)
                recordBloombergHits("subscribe", resubscriptionList.size() * 3)
                app.sessionForSubscriptions.subscribe(resubscriptionList)
    except Exception as e:
        handleBrokenSession(app, e)
        traceback.print_exc()
        return respond500(e)

    response = Response(json.dumps({
        "message": "OK"
    }).encode(),
                        status=202,
                        mimetype='application/json')
    response.headers['Access-Control-Allow-Origin'] = allowCORS(
        request.headers.get('Origin'))
    return response