Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.2 kB
1
Indexable
Never
from flask import Flask, jsonify
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model
from cassandra.cqlengine.management import sync_table

# Define the Cassandra connection properties
cass_host = '<cassandra_host>'
cass_port = '<cassandra_port>'
cass_username = '<cassandra_username>'
cass_password = '<cassandra_password>'
cass_keyspace = 'hobs_ods_staging'

# Create the Flask app
app = Flask(__name__)

# Define the Cassandra connection and session objects
auth_provider = PlainTextAuthProvider(username=cass_username, password=cass_password)
cluster = Cluster([cass_host], port=cass_port, auth_provider=auth_provider)
session = cluster.connect(cass_keyspace)

# Define the model class for the hos_cart_details table
class HosCartDetails(Model):
    hoscd_event_date = columns.Date(primary_key=True, partition_key=True)
    hoscd_businessid = columns.Text(primary_key=True, partition_key=True)
    hoscd_key = columns.Text(primary_key=True)
    hoscd_buid = columns.Text()
    hoscd_createdtime = columns.DateTime()
    hoscd_opid = columns.Text()
    hoscd_status = columns.Text()
    hoscd_type = columns.Text()
    hoscd_updatedtime = columns.DateTime()
    hoscd_username = columns.Text()
    hoscd_value = columns.Blob()
    hoscd_valuetype = columns.Text()

# Sync the HosCartDetails model with the Cassandra keyspace
sync_table(HosCartDetails)

# Define the API endpoint for fetching data
@app.route('/<businessid>/<status>/<hoscd_event_date>', methods=['GET'])
def get_data(businessid, status, hoscd_event_date):
    # Query the hos_cart_details table based on the provided parameters
    query = HosCartDetails.objects.filter(
        HosCartDetails.hoscd_businessid == businessid,
        HosCartDetails.hoscd_status == status,
        HosCartDetails.hoscd_event_date == hoscd_event_date
    ).allow_filtering()

    # Extract the username data from the query results
    usernames = [row.hoscd_username for row in query]

    # Return the usernames as a JSON response
    return jsonify({'usernames': usernames})

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