-
Notifications
You must be signed in to change notification settings - Fork 34
/
client.py
242 lines (195 loc) · 7.79 KB
/
client.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import os
import sys
import json
import requests
import random
import secrets.auth as auth
PREFIX = '_'
HEADERS = {
'content-type': 'application/json',
'x-pendo-xsrf-token': auth.get_xsrf(),
'cookie': '; '.join(auth.get_cookies()),
}
ids_map = {}
s = requests.Session()
s.headers.update(HEADERS)
class CacheGetter:
cache = {}
def get(self, input_url):
r = s.get(url(input_url))
r.raise_for_status()
json_data = r.json()
if not input_url in self.cache:
log('cache miss for: {}'.format(input_url))
self.cache[input_url] = json_data
return json_data
log('cache hit for: {}'.format(input_url))
return self.cache[input_url]
cache_getter = CacheGetter()
def log(msg):
print('> DEBUG: {}'.format(msg), file=sys.stderr)
def stash_ids(pendo_id, config_id):
ids_map[config_id] = pendo_id
def get_ids_map():
return ids_map
def clear_ids_map():
ids_map.clear()
def get_name(name):
if name.startswith(PREFIX):
return name
return PREFIX + name
def get_feature_full_name(group_name, feature_name):
return '{} | {}'.format(group_name, feature_name)
def url(path):
return 'https://app.pendo.io/api/s/5300167311360000{}'.format(path)
def _create_feature_idempotent(name, data):
log('looking for feature: {}'.format(name))
for feature in cache_getter.get('/feature?expand=*'):
if feature['name'] == name:
log('found feature [{}] {}'.format(feature['id'], name))
stash_ids(feature['id'], name)
return feature
return _create_feature(name, data)
def _create_feature(name, data):
payload = {
"appId": 6245718179446784,
"kind": "Feature",
"name": name,
"color": "",
"eventPropertyConfigurations": [],
"elementPathRules": data['selectors']
}
if 'page_id' in data:
log('setting page_id {}'.format(data['page_id']))
payload['pageId'] = data['page_id']
r = s.post(url('/feature?includeMobileRuleset=1'), json = payload)
r.raise_for_status()
feature = r.json()
log('created feature [{}] {}'.format(feature['id'], feature['name']))
stash_ids(feature['id'], feature['name'])
return feature
def _create_page(page_full_name, page_data):
# POST https://app.pendo.io/api/s/5300167311360000/page?includeMobileRuleset=1
# {"name":"Test123","rules":[{"rule":"//*/insights","designerHint":"https://console.redhat.com/insights/"}],"appId":6245718179446784}
r = s.post(url('/page?includeMobileRuleset=1'), json = {
"appId": 6245718179446784,
"name": page_full_name,
"rules": list(map(lambda u : { "rule": u }, page_data['url_rules']))
})
##
if r.status_code == 409:
return
r.raise_for_status()
page = r.json()
log('created page [{}] {}'.format(page['id'], page_full_name))
stash_ids(page['id'], page_full_name)
return page
def _create_page_idempotent(page_full_name, page_data):
log('looking for page: {}'.format(page_full_name))
for page in cache_getter.get('/page?expand=*'):
if page['name'] == page_full_name:
log('found page [{}] {}'.format(page['id'], page_full_name))
stash_ids(page['id'], page_full_name)
return page
return _create_page(page_full_name, page_data)
def create_page_in_group(group, page_name, page_data):
page_full_name = get_feature_full_name(group['name'], page_name)
page = _create_page_idempotent(page_full_name, page_data)
# POST https://app.pendo.io/api/s/5300167311360000/page/H9FF-czQ7RZTTb9aUB4fBbk3Cdo/apply
# no data?
r = s.post(url('/page/{}/apply'.format(page['id'])))
# r.raise_for_status()
# PUT https://app.pendo.io/api/s/5300167311360000/group/G7YaDIq_AjRz-1Y6m2x2FP_Bbe0/page/H9FF-czQ7RZTTb9aUB4fBbk3Cdo
# no data?
r = s.put(url('/group/{}/page/{}'.format(group['id'], page['id'])))
# r.raise_for_status()
_ensure_page_url_rules(page_full_name, page['id'], page_data['url_rules'], page['rules'])
def _ensure_page_url_rules(page_name, page_id, input_rules, existing_rules):
input_rules = list(map(lambda u : { "rule": u }, input_rules))
existing_rules = list(map(lambda u : { "rule": u['rule'] }, existing_rules))
if not input_rules == existing_rules:
r = s.put(url('/page/{}'.format(page_id)), json = {
"name": page_name,
"rules": input_rules
})
r.raise_for_status()
log('added rules to page [{}] {}'.format(page_id, input_rules))
stash_ids(page_id, page_name)
def create_feature_in_group(group, feature_name, feature_data):
feature_full_name = get_feature_full_name(group['name'], feature_name)
feature = _create_feature_idempotent(feature_full_name, feature_data)
_set_feature_app_name(feature['id'])
_add_feature_to_group(group['id'], feature['id'])
_ensure_feature_rules(feature_full_name, feature['id'], feature_data['selectors'], feature['elementPathRules'])
def _ensure_feature_rules(feature_name, feature_id, input_selectors, existing_selectors):
# PUT https://app.pendo.io/api/s/5300167311360000/feature/HPfHlgH8MgHemhfc_YTeLrV_RNE
if not input_selectors == existing_selectors:
r = s.put(url('/feature/{}'.format(feature_id)), json = {
"name": feature_name,
"elementPathRules": input_selectors
})
r.raise_for_status()
log('added selectors to feature [{}] {}'.format(feature_id, input_selectors))
stash_ids(feature_id, feature_name)
def get_entity(t):
headers = { 'x-pendo-integration-key': auth.API_KEY }
r = requests.get('https://app.pendo.io/api/v1/{}?expand=*'.format(t), headers=headers)
r.raise_for_status()
return r.json()
def delete_entity(t, eid):
# https://app.pendo.io/api/s/5300167311360000/feature/QynmTYtT1EJsSlzMEaa61Z6Fl-c
r = s.delete(url('/{}/{}'.format(t, eid)))
r.raise_for_status()
def _set_feature_app_name(feature_id):
r = s.post(url('/feature/{}/apply'.format(feature_id)), json = {
"namespace": "Red_Hat_Customer_Portal",
"type": "PageFeatures",
"applicationName": "cloud_redhat_com"
})
##
if r.status_code == 409:
return
r.raise_for_status()
log('set app name on feature [{}]'.format(feature_id))
def _add_feature_to_group(group_id, feature_id):
r = s.put(url('/group/{}/feature/{}'.format(group_id, feature_id)), json = {
"itemId": feature_id,
"itemType": "Feature"
})
r.raise_for_status()
log('added feature [{}] to group [{}]'.format(feature_id, group_id))
# stash_ids(feature_id, group_id)
def _int_to_color_str(i):
return '.groupColor{:02d}'.format(i)
def get_color(name, group):
if 'color' in group:
color = int(group['color'])
assert color > 0 and color <= 10, "Color for group `{}` is not an int between 1 and 10".format(name)
if color > 0 and color <= 10:
color_str = _int_to_color_str(color)
log('setting group color: {}'.format(color_str))
return color_str
color_str = _int_to_color_str(random.randint(1,10))
log('setting random group color: {}'.format(color_str))
return color_str
def create_group_idempotent(name, color):
name = get_name(name)
for group in cache_getter.get('/group'):
if group['name'] == name:
log('found group {}'.format(group['id']))
stash_ids(group['id'], group['name'])
return group
group = _create_group(name, color)
log('created group {}'.format(group['id']))
stash_ids(group['id'], group['name'])
return group
def _create_group(name, color):
name = get_name(name)
r = s.post(url('/group'), json = {
'name': name,
'description': '',
'color': color,
'items': [],
})
r.raise_for_status()
return r.json()