如何通过更好的编码在面向对象的编程中执行相同的操作



我如何在以更好的编码为方面的面向对象的编程中执行相同的操作?也许通过创建类并重复使用相同的代码?现在,我拥有的更像是一个脚本不可解决的代码

import requests
import json
url = "https://sandbox.esignlive.com/api/packages"
payload = json.dumps({"documents":[{"id":"sample-contract","name":"Test Document"}],"status":"SENT","type":"PACKAGE","roles":[{"type":"SIGNER","id":"Signer1","signers":[{"email":"signer@example.com","firstName":"John","lastName":"Smith","id":"Signer1"}],"name":"Signer1"}],"name":"Example Package"})
file = open('doc1.pdf', 'rb')
files = {
     'payload': payload,
     'file': file
}
headers = {
    'authorization': "Basic **********",
    'accept': "application/json"
    }
response = requests.post(url, files=files, headers=headers)
# create a new approval
url = "https://sandbox.esignlive.com/api/packages/" + str(response.text[1]) + "/documents/sample-contract/approvals"
requests.post(url, headers=headers)
# Create a new field with an auto-generated name
url = "https://sandbox.e-signlive.com/api/user/customfields"
requests.post(url, headers=headers)
# get and display signing url
url = "https://sandbox.e-signlive.com/api/packages/"+response.text+"/roles/Signer1/signingUrl"
response = requests.get(url, headers=headers)
print(response.text)

如果您希望代码可重复使用,则只需使用某些功能即可。天气与否,您将这些功能放入班级都是个人品味的问题。以下是您这样做的方式。

import requests
import json
class Document(object):
    def __init__(self):
        """ Setup the variables on the object as soon as one is created """
        self.url = "https://sandbox.esignlive.com/api/packages"
        self.headers = {
            'authorization': "Basic **********",
            'accept': "application/json"
            }
    def create_new_approval(self, doc):
        """create a new approval """
        #you'll probably want to change this function to accept additional items other than self and doc that you can place inside of the payload. I'll leave that up to you to do.
        payload = json.dumps({"documents":[{"id":"sample-contract","name":"Test Document"}],"status":"SENT","type":"PACKAGE","roles":[{"type":"SIGNER","id":"Signer1","signers":[{"email":"signer@example.com","firstName":"John","lastName":"Smith","id":"Signer1"}],"name":"Signer1"}],"name":"Example Package"})
        file = open(doc, 'rb')
        files = {
             'payload': payload,
             'file': file
        }
        response = requests.post(url, files=files, headers=self.headers)
        url = "https://sandbox.esignlive.com/api/packages/" + str(response.text[1]) + "/documents/sample-contract/approvals"
        response = requests.post(self.url, headers=self.headers)
        #You'll want to do something like this each time you talk to esign in case esign gives back an error
        if not reponse:
            raise Exception("Failed to create an approval")
        return response.text
    def add_field(self):
        """Create a new field with an auto-generated name """
        url = "https://sandbox.e-signlive.com/api/user/customfields"
        requests.post(self.url, headers=self.headers)
    def get_signing_url(self, give_this_a_good_name):
        """ get the signing url """
        url = "https://sandbox.e-signlive.com/api/packages/"+give_this_a_good_name+"/roles/Signer1/signingUrl"
        response = requests.get(self.url, headers=self.headers)
        return response
    def display_signing_url(self):
        """ display signing url """
        print(self.get_signing_url())
# You would then use your Document object like this.
doc = Document('doc1.pdf')
doc_name_maybe = doc.add_field()
doc.display_signing_url(doc_name_maybe)

班上可能存在一些缺陷,但是如果有任何事情不起作用,请在评论中告诉我。我不知道Esign是如何工作的,但这应该给您一个起点的好地方。

最新更新