So I am writing a datamodel in python that interacts with an python module that queries the api.
class QueryAPI():
def __init__(self, clientid, clientsecret):
self.clientid = clientid
self.clientsecret = clientsecret
def create_user_info(self, name, address):
......
class MyModel(object):
def __init__(self, name, address);
self.name = name
self.address = address
def commit():
create_user_info(name, address)
and this api has lot more functions that will be used by my other datamodels. Right now, I am wondering where should I instantiate this QueryAPI object. Does it make sense to have a base class that instantiate it and have all my data models access it from there?
5
You can use the concept of a connection object (similar to how database driver libraries work) . Your API clients will instantiate your QueryAPI object once passing the clientid and secret.
client = QueryAPI(clientid, clientsecret)
user = User(name, address)
client.create_user(user)
If your data model objects can in turn own child objects it might be better to keep a reference to the QueryAPI in each data model to provide a more fluent interface.
client = QueryAPI(clientid, clientsecret)
user = client.create_user(firstname, lastname) # return a new user object with a reference to the QueryAPI
user.commit()
childentity = user.create_child_entity(attribute1, attribute2) # return a new child entity with reference to user and the QueryAPI
childentity.commit()