Skip to main content

Custom libraries for hotelier coupon app.

Project description

hotel_coupon_app_package_alexandermamani

Custom libraries for the hotelier coupon app. This package includes the following classes:

  • ReportPDF: Helps to generate a custom report PDF.
  • SNSService: Manages interactions with AWS SNS.
  • SQSService: Manages interactions with AWS SQS.

This package also includes the following Exceptions:

  • SNSPublishMessageError: Exception raised for publishing SNS message error.
  • SQSPollingMessagesError: Exception raised for polling SQS messages error.
  • SQSClosingConnectionError: Exception raised for closing SQS connection error.

Installation

To install the hotel coupon package, use:

pip install hotel-coupon-app-package-alexandermamani

Usage

ReportPDF

from hotel_coupon_app_package_alexandermamani.report_pdf import ReportPDF

coupon_gral_information = {}
coupon_gral_information['1'] = {}
coupon_gral_information['1']['title'] = "December offer"
coupon_gral_information['1']['how_many_have_redeemed'] = "2"
coupon_gral_information['1']['how_many_have_used'] = "1"
coupon_gral_information['1']['quantity'] = "30"
coupon_gral_information['1']['discount'] = "10"
coupon_gral_information['2'] = {}
coupon_gral_information['2']['title'] = "January offer"
coupon_gral_information['2']['how_many_have_redeemed'] = "0"
coupon_gral_information['2']['how_many_have_used'] = "0"
coupon_gral_information['2']['quantity'] = "15"
coupon_gral_information['2']['discount'] = "5"

user_interactions = {}
user_interactions["1"] = {}
user_interactions["1"]['view'] = 0
user_interactions["1"]['redeem'] = 0
user_interactions["1"]['coupon_title'] = "Winter promotion"
user_interactions["2"] = {}
user_interactions["2"]['view'] = 10
user_interactions["2"]['redeem'] = 10
user_interactions["2"]['coupon_title'] = "Summer promotion"

hotelier_name="Dublin hotel"
from_date_report=datetime.datetime.now().date()
report = ReportPDF(coupon_interaction_data=user_interactions,
                   coupon_gral_information_data=coupon_gral_information, 
                   from_date_report=from_date_report, 
                   hotelier_name=hotelier_name)

report_pdf_buffer = report.generate()

SNSService

To notify a specific user about a coupon usage, you can use the following code:

from hotel_coupon_app_package_alexandermamani.aws_services import SNSService, SNSPublishMessageError
import environ

env = environ.Env()
AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY')
AWS_REGION = env('AWS_REGION')
AWS_SNS_USED_COUPON_NOTIFICATION_ARN = env('AWS_SNS_USED_COUPON_NOTIFICATION_ARN')

message = { "user_profile_id": "u1s6574-234-123244342",
                "coupon_code": "uihj123123-123123-123"}

sns_service = SNSService(aws_access_key=AWS_ACCESS_KEY_ID, 
                         aws_secret_key=AWS_SECRET_ACCESS_KEY, 
                         region_name=AWS_REGION)
try:
    sns_service.publish_message(target_arn=AWS_SNS_USED_COUPON_NOTIFICATION_ARN, 
                                message=message, 
                                subject="Report Notification")
except SNSPublishMessageError as e:
    print("SNS Error", e)

SQSService

Example of polling user interaction data from AWS SQS to generate a custom report PDF

from hotel_coupon_app_package_alexandermamani.aws_services import SQSService, SQSPollingMessagesError,SQSClosingConnectionError
import environ
import json
from functools import partial


def handler_to_get_data_for_a_specific_hotelier_id(message, buffer_data, hotelier_coupon_ids):
    """
    Return True if the user interaction data is part of their own hotelier coupons, False otherwise.
    
    When True: Add the interaction user message to the buffer_data list and delete it from AWS SQS Queue.
    When False: keep the interaction user message to AWS SQS Queue.
    """
    message = json.loads(message['Body'])
    if message['coupon_id'] in hotelier_coupon_ids:
        buffer_data.append(message)
        return True
    else:
        return False


env = environ.Env()
AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY')
AWS_REGION = env('AWS_REGION')
AWS_SQS_QUEUE_URL = env('AWS_SQS_QUEUE_URL')

buffer_processed_data = []

hotelier_coupon_ids = ["321-123-123123-123", 
                       "898-s-2-132-ad-213",
                       "f123-d2312-dwd-123"]

handler_with_buffer = partial(message_handler=handler_to_get_data_for_a_specific_hotelier_id, 
                              buffer_data=buffer_processed_data, 
                              hotelier_coupon_ids=hotelier_coupon_ids)

try:
    sqs_queue_instance = SQSService(aws_access_key_id=AWS_ACCESS_KEY_ID,
                                    aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
                                    aws_region=AWS_REGION,
                                    aws_sqs_queue_url=AWS_SQS_QUEUE_URL)    
    sqs_queue_instance.poll_messages(handler_with_buffer, target_message_count=30)
    sqs_queue_instance.close()
    
    # Use buffer_processed_data to generate custom report PDF
    
except (SQSPollingMessagesError, SQSClosingConnectionError) as e:
    print("Error SQS", e)

Example of send user interaction data from AWS Lambda to AWS SQS Queue to

import json
from hotel_coupon_app_package_alexandermamani.aws_services import SQSService, SQSSendMessageError, SQSClosingConnectionError
import os


def lambda_handler(event, context):
    sqs_queue_instance = SQSService(aws_sqs_queue_url=os.environ['AWS_SQS_QUEUE_URL'])
    message = json.loads(event['body'])['message']

    data = {}
    data['coupon_id'] = message['coupon_id']
    data['action'] = message['action']
    data['user_profile_id'] = message['user_profile_id']
    data['country'] = message['country']
    data['date'] = message['date']

    try:
        sqs_queue_instance.send_message(data)
        sqs_queue_instance.close()
    except (SQSSendMessageError, SQSClosingConnectionError) as e:
        raise ("Error SQS", e)

    return {}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file hotel_coupon_app_package_alexandermamani-1.1.8.tar.gz.

File metadata

File hashes

Hashes for hotel_coupon_app_package_alexandermamani-1.1.8.tar.gz
Algorithm Hash digest
SHA256 43ed36e2fbef30097d8139f2126d8bea56a7726b0440df7d44d43950d4b80e8b
MD5 2a819abd3fd9f53cda6d078087e0e9f2
BLAKE2b-256 a6e7844c5d84c7cf3c0595373c1c2ebd9051c64089f8badc2959ba48df232a03

See more details on using hashes here.

File details

Details for the file hotel_coupon_app_package_alexandermamani-1.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for hotel_coupon_app_package_alexandermamani-1.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 dd3f8f339b059f1b071639b1270505ad539b5529349da01e15e4239eb605b3e8
MD5 d3ebd7b5c73fbd0b52ee7c30f234d8e2
BLAKE2b-256 eb81cfdffbf29d6380c9e3a528a01b2309229720327be82dbbcdb6403100be94

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page