1

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?

  • Does it make sense to have a base class that instantiate it and have all my data models access it from there? -- That depends on whether or not you need the features and functionality that a base class provides. – Robert Harvey Jul 7 '16 at 15:18
  • I need it. Also, I want to have a abstract method called commit ?. Following python idiom is it good to have base class and abstract method inside it. – paris_serviola Jul 7 '16 at 15:26
  • Sounds like you already know the answer. – Robert Harvey Jul 7 '16 at 15:26
  • so you are saying yes – paris_serviola Jul 7 '16 at 15:46
  • I am saying that, if you need it, you need it. – Robert Harvey Jul 7 '16 at 16:05
0

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.