Skip to content

Commit 687c6ad

Browse files
update example
1 parent 225f5ee commit 687c6ad

File tree

2 files changed

+106
-120
lines changed

2 files changed

+106
-120
lines changed

examples/ConnectedFields/eg001SetConnectedFields.sh

Lines changed: 8 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ declare -a Headers=('--header' "Authorization: Bearer ${ACCESS_TOKEN}" \
2222
#ds-snippet-end:ConnectedFields1Step2
2323

2424
#ds-snippet-start:ConnectedFields1Step3
25-
response=$(mktemp /tmp/response-cf.XXXXXX)
25+
response=$(mktemp ./response-cf.XXXXXX)
2626
Status=$(curl -w '%{http_code}' -i --ssl-no-revoke --request GET https://api-d.docusign.com/v1/accounts/${account_id}/connected-fields/tab-groups \
2727
"${Headers[@]}" \
2828
--output ${response})
@@ -49,8 +49,7 @@ invoke_python() {
4949
#Extract tab data from response
5050
#ds-snippet-start:ConnectedFields1Step4
5151
extract_verify_info() {
52-
clean_response=$(sed -n '/\[/,$p' "$response")
53-
invoke_python "filter_by_verify_action('''$clean_response''')"
52+
invoke_python "filter_by_verify_action('''$response''')"
5453
}
5554

5655
prompt_user_choice() {
@@ -73,46 +72,13 @@ prompt_user_choice() {
7372
read -p "Enter choice (1-${#unique_apps[@]}): " choice
7473
if [[ "$choice" =~ ^[1-${#unique_apps[@]}]$ ]]; then
7574
chosen_app_id="'${unique_apps[$((choice-1))]%% *}'"
76-
selected_data=$(invoke_python "filter_by_app_id($json_data, $chosen_app_id)")
77-
parse_verification_data "$selected_data"
75+
selected_data=$(invoke_python "filter_by_app_id('''$response''', $chosen_app_id)")
7876
else
7977
echo "Invalid choice. Exiting."
8078
exit 1
8179
fi
8280
}
8381

84-
parse_verification_data() {
85-
local clean_json="'''$1'''"
86-
87-
app_id=$(echo "$clean_json" | invoke_python "get_app_id($clean_json)")
88-
extension_group_id=$(echo "$clean_json" | invoke_python "get_extension_group_id($clean_json)")
89-
publisher_name=$(echo "$clean_json" | invoke_python "get_publisher_name($clean_json)")
90-
application_name=$(echo "$clean_json" | invoke_python "get_application_name($clean_json)")
91-
action_name=$(echo "$clean_json" | invoke_python "get_action_name($clean_json)")
92-
action_input_key=$(echo "$clean_json" | invoke_python "get_action_input_key($clean_json)")
93-
action_contract=$(echo "$clean_json" | invoke_python "get_action_contract($clean_json)")
94-
extension_name=$(echo "$clean_json" | invoke_python "get_extension_name($clean_json)")
95-
extension_contract=$(echo "$clean_json" | invoke_python "get_extension_contract($clean_json)")
96-
required_for_extension=$(echo "$clean_json" | invoke_python "get_required_for_extension($clean_json)")
97-
tab_label=$(echo "$clean_json" | invoke_python "get_tab_label($clean_json)")
98-
connection_key=$(echo "$clean_json" | invoke_python "get_connection_key($clean_json)")
99-
connection_value=$(echo "$clean_json" | invoke_python "get_connection_value($clean_json)")
100-
101-
echo "App ID: $app_id"
102-
echo "Extension Group ID: $extension_group_id"
103-
echo "Publisher Name: $publisher_name"
104-
echo "Application Name: $application_name"
105-
echo "Action Name: $action_name"
106-
echo "Action Contract: $action_contract"
107-
echo "Action Input Key: $action_input_key"
108-
echo "Extension Name: $extension_name"
109-
echo "Extension Contract: $extension_contract"
110-
echo "Required for Extension: $required_for_extension"
111-
echo "Tab Label: $tab_label"
112-
echo "Connection Key: $connection_key"
113-
echo "Connection Value: $connection_value"
114-
}
115-
11682
if [[ -z "$response" ]]; then
11783
echo "Error: response file variable not set."
11884
exit 1
@@ -137,6 +103,8 @@ cat demo_documents/World_Wide_Corp_lorem.pdf | base64 > $doc1_base64
137103

138104
#Construct the request body
139105
#ds-snippet-start:ConnectedFields1Step5
106+
text_tabs=$(invoke_python "make_text_tabs_list('''$selected_data''', $chosen_app_id)")
107+
140108
printf \
141109
'{
142110
"emailSubject": "Please sign this document",
@@ -167,49 +135,9 @@ printf \
167135
"anchorYOffset": "10"
168136
}
169137
],
170-
"textTabs": [
171-
{
172-
"requireInitialOnSharedChange": false,
173-
"requireAll": false,
174-
"name": "'"${application_name}"'",
175-
"required": true,
176-
"locked": false,
177-
"disableAutoSize": false,
178-
"maxLength": 4000,
179-
"tabLabel": "'"${tab_label}"'",
180-
"font": "lucidaconsole",
181-
"fontColor": "black",
182-
"fontSize": "size9",
183-
"documentId": "1",
184-
"recipientId": "1",
185-
"pageNumber": "1",
186-
"xPosition": "273",
187-
"yPosition": "191",
188-
"width": "84",
189-
"height": "22",
190-
"templateRequired": false,
191-
"tabType": "text",
192-
"extensionData": {
193-
"extensionGroupId": "'"${extension_group_id}"'",
194-
"publisherName": "'"${publisher_name}"'",
195-
"applicationId": "'"${app_id}"'",
196-
"applicationName": "'"${application_name}"'",
197-
"actionName": "'"${action_name}"'",
198-
"actionContract": "'"${action_contract}"'",
199-
"extensionName": "'"${extension_name}"'",
200-
"extensionContract": "'"${extension_contract}"'",
201-
"requiredForExtension": "'"${required_for_extension}"'",
202-
"actionInputKey": "'"${action_input_key}"'",
203-
"extensionPolicy": "None",
204-
"connectionInstances": [
205-
{
206-
"connectionKey": "'"${connection_key}"'",
207-
"connectionValue": "'"${connection_value}"'"
208-
}
209-
]
210-
}
211-
}
212-
]
138+
"textTabs": ' >> "$request_data"
139+
printf '%s' "$text_tabs" >> "$request_data" # Ensure all tabs are added
140+
printf '
213141
}
214142
}
215143
]
Lines changed: 98 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
from json import dumps, loads
22

3-
def filter_by_verify_action(data: str) -> str:
3+
def filter_by_verify_action(filename: str) -> str:
44
"""
5-
Filters items where any tab contains 'Verify' in the actionContract.
5+
Filters items where any tab contains 'Verify' in the actionContract and 'connecteddata' in the tabLabel.
66
"""
7+
8+
with open(filename) as file:
9+
data = file.read()
10+
11+
apps = data[data.find("["):] if "[" in data else ""
712
return dumps([
8-
item for item in loads(data)
9-
if any(
13+
{"appId": app["appId"], "applicationName": app["tabs"][0]["extensionData"]["applicationName"]}
14+
for app in loads(apps)
15+
if "appId" in app and app.get("tabs") and "extensionData" in app["tabs"][0] and "applicationName" in app["tabs"][0]["extensionData"]
16+
and any(
1017
("extensionData" in tab and "actionContract" in tab["extensionData"] and "Verify" in tab["extensionData"]["actionContract"]) or
1118
("tabLabel" in tab and "connecteddata" in tab["tabLabel"])
12-
for tab in item.get("tabs", [])
19+
for tab in app["tabs"]
1320
)
1421
])
1522

@@ -20,55 +27,106 @@ def extract_unique_apps(data: str):
2027
unique_apps = {}
2128
for item in loads(data):
2229
app_id = item.get('appId', '')
23-
application_name = item.get('tabs', [{}])[0].get('extensionData', {}).get('applicationName', '')
30+
application_name = item.get('applicationName', '')
2431
if app_id != '' and application_name != '':
2532
unique_apps[app_id] = application_name
2633

2734
return '\n'.join([f'{app_id} {application_name}' for app_id, application_name in unique_apps.items()])
2835

29-
def filter_by_app_id(data: str, chosen_app_id: str) -> str:
36+
def filter_by_app_id(filename: str, chosen_app_id: str) -> str:
3037
"""
3138
Filters items based on a given appId.
3239
"""
33-
return dumps([item for item in loads(data) if item.get('appId') == chosen_app_id])
34-
35-
def get_app_id(data: str) -> str:
36-
return loads(data)[0].get('appId', '')
37-
38-
def get_extension_group_id(data: str) -> str:
39-
return loads(data)[0]['tabs'][0]['extensionData'].get('extensionGroupId', '')
40-
41-
def get_publisher_name(data: str) -> str:
42-
return loads(data)[0]['tabs'][0]['extensionData'].get('publisherName', '')
43-
44-
def get_application_name(data: str) -> str:
45-
return loads(data)[0]['tabs'][0]['extensionData'].get('applicationName', '')
40+
with open(filename) as file:
41+
data = file.read()
4642

47-
def get_action_name(data: str) -> str:
48-
return loads(data)[0]['tabs'][0]['extensionData'].get('actionName', '')
43+
apps = data[data.find("["):] if "[" in data else ""
4944

50-
def get_action_input_key(data: str) -> str:
51-
return loads(data)[0]['tabs'][0]['extensionData'].get('actionInputKey', '')
45+
return dumps([item for item in loads(apps) if item.get('appId') == chosen_app_id])
5246

53-
def get_action_contract(data: str) -> str:
54-
return loads(data)[0]['tabs'][0]['extensionData'].get('actionContract', '')
47+
def extract_verification_data(selected_app_id, tab):
48+
extension_data = tab["extensionData"]
5549

56-
def get_extension_name(data: str) -> str:
57-
return loads(data)[0]['tabs'][0]['extensionData'].get('extensionName', '')
50+
return {
51+
"app_id": selected_app_id,
52+
"extension_group_id": extension_data["extensionGroupId"] if "extensionGroupId" in extension_data else "",
53+
"publisher_name": extension_data["publisherName"] if "publisherName" in extension_data else "",
54+
"application_name": extension_data["applicationName"] if "applicationName" in extension_data else "",
55+
"action_name": extension_data["actionName"] if "actionName" in extension_data else "",
56+
"action_input_key": extension_data["actionInputKey"] if "actionInputKey" in extension_data else "",
57+
"action_contract": extension_data["actionContract"] if "actionContract" in extension_data else "",
58+
"extension_name": extension_data["extensionName"] if "extensionName" in extension_data else "",
59+
"extension_contract": extension_data["extensionContract"] if "extensionContract" in extension_data else "",
60+
"required_for_extension": extension_data["requiredForExtension"] if "requiredForExtension" in extension_data else "",
61+
"tab_label": tab["tabLabel"],
62+
"connection_key": (
63+
extension_data["connectionInstances"][0]["connectionKey"]
64+
if "connectionInstances" in extension_data and extension_data["connectionInstances"]
65+
else ""
66+
),
67+
"connection_value": (
68+
extension_data["connectionInstances"][0]["connectionValue"]
69+
if "connectionInstances" in extension_data and extension_data["connectionInstances"]
70+
else ""
71+
),
72+
}
5873

59-
def get_extension_contract(data: str) -> str:
60-
return loads(data)[0]['tabs'][0]['extensionData'].get('extensionContract', '')
74+
def get_extension_data(verification_data):
75+
return {
76+
"extensionGroupId": verification_data["extension_group_id"],
77+
"publisherName": verification_data["publisher_name"],
78+
"applicationId": verification_data["app_id"],
79+
"applicationName": verification_data["application_name"],
80+
"actionName": verification_data["action_name"],
81+
"actionContract": verification_data["action_contract"],
82+
"extensionName": verification_data["extension_name"],
83+
"extensionContract": verification_data["extension_contract"],
84+
"requiredForExtension": verification_data["required_for_extension"],
85+
"actionInputKey": verification_data["action_input_key"],
86+
"extensionPolicy": 'MustVerifyToSign',
87+
"connectionInstances": [
88+
{
89+
"connectionKey": verification_data["connection_key"],
90+
"connectionValue": verification_data["connection_value"],
91+
}
92+
]
93+
}
6194

62-
def get_required_for_extension(data: str) -> str:
63-
return loads(data)[0]['tabs'][0]['extensionData'].get('requiredForExtension', '')
95+
def make_text_tab(verification_data, extension_data, text_tab_count):
96+
text_tab = {
97+
"requireInitialOnSharedChange": "false",
98+
"requireAll": "false",
99+
"name": verification_data["application_name"],
100+
"required": "true",
101+
"locked": "false",
102+
"disableAutoSize": "false",
103+
"maxLength": 4000,
104+
"tabLabel": verification_data["tab_label"],
105+
"font": "lucidaconsole",
106+
"fontColor": "black",
107+
"fontSize": "size9",
108+
"documentId": "1",
109+
"recipientId": "1",
110+
"pageNumber": "1",
111+
"xPosition": f"{70 + 100 * int(text_tab_count / 10)}",
112+
"yPosition": f"{560 + 20 * (text_tab_count % 10)}",
113+
"width": "84",
114+
"height": "22",
115+
"templateRequired": "false",
116+
"tabType": "text",
117+
"tooltip": verification_data["action_input_key"],
118+
"extensionData": extension_data
119+
}
120+
return text_tab
64121

65-
def get_tab_label(data: str) -> str:
66-
return ' '.join([tab['tabLabel'] for tab in loads(data)[0].get('tabs', [])])
122+
def make_text_tabs_list(data: str, selected_app_id: str):
123+
app = loads(data)[0]
124+
text_tabs = []
125+
for tab in (t for t in app["tabs"] if "SuggestionInput" not in t["tabLabel"]):
126+
verification_data = extract_verification_data(selected_app_id, tab)
127+
extension_data = get_extension_data(verification_data)
67128

68-
def get_connection_key(data: str) -> str:
69-
connection_instances = loads(data)[0]['tabs'][0]['extensionData'].get('connectionInstances', [])
70-
return connection_instances[0].get('connectionKey', '') if len(connection_instances) > 0 else ''
129+
text_tab = make_text_tab(verification_data, extension_data, len(text_tabs))
130+
text_tabs.append(text_tab)
71131

72-
def get_connection_value(data: str) -> str:
73-
connection_instances = loads(data)[0]['tabs'][0]['extensionData'].get('connectionInstances', [])
74-
return connection_instances[0].get('connectionValue', '') if len(connection_instances) > 0 else ''
132+
return dumps(text_tabs)

0 commit comments

Comments
 (0)