Example #1
0
def lambda_handler(event, context):

    # If the environment advises on a specific debug level, set it accordingly.
    aux.update_log_level(LOGGER, event, context)
    # Log environment details.
    aux.log_env_details(LOGGER)
    # Log request details.
    aux.log_event_and_context(LOGGER, event, context)
    # Publish Lambda event to the respective event logging topic.
    aux_api.publish_apigw_lambda_event(LOGGER, event)

    # Create a new completed ride object from the incoming event.
    try:
        LOGGER.debug(
            "Create a new completed ride object from the incoming event.")
        completed_ride = CompletedRide(LOGGER, event, event[aux.EK_BODY])
    except Exception as ex:
        LOGGER.exception(aux_api.BAD_REQUEST_NO_JSON_BODY)
        LOGGER.exception(ex)
        return aux_api.bad_request(LOGGER, event,
                                   aux_api.BAD_REQUEST_NO_JSON_BODY, ex)

    # Persist ride details.
    # Feature request: Eventually respond with Internal Server Error if this fails.
    completed_ride.persist_ride_details()

    # Send ride details to the ride completion topic.
    # Feature request: If this fails, add a scheduled process to retry.
    publish_ride_details(completed_ride)

    # Prepare self link for the new completed ride resource.
    # Feature request: Make this an instance operation of a CompletedRide instance?
    completed_ride_link = create_self_link_url(event, completed_ride)

    data = {
        "links": {
            "self": completed_ride_link
        },
        "unicorn-id": completed_ride.get_unicorn_id(),
        "customer-id": completed_ride.get_customer_id(),
        "submitted-at": completed_ride.get_submitted_at(),
        "ride-details": completed_ride.get_ride_details()
    }

    return {
        "statusCode": 201,
        "body": json.dumps(data),
        "headers": {
            "Location": completed_ride_link,
            "Content-Location": completed_ride_link,
            "Content-Type": "application/json"
        }
    }
Example #2
0
def lambda_handler(event, context):

    # If the environment advises on a specific debug level, set it accordingly.
    aux.update_log_level(LOGGER, event, context)
    # Log environment details.
    aux.log_env_details(LOGGER)
    # Log request details.
    aux.log_event_and_context(LOGGER, event, context)
    # Publish Lambda event to the respective event logging topic.
    aux_processing.publish_sns_lambda_event(LOGGER, event)

    # We expect SNS messages coming in in a "Records" array, within each record, there's an object "Sns".
    # Within that object:
    # - The message body is in the "Message" object.
    # - Message meta data is in the "MessageAttributes" object.
    count = 0
    for record in event["Records"]:
        count += 1
        LOGGER.debug("Looking into record #%d:", count)

        # Extract ride details from record.
        ride_details = json.loads(record["Sns"]["Message"])
        LOGGER.debug("ride_details: %s", ride_details)
        # Extract unicorn ID from ride details.
        unicorn_id = ride_details["unicorn-id"]
        LOGGER.debug("unicorn_id: %s", unicorn_id)
        # Extract customer ID from ride details.
        customer_id = ride_details["customer-id"]
        LOGGER.debug("customer_id: %s", customer_id)
        # Extract submitted-at from ride details.
        submitted_at = ride_details["submitted-at"]
        LOGGER.debug("submitted_at: %s", submitted_at)
        # Extract ride ID from ride details.
        ride_id = ride_details["ride-id"]
        LOGGER.debug("ride_id: %s", ride_id)
        # Extract fare from ride details.
        fare = ride_details["fare"]
        LOGGER.debug("fare: %s", fare)
        # Extract distance from ride details.
        distance = ride_details["distance"]
        LOGGER.debug("distance: %s", distance)
        # Extract correlation ID from ride details.
        correlation_id = ride_details["correlation-id"]
        LOGGER.debug("correlation_id: %s", correlation_id)

        # Persist ride details.
        persist_ride_details(unicorn_id, customer_id, submitted_at, ride_id, fare, distance, correlation_id, ride_details)
