def get_account_info(self, client): client.host = DSConfig.auth_server() response = client.call_api("/oauth/userinfo", "GET", response_type="object") if len(response) > 1 and 200 > response[1] > 300: raise Exception("can not get user info: %d".format(response[1])) accounts = response[0]['accounts'] target = DSConfig.target_account_id() if target is None or target == "FALSE": # Look for default for acct in accounts: if acct['is_default']: return acct # Look for specific account for acct in accounts: if acct['account_id'] == target: return acct raise Exception( f"\n\nUser does not have access to account {target}\n\n")
def createCarbonCopy(): cc = CarbonCopy() cc.email = DSConfig.cc_email() cc.name = DSConfig.cc_name() cc.routing_order = "2" cc.recipient_id = "2" return cc
def createSigner(): signer = Signer() signer.email = DSConfig.signer_email() signer.name = DSConfig.signer_name() signer.recipient_id = "1" signer.routing_order = "1" return signer
def update_token(self): client = ExampleBase.api_client print("Requesting an access token via JWT grant...", end='') client.set_base_path(DSConfig.aud()) client.request_jwt_user_token(DSConfig.client_id(), DSConfig.impersonated_user_guid(), DSConfig.aud(), DSConfig.private_key(), TOKEN_EXPIRATION_IN_SECONDS) if ExampleBase.account is None: account = self.get_account_info(client) ExampleBase.base_uri = account['base_uri'] + '/restapi' ExampleBase.accountID = account['account_id'] client.host = ExampleBase.base_uri ExampleBase._token_received = True ExampleBase.expiresTimestamp = (int(round(time.time())) + TOKEN_EXPIRATION_IN_SECONDS) print("Done. Continuing...")
def create_private_key_temp_file(cls, file_suffix): """ create temp file and write into private key string in :param file_suffix: :return: """ tmp_file = tempfile.NamedTemporaryFile(mode='w+b', suffix=file_suffix) f = open(tmp_file.name, "w+") f.write(DSConfig.private_key()) f.close() return tmp_file
def update_token(self): client = ExampleBase.api_client private_key_file = DSHelper.create_private_key_temp_file("private-key") print("Requesting an access token via JWT grant...", end='') client.configure_jwt_authorization_flow( private_key_file.name, DSConfig.aud(), DSConfig.client_id(), DSConfig.impersonated_user_guid(), TOKEN_EXPIRATION_IN_SECONDS) private_key_file.close() if ExampleBase.account is None: account = self.get_account_info(client) ExampleBase.base_uri = account['base_uri'] + '/restapi' ExampleBase.accountID = account['account_id'] client.host = ExampleBase.base_uri ExampleBase._token_received = True ExampleBase.expiresTimestamp = (int(round(time.time())) + TOKEN_EXPIRATION_IN_SECONDS) print("Done. Continuing...")
def send_envelope(self): self.check_token() # document 1 (html) has sign here anchor tag **signature_1** # document 2 (docx) has sign here anchor tag /sn1/ # document 3 (pdf) has sign here anchor tag /sn1/ # # The envelope has two recipients. # recipient 1 - signer # recipient 2 - cc # The envelope will be sent first to the signer. # After it is signed, a copy is sent to the cc person. args = { 'signer_email': DSConfig.signer_email(), 'signer_name': DSConfig.signer_name(), 'cc_email': DSConfig.cc_email(), 'cc_name': DSConfig.cc_name(), } # create the envelope definition env = EnvelopeDefinition(email_subject='Please sign this document set') doc1_b64 = base64.b64encode(bytes(self.create_document1(args), 'utf-8')).decode('ascii') # read files 2 and 3 from a local directory # The reads could raise an exception if the file is not available! with open(path.join(demo_docs_path, DOC_2_DOCX), "rb") as file: doc2_docx_bytes = file.read() doc2_b64 = base64.b64encode(doc2_docx_bytes).decode('ascii') with open(path.join(demo_docs_path, DOC_3_PDF), "rb") as file: doc3_pdf_bytes = file.read() doc3_b64 = base64.b64encode(doc3_pdf_bytes).decode('ascii') # Create the document models document1 = Document( # create the DocuSign document object document_base64=doc1_b64, name='Order acknowledgement', # can be different from actual file name file_extension='html', # many different document types are accepted document_id='1' # a label used to reference the doc ) document2 = Document( # create the DocuSign document object document_base64=doc2_b64, name='Battle Plan', # can be different from actual file name file_extension='docx', # many different document types are accepted document_id='2' # a label used to reference the doc ) document3 = Document( # create the DocuSign document object document_base64=doc3_b64, name='Lorem Ipsum', # can be different from actual file name file_extension='pdf', # many different document types are accepted document_id='3' # a label used to reference the doc ) # The order in the docs array determines the order in the envelope env.documents = [document1, document2, document3] # Create the signer recipient model signer1 = Signer(email=args['signer_email'], name=args['signer_name'], recipient_id="1", routing_order="1") # routingOrder (lower means earlier) determines the order of deliveries # to the recipients. Parallel routing order is supported by using the # same integer as the order for two or more recipients. # create a cc recipient to receive a copy of the documents cc1 = CarbonCopy(email=args['cc_email'], name=args['cc_name'], recipient_id="2", routing_order="2") # Create signHere fields (also known as tabs) on the documents, # We're using anchor (autoPlace) positioning # # The DocuSign platform searches throughout your envelope's # documents for matching anchor strings. So the # signHere2 tab will be used in both document 2 and 3 since they # use the same anchor string for their "signer 1" tabs. sign_here1 = SignHere(anchor_string='**signature_1**', anchor_units='pixels', anchor_y_offset='10', anchor_x_offset='20') sign_here2 = SignHere(anchor_string='/sn1/', anchor_units='pixels', anchor_y_offset='10', anchor_x_offset='20') # Add the tabs model (including the sign_here tabs) to the signer # The Tabs object wants arrays of the different field/tab types signer1.tabs = Tabs(sign_here_tabs=[sign_here1, sign_here2]) # Add the recipients to the envelope object recipients = Recipients(signers=[signer1], carbon_copies=[cc1]) env.recipients = recipients # Request that the envelope be sent by setting |status| to "sent". # To request that the envelope be created as a draft, set to "created" env.status = "sent" envelope_api = EnvelopesApi(SendEnvelope.api_client) results = envelope_api.create_envelope(SendEnvelope.accountID, envelope_definition=env) return results