-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparent_segment_api.py
More file actions
148 lines (132 loc) · 5.04 KB
/
Copy pathparent_segment_api.py
File metadata and controls
148 lines (132 loc) · 5.04 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import logging
logger = logging.getLogger(__name__)
def createParentSegment(client, body):
try:
response = None
URL = f"/audiences"
response = client.request("POST", URL, json=body)
return response.get("id"), response.get("name"), "created"
except Exception as ex:
logger.error(
"createParentSegment exception: %s, URL: %s, body: %s, response: %s",
str(ex),
URL,
body,
response if response else "No Response",
)
raise
def updateParentSegment(client, body, id):
try:
response = None
URL = f"/audiences/{id}"
response = client.request("PUT", URL, json=body)
return response.get("id"), response.get("name"), "updated"
except Exception as ex:
logger.error(
"updateParentSegment exception: %s, URL: %s, body: %s, response: %s",
str(ex),
URL,
body,
response if response else "No Response",
)
raise
def deleteParentSegment(client, id):
try:
response = None
URL = f"/audiences/{id}"
response = client.request("DELETE", URL)
return response.get("id"), response.get("name"), "updated"
except Exception as ex:
logger.error(
"deleteParentSegment exception: %s, URL: %s, response: %s",
str(ex),
URL,
response if response else "No Response",
)
raise
def getParentSegment(client, _body):
try:
import json
# Ensure _body is a dictionary
if isinstance(_body, str):
try:
_body = json.loads(_body)
except json.JSONDecodeError as e:
logger.error("Failed to parse _body as JSON: %s", str(e))
raise
if not isinstance(_body, dict):
raise TypeError("_body must be a dictionary or valid JSON string")
response = None
URL = f"/audiences"
response = client.request("GET", URL)
total_segments = len(response) if isinstance(response, list) else 0
logger.info(
"getParentSegment searching through %d parent segments for id: %s, name: %s",
total_segments,
_body.get("id"),
_body.get("name"),
)
# Track progress for large lists
for idx, row in enumerate(response):
# Log progress every 100 segments to avoid excessive logging
if idx > 0 and idx % 100 == 0:
logger.info(
"getParentSegment progress: checked %d/%d parent segments",
idx,
total_segments,
)
if _body.get("id") and str(row["id"]) == str(_body["id"]):
logger.info(
"Found Parent Segment by id %s at position %d", row.get("id"), idx
)
return (row.get("id"), row.get("name"), "selected")
elif (
not _body.get("id")
and _body.get("name")
and str(row["name"]) == str(_body["name"])
):
logger.info(
"Found Parent Segment by name '%s' (id: %s) at position %d",
row.get("name"),
row.get("id"),
idx,
)
return (row.get("id"), row.get("name"), "selected")
elif not _body.get("id") and not _body.get("name"):
logger.warning(
"At least name or id should be provided in the parent segment template yml file."
)
break
# No Matches
logger.info("Not Found Parent Segment (searched %d segments)", total_segments)
return None, None, "selected"
except Exception as ex:
logger.error(
"getParentSegment exception: %s, URL: %s, response: %s",
str(ex),
URL,
response if response else "No Response",
)
raise
def ps_check_and_update(client, body):
# check if parent segment exists
# if exists return the audience_id
# and update the parent segment
audience_id, name, message = getParentSegment(client, body)
try:
if audience_id:
logger.info("ps_check_and_update update parent segment")
audience_id, name, message = updateParentSegment(client, body, audience_id)
elif not audience_id:
logger.info("ps_check_and_update create parent segment")
audience_id, name, message = createParentSegment(client, body)
return audience_id, name, message
except Exception as ex:
logger.info(
"ps_check_and_update exception for create and update now doing deletion: %s",
str(ex),
)
_audience_id, _name, _message = deleteParentSegment(client, audience_id)
logger.info("ps_check_and_update create parent segment after deletion")
audience_id, name, message = createParentSegment(client, body)
return audience_id, name, message