-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
129 lines (113 loc) · 5.27 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import functions_framework
from flask import jsonify, Request
from google.cloud import bigquery
from datetime import datetime
from typing import Any
client = bigquery.Client()
DISPOSITION_MAP = {
"DROP": "No Answer", "ADC": "No Answer", "PDROP": "Outbound Pre-Routing",
"A": "Answering Machine", "AA": "Answering Machine Auto", "AB": "Busy Auto",
"B": "Busy", "CALLBK": "Call Back", "CBL": "Call Back Later",
"DC": "Disconnected Number", "DNC": "Do Not Call", "Follow": "Follow Up",
"N": "No Answer", "NAU": "No Answer", "NA": "No Answer Autodial",
"NI": "Not Interested", "NPRSN": "In Person Appointment", "Nurtre": "Nurture",
"PHNAPT": "Phone Appointment", "WN": "Wrong Number"
}
ENV_VAR_MSG = "Specified environment variable is not set."
TABLE_ID = os.getenv("BIGQUERY_TABLE_ID", ENV_VAR_MSG)
def transform_field(field: str, default: str = "") -> str:
return default if "--A--" in field and "--B--" in field else field
def validate_timestamp(timestamp: str) -> str:
"""Validate and parse timestamp."""
try:
# Try to parse the timestamp in ISO 8601 format
parsed_time = datetime.fromisoformat(timestamp)
return parsed_time.strftime("%Y-%m-%d %H:%M:%S")
except ValueError:
raise ValueError("Invalid timestamp format. Expected ISO 8601 string.")
@functions_framework.http
def post_to_bigquery_with_timestamp(request: Request) -> Any:
"""
Handles HTTP POST requests to insert data into a BigQuery table with an optional timestamp.
Args:
request (Request): The HTTP request object containing query parameters and JSON body.
Returns:
Any: A JSON response indicating the success or failure of the data insertion.
Query Parameters:
firstName (str): The first name of the individual.
lastName (str): The last name of the individual.
listDescription (str): Description of the list.
dialedNumber (str): The phone number that was dialed.
disposition (str): The disposition of the call.
talkTime (str): The talk time of the call.
termReason (str): The termination reason of the call.
callNote (str): Notes related to the call.
email (str): The email address of the individual.
listID (str): The ID of the list.
leadID (str, optional): The ID of the lead. Defaults to '0' if not provided.
subscriberID (str): The ID of the subscriber.
JSON Body:
timestamp (str, optional): The timestamp of the event in a valid format. If not provided, the current date and time will be used.
Raises:
ValueError: If the provided timestamp is in an invalid format.
Exception: For any other exceptions that occur during processing.
Returns:
JSON response:
- 200: If the data is successfully inserted into BigQuery.
- 400: If there are errors during data insertion or invalid timestamp format.
- 500: For any other exceptions encountered during processing.
"""
try:
params = request.args
firstName = params.get('firstName')
lastName = params.get('lastName')
listDescription = params.get('listDescription')
dialedNumber = params.get('dialedNumber')
disposition = params.get('disposition')
talkTime = params.get('talkTime')
termReason = params.get('termReason')
callNote = params.get('callNote')
email = params.get('email')
listID = params.get('listID')
leadID = params.get('leadID', '0')
subscriberID = params.get('subscriberID')
request_data = request.get_json(silent=True) or {}
timestamp = request_data.get('timestamp')
if timestamp:
date = validate_timestamp(timestamp)
else:
date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if not leadID:
leadID = '0'
callNotesFormatted = transform_field(callNote)
dispositionFormatted = DISPOSITION_MAP.get(disposition, disposition)
talkTimeFormatted = transform_field(talkTime, "0")
termReasonFormatted = transform_field(termReason)
subscriberIDFormatted = transform_field(subscriberID)
listDescriptionFormatted = transform_field(listDescription)
row_to_insert = [{
"Date": date,
"FirstName": firstName,
"LastName": lastName,
"CallNotesFormatted": callNotesFormatted,
"Phone": dialedNumber,
"Email": email,
"ListID": listID,
"Disposition": dispositionFormatted,
"LeadID": leadID,
"TalkTimeFormatted": talkTimeFormatted,
"TermReasonFormatted": termReasonFormatted,
"SubscriberIDFormatted": subscriberIDFormatted,
"ListDescriptionFormatted": listDescriptionFormatted
}]
errors = client.insert_rows_json(TABLE_ID, row_to_insert)
if errors:
return jsonify({"errors": errors}), 400
return jsonify({"status": "success", "message": "Data inserted into BigQuery successfully"}), 200
except ValueError as ve:
# Handle invalid timestamp format
return jsonify({"error": str(ve)}), 400
except Exception as e:
# Catch and return any exceptions
return jsonify({"error": str(e)}), 500