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

Python 如何在面向对象编程中使用更好的编码实现同样的功能?,python,api,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","typ

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

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

我认为为此定义类没有任何好处。但是如果脚本不断增加,那么我将需要它?这取决于脚本的增长方式。面向对象设计只是一种可能的工具;这对任何地方都没有帮助。假设我还有100个请求?您需要开始使用循环和函数,并且您可能希望将硬编码数据移动到单独的数据文件或其他文件中。