如何在面向对象编程中使用更好的编码来做同样的事情?

时间:2017-09-02 23:23:47

标签: python api

如何在面向对象编程中使用更好的编码来做同样的事情?也许通过创建一个类并重用相同的代码?现在我拥有的更像是一个不可恢复代码的脚本

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)

1 个答案:

答案 0 :(得分:0)

如果您希望代码可重复使用,则只需使用某些功能即可。天气与否你把这些功能放在课堂里都是个人品味的问题。以下是你将如何做到这一点。

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如何运作,但这应该给你一个好的起点。