Untitled

 avatar
unknown
plain_text
2 years ago
3.1 kB
3
Indexable
from flask import Flask, jsonify, request
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
from cassandra.query import dict_factory

# create a connection to Cassandra
auth_provider = PlainTextAuthProvider(username='your_username', password='your_password')
cluster = Cluster(['172.16.177.58'], auth_provider=auth_provider)
session = cluster.connect('hobs_ods_staging')
session.row_factory = dict_factory

# create a Flask app
app = Flask(__name__)

# define the model class for the "hos_cart_details" table
class HosCartDetails:
    def __init__(self, event_date, business_id, key, buid, created_time, opid, status, cart_type, updated_time, username, value, value_type):
        self.event_date = event_date
        self.business_id = business_id
        self.key = key
        self.buid = buid
        self.created_time = created_time
        self.opid = opid
        self.status = status
        self.cart_type = cart_type
        self.updated_time = updated_time
        self.username = username
        self.value = value
        self.value_type = value_type

    @classmethod
    def find_by_event_date(cls, event_date):
        rows = session.execute("""
            SELECT * FROM hos_cart_details
            WHERE hoscd_event_date = %s
        """, (event_date,))
        return [cls(**row) for row in rows]

    @classmethod
    def find_by_business_id(cls, business_id):
        rows = session.execute("""
            SELECT * FROM hos_cart_details
            WHERE hoscd_businessid = %s
        """, (business_id,))
        return [cls(**row) for row in rows]

# define a route for fetching data by event date
@app.route('/hos_cart_details/eventdate/<string:event_date>')
def get_hos_cart_details_by_event_date(event_date):
    cart_details = HosCartDetails.find_by_event_date(event_date)
    return jsonify([{
        'event_date': cart.event_date,
        'business_id': cart.business_id,
        'key': cart.key,
        'buid': cart.buid,
        'created_time': cart.created_time,
        'opid': cart.opid,
        'status': cart.status,
        'cart_type': cart.cart_type,
        'updated_time': cart.updated_time,
        'username': cart.username,
        'value': cart.value,
        'value_type': cart.value_type
    } for cart in cart_details])

# define a route for fetching data by business ID
@app.route('/hos_cart_details/businessid/<string:business_id>')
def get_hos_cart_details_by_business_id(business_id):
    cart_details = HosCartDetails.find_by_business_id(business_id)
    return jsonify([{
        'event_date': cart.event_date,
        'business_id': cart.business_id,
        'key': cart.key,
        'buid': cart.buid,
        'created_time': cart.created_time,
        'opid': cart.opid,
        'status': cart.status,
        'cart_type': cart.cart_type,
        'updated_time': cart.updated_time,
        'username': cart.username,
        'value': cart.value,
        'value_type': cart.value_type
    } for cart in cart_details])

if __name__ == '__main__':
    app.run()