我正在尝试为这样工作的API创建一个Python包装器。
>>> api = APIClient(client_id, client_secret)
>>> api.Project.get(name="proj_name") # returns Project object of that specific name
如何在Project
内api
?我不想让它成为嵌套类,因为有许多其他数据对象,嵌套将创建一个大文件。如果有人为这项具体任务提出了更好的设计,我也会很高兴。
APIClient.py
class APIClient(object):
def __init__(self, username, password):
self.client_id = username
self.client_secret = password
self.headers = {"Content-Type" : "application/json"}
def _get_request(self, params):
r =requests.get(url, params = params, headers = self.headers, auth = HTTPBasicAuth(username, password)
project.py
class Project(object):
def __init__(self, name, orgname):
self.name = name
self.orgname = orgname
@classmethod
def get(name):
r = ... # should call _get_request in APIClient
customer.py
class Customer(object):
def __init__(self, name, address):
self.name = name
self.address.address
@classmethod
def get(name):
r = ... # should call _get_request with name and address as param