Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.7 kB
1
Indexable
Never
from flask import Flask, request, jsonify
from cassandra.cluster import Cluster
from cassandra.query import SimpleStatement

app = Flask(__name__)
cluster = Cluster(['127.0.0.1'])  # Replace with your Cassandra cluster IP address
session = cluster.connect('hobs_ods_staging')  # Replace with your Cassandra keyspace

@app.route('/data', methods=['GET'])
def get_data():
    # Extract path parameters
    event_date = request.args.get('hoscd_event_date')
    business_id = request.args.get('hoscd_businessid')
    status = request.args.get('hoscd_status')
    username = request.args.get('hoscd_username')
    
    # Build query based on provided path parameters
    if event_date:
        query = SimpleStatement("SELECT * FROM hos_cart_details WHERE hoscd_event_date=%s", consistency_level=2)
        result = session.execute(query, [event_date])
    elif business_id:
        query = SimpleStatement("SELECT * FROM hos_cart_details WHERE hoscd_businessid=%s", consistency_level=2)
        result = session.execute(query, [business_id])
    elif status:
        query = SimpleStatement("SELECT * FROM hos_cart_details WHERE hoscd_status=%s", consistency_level=2)
        result = session.execute(query, [status])
    elif username:
        query = SimpleStatement("SELECT * FROM hos_cart_details WHERE hoscd_username=%s", consistency_level=2)
        result = session.execute(query, [username])
    else:
        # Return 400 Bad Request error if no path parameters are provided
        return jsonify({'error': 'Please provide at least one path parameter'}), 400
    
    # Convert result to list of dictionaries and return as JSON response
    data = [dict(row) for row in result]
    return jsonify(data)