def lambda_handler(event, context):

    # If the environment advises on a specific debug level, set it accordingly.
    aux.update_log_level(LOGGER, event, context)
    # Log environment details.
    aux.log_env_details(LOGGER)
    # Log request details.
    aux.log_event_and_context(LOGGER, event, context)
    # Publish Lambda event to the respective event logging topic.
    aux_processing.publish_sns_lambda_event(LOGGER, event)

    # Retrieve unicorn ID from environment.
    unicorn_id = retrieve_unicorn_id()

    # We expect either SNS or SQS messages coming in - both will appear within an array called "Records".
    # Within each record, SNS data appears in an object calles "Sns". Within that object:
    # - The message body is in the "Message" object.
    # - Message meta data is in the "MessageAttributes" object.
    # In case we receive an SQS message here, it will be from topic-queue-chaining.
    # All relevant data from the SNS message is going to be stuffed into the "body" object of a record.
    # Within that "body" object, we will find the "Message" and "MessageAttributes" objects again.
    count = 0
    for record in event["Records"]:
        count += 1
        LOGGER.debug("Looking into record #%d:", count)

        # Extract RFQ details and message meta data.
        if "Sns" in record:
            # Direct message reception from SNS.
            LOGGER.debug("Direct message reception from SNS.")
            rfq_details = json.loads(record["Sns"]["Message"])
            message_attributes = record["Sns"]["MessageAttributes"]
        else:
            # Indirect message reception via buffering SQS queue due to topic-queue-chaining.
            LOGGER.debug("Indirect message reception via buffering SQS queue due to topic-queue-chaining.")
            body = json.loads(record["body"])
            rfq_details = json.loads(body["Message"])
            LOGGER.debug("FIXME: This branch is only prepared, need to add to extract message attributes from the SQS message.")
        LOGGER.debug("rfq_details: %s", rfq_details)
        LOGGER.debug("message_attributes: %s", message_attributes)

        # Extract correlation ID from message meta data.
        correlation_id = extract_correlation_id(message_attributes)
        LOGGER.debug("correlation_id: %s", correlation_id)
        # Extract customer ID from RFQ.
        customer_id = rfq_details["customer-id"]
        LOGGER.debug("customer_id: %s", customer_id)
        # Extract return address from message meta data.
        return_address = extract_return_address(message_attributes)
        LOGGER.debug("return_address: %s", return_address)
        
        # Calculate the fare for the offer.
        offered_fare = calculate_offered_fare(unicorn_id)
        # Calculate goodies for the offer.
        offered_goodies = ride_goodies.calculate_offered_goodies(LOGGER, unicorn_id)

        # Create a random RFQ response.
        rfq_response = {
            "unicorn-id": unicorn_id,
            "customer-id": customer_id,
            # "price": 2.95,
            # "goodies": [ "FREE_DRINKS_NON_ALC", "FREE_DRINKS_ALC" ]
            "price": offered_fare,
            "goodies": list(offered_goodies)
        }        
        LOGGER.debug("rfq_response: %s", rfq_response)

        # Send RFQ response to RFQ response queue.
        send_rfq_response(return_address, correlation_id, unicorn_id, rfq_response)
