jeudi 4 février 2016

What is a good pattern for working with a service client instance in python?

Say I'm building a software repository web API. I want to be implementation agnostic, so I will support both github and bitbucket. They both have python client libraries, so I will use them. I will have to have an instance of GithubClient and configure it. I only want to configure it (put in the credentials from the config) once and then be able to use the same Client instance for the lifetime of my server process.

What is the best pattern in python for using that Client instance? Two ways I've seen used are:

  1. Store the client instance in a module. The instance gets created when it is first imported and it is configured when the server process first starts. All the users of the client instance will just import it from that module. This is effectively a singleton.

  2. Initialize and configure the client instance at server startup and pass the instance to other methods/objects which need to use it as an argument.

There are many variations on these two themes: You can check if the client is configured each time you make a call or you can create another service singleton effectively which wraps the external client instance and calls methods on it etc.

Can anyone provide links to documented best practices regarding to this? I'm specifically interested in doing this in a pythonic way.

Aucun commentaire:

Enregistrer un commentaire