-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreate_from_template_and_send.py
187 lines (169 loc) · 6.98 KB
/
create_from_template_and_send.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
from time import sleep
import pandadoc_client
from pandadoc_client.model.document_create_request_content_library_items import \
DocumentCreateRequestContentLibraryItems
from pandadoc_client.model.document_create_request_content_placeholders import \
DocumentCreateRequestContentPlaceholders
from pandadoc_client.model.document_create_request_images import DocumentCreateRequestImages
from pandadoc_client.model.document_create_by_template_request_tokens import \
DocumentCreateByTemplateRequestTokens
from pandadoc_client.model.document_create_request_recipients import DocumentCreateRequestRecipients
from pandadoc_client.model.document_create_request import DocumentCreateRequest
from pandadoc_client.model.document_send_request import DocumentSendRequest
from pandadoc_client import ApiClient, Configuration
from pandadoc_client.api import documents_api
from pandadoc_client.model.pricing_table_request import PricingTableRequest
from pandadoc_client.model.pricing_table_request_row_options import PricingTableRequestRowOptions
from pandadoc_client.model.pricing_table_request_rows import PricingTableRequestRows
from pandadoc_client.model.pricing_table_request_sections import (
PricingTableRequestSections,
)
# place your api key here
API_KEY = 'YOUR_API_KEY'
#
# you should have an `Full API Sample Document from PandaDoc Template`
# onboarding template in your workspace just place its ID here
TEMPLATE_UUID = 'YOUR_TEMPLATE_UUID'
#
# you should have a several onboarding CLIs in your workspace
# just place an ID of any
CONTENT_LIBRARY_ITEM_ID = 'YOUR_CONTENT_LIBRARY_ITEM_ID'
MAX_CHECK_RETRIES = 5
def create_doc_from_sample_template(api_instance):
pricing_tables = [
PricingTableRequest(
name='Pricing Table 1',
data_merge=True,
options={
"Discount": {
"type": "absolute",
"name": "Global Discount",
"value": 10
},
"Tax": {
"type": "percent",
"name": "Tax First",
"value": 15
}
},
sections=[
PricingTableRequestSections(
title='Sample Section',
default=True,
multichoice_enabled=False,
rows=[
PricingTableRequestRows(
options=PricingTableRequestRowOptions(
qty_editable=True,
optional_selected=True,
optional=True,
),
data={
"Name": "Toy Panda",
"Description": "Fluffy",
"Price": 10,
"Cost": 8.5,
"QTY": 3,
"SKU": "toy_panda",
"Discount": {
"value": 10,
"type": "percent"
},
"Tax": {
"value": 10,
"type": "percent"
}
},
custom_fields={},
),
],
),
],
),
]
document_create_request = DocumentCreateRequest(
name='API Sample Document from PandaDoc Template',
template_uuid=TEMPLATE_UUID,
# specify a folder uuid if you want to document to be created
# in specific folder otherwise it will be created in root directory
#
# folder_uuid='QMDSzwabfFzTgjW4kUijqQ',
recipients=[
DocumentCreateRequestRecipients(
email='[email protected]',
first_name='Josh',
last_name='Ron',
role='user',
signing_order=1,
),
],
tokens=[DocumentCreateByTemplateRequestTokens(name='Favorite.Pet', value='Panda')],
fields={},
metadata={},
tags=['created_via_api', 'test_document'],
images=[
DocumentCreateRequestImages(
urls=[
'https://s3.amazonaws.com/pd-static-content/public-docs/pandadoc-panda-bear.png',
],
name='Image 1',
),
],
pricing_tables=pricing_tables,
content_placeholders=[
DocumentCreateRequestContentPlaceholders(
block_id='Content Placeholder 1',
content_library_items=[
DocumentCreateRequestContentLibraryItems(
id=CONTENT_LIBRARY_ITEM_ID,
pricing_tables=pricing_tables,
),
],
),
],
)
return api_instance.create_document(document_create_request=document_create_request)
def ensure_document_created(api_instance, document):
# Document creation is non-blocking (asynchronous) operation.
#
# With a successful request, you receive a response with the created
# document's ID and status document.uploaded.
# After processing completes on our servers, usually a few seconds,
# the document moves to the document.draft status.
# Please wait for the webhook call or check this document's
# status before proceeding.
#
# The change of status from document.uploaded to another status signifies
# the document is ready for further processing.
# Attempting to use a newly created document before PandaDoc servers
# process it will result in a '404 document not found' response.
retries = 0
while retries < MAX_CHECK_RETRIES:
sleep(2)
retries += 1
doc_status = api_instance.status_document(document['id'])
if doc_status.status == 'document.draft':
return
raise RuntimeError('Document was not sent')
def send_document(api_instance, document):
api_instance.send_document(
document['id'],
document_send_request=DocumentSendRequest(
silent=False, subject='This doc was send via python SDK'
),
)
def main():
cfg = Configuration(api_key={'apiKey': f'API-Key {API_KEY}'})
# Enter a context with an instance of the API client
with ApiClient(cfg) as client:
try:
api_instance = documents_api.DocumentsApi(client)
document = create_doc_from_sample_template(api_instance)
print(f'Document was successfully uploaded:\n{document}')
ensure_document_created(api_instance, document)
send_document(api_instance, document)
print(f'Document was successfully created and sent to the recipients!')
except pandadoc_client.ApiException as e:
print(f'{e.status} {e.reason} {e.body}')
if __name__ == '__main__':
main()