def lambda_handler(event, context):

    # If the environment advises on a specific debug level, set it accordingly.
    aux.update_log_level(LOGGER, event, context)
    # Log environment details.
    aux.log_env_details(LOGGER)
    # Log request details.
    aux.log_event_and_context(LOGGER, event, context)
    # Publish Lambda event to the respective event logging topic.
    aux_processing.publish_sns_lambda_event(LOGGER, event)

    # We expect either SNS or SQS messages coming in - both will appear within an array called "Records".
    # Within each record, SNS data appears in an object calles "Sns". Within that object:
    # - The message body is in the "Message" object.
    # - Message meta data is in the "MessageAttributes" object.
    # In case we receive an SQS message here, it will be from topic-queue-chaining.
    # All relevant data from the SNS message is going to be stuffed into the "body" object of a record.
    # Within that "body" object, we will find the "Message" and "MessageAttributes" objects again.
    count = 0
    for record in event["Records"]:
        count += 1
        LOGGER.debug("Looking into record #%d:", count)

        # Extract ride details from record.
        ride_details = {}
        if "Sns" in record:
            # Direct message reception from SNS.
            LOGGER.debug("Direct message reception from SNS.")
            ride_details = json.loads(record["Sns"]["Message"])
        else:
            # Indirect message reception via buffering SQS queue due to topic-queue-chaining.
            LOGGER.debug(
                "Indirect message reception via buffering SQS queue due to topic-queue-chaining."
            )
            body = json.loads(record["body"])
            ride_details = json.loads(body["Message"])
        LOGGER.debug("ride_details: %s", ride_details)

        # Extract unicorn ID from ride details.
        unicorn_id = ride_details["unicorn-id"]
        LOGGER.debug("unicorn_id: %s", unicorn_id)
        # Extract customer ID from ride details.
        customer_id = ride_details["customer-id"]
        LOGGER.debug("customer_id: %s", customer_id)
        # Extract submitted-at from ride details.
        submitted_at = ride_details["submitted-at"]
        LOGGER.debug("submitted_at: %s", submitted_at)
        # Extract ride ID from ride details.
        ride_id = ride_details["ride-id"]
        LOGGER.debug("ride_id: %s", ride_id)
        # Extract fare from ride details.
        fare = ride_details["fare"]
        LOGGER.debug("fare: %s", fare)
        # Extract distance from ride details.
        distance = ride_details["distance"]
        LOGGER.debug("distance: %s", distance)
        # Extract correlation ID from ride details.
        correlation_id = ride_details["correlation-id"]
        LOGGER.debug("correlation_id: %s", correlation_id)

        # Persist ride details.
        persist_ride_details(unicorn_id, customer_id, submitted_at, ride_id,
                             fare, distance, correlation_id, ride_details)
def lambda_handler(event, context):

    # If the environment advises on a specific debug level, set it accordingly.
    aux.update_log_level(LOGGER, event, context)
    # Log environment details.
    aux.log_env_details(LOGGER)
    # Log request details.
    aux.log_event_and_context(LOGGER, event, context)
    # Publish Lambda event to the respective event logging topic.
    aux_api.publish_apigw_lambda_event(LOGGER, event)

    # Extract unicorn ID from request query parameter.
    unicorn_id = event["queryStringParameters"]["unicorn-id"]
    LOGGER.debug("unicorn_id: %s", unicorn_id)
    # Extract customer ID from request query parameter.
    customer_id = event["queryStringParameters"]["customer-id"]
    LOGGER.debug("customer_id: %s", customer_id)
    # Update metric for customer ID.
    #update_metric_for_requests_per_customer(customer_id)
    # Log full request.
    #log_full_request(event)
    # Extract submitted-at from request query parameter.
    submitted_at = event["queryStringParameters"]["submitted-at"]
    LOGGER.debug("submitted_at: %s", submitted_at)

    # Fetch ride details from database.
    ride_details = fetch_ride_details(unicorn_id, customer_id, submitted_at)

    # Create self link for the resource representation.
    self_link_url = create_self_link_url(event, unicorn_id, customer_id,
                                         submitted_at)

    # Create response depending on if we found a ride item in the database.
    status_code = 200
    if ride_details == STR_NONE:
        # Oh, we didn't find an item for the input data.
        data = {
            "links": {
                "self": self_link_url
            },
            "error-message": "This is not the ride you're looking for!"
        }
        status_code = 404
    else:
        # We found an item and can create a nice resource representation.
        data = {
            "links": {
                "self": self_link_url
            },
            #"title": "Ride " + ride_id + " for customer " + customer_id + " is completed by unicorn " + unicorn_id,
            "unicorn-id": unicorn_id,
            "customer-id": customer_id,
            "submitted-at": submitted_at,
            "ride-details": ride_details
        }

    # Return resource representation.
    return {
        "statusCode": status_code,
        "body": json.dumps(data),
        "headers": {
            "Content-Type": "application/json"
        }
    }