如何在面向对象编程中使用更好的编码实现同样的功能?

2024-09-30 22:10:53 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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)

Tags: namehttpscomapiidjsonurlresponse
1条回答
网友
1楼 · 发布于 2024-09-30 22:10:53

如果您希望您的代码是可重用的,您只需要使用一些函数。你是否把这些函数放在一个类中完全是个人品味的问题。下面是你将如何做到这一点。你知道吗

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是怎么工作的,但这应该给你一个好的开始。你知道吗

相关问题 更多 >