Beispiel #1
0
def set_subscription(sub_type, id):
    the_json = request.get_json()

    if not the_json:
        return error(ERROR_NO_INPUT_JSON_PROVIDED)

    # Validate and deserialize input
    try:
        sub_dict, _ = price_subscription_shema.load(the_json)
    except ValidationError as err:
        return error(ERROR_INVALID_INPUT_JSON, err.messages)

    # the update operation
    if id:
        sub_model = PriceSubscription.get(id=id, ignore=404)
        sub_dict['id'] = id
        if not sub_model:
            logger.warning('could not find subscription:{}'.format(id))
            return error(ERROR_SUBSCRIPTION_NOT_FOUND, id)
    else:
        # generate securityId
        sub_dict['securityId'] = get_security_id(sub_dict['securityType'],
                                                 sub_dict['exchange'],
                                                 sub_dict['code'])
        # generate subscription id
        sub_dict['id'] = "{}_{}".format(sub_dict['userId'],
                                        sub_dict['securityId'])

        sub_dict['timestamp'] = to_time_str(datetime.now(),
                                            time_fmt=TIME_FORMAT_MICRO)

        sub_model = PriceSubscription(meta={'id': sub_dict['id']})

    fill_doc_type(sub_model, sub_dict)

    sub_model.save(force=True)

    result_json = sub_model.to_dict(include_meta=True)

    logger.info('subscription:{} saved'.format(result_json))

    resp = kafka_producer.send('subscription',
                               bytes(json.dumps(sub_dict), encoding='utf8'),
                               key=bytes(sub_dict['id'], encoding='utf8'),
                               timestamp_ms=int(
                                   pd.Timestamp.now().timestamp() * 1000))
    kafka_producer.flush()

    logger.info(resp)

    return success(payload=result_json)
Beispiel #2
0
def get_subscription():
    user_id = request.args.get('userId')

    if not user_id:
        return error(ERROR_MISSING_REQUEST_PARAMS, 'user_id')

    result = esapi.es_get_subscription(user_id)
    return success(result)
Beispiel #3
0
def set_subscription(id):
    the_json = request.get_json()

    if not the_json:
        return error(ERROR_NO_INPUT_JSON_PROVIDED)

    # Validate and deserialize input
    try:
        sub_dict, _ = price_subscription_shema.load(the_json)
    except ValidationError as err:
        return error(ERROR_INVALID_INPUT_JSON, err.messages)

    # the update operation
    if id:
        # FIXME:just check whether exist?
        sub_model = PriceSubscription.get(id=id, ignore=404)
        if not sub_model:
            logger.warning('could not find subscription:{}'.format(id))
            return error(ERROR_SUBSCRIPTION_NOT_FOUND, id)
    else:
        sub_model = PriceSubscription()

    fill_doc_type(sub_model, the_json)

    sub_model.save(force=True)

    result_json = sub_model.to_dict(include_meta=True)

    logger.info('subscription:{} saved'.format(result_json))

    resp = kafka_producer.send('subscription',
                               bytes(json.dumps(result_json), encoding='utf8'),
                               key=bytes(result_json['_id'], encoding='utf8'),
                               timestamp_ms=int(
                                   pd.Timestamp.now().timestamp()))
    kafka_producer.flush()

    logger.info(resp)

    return success(payload=result_json)