lundi 23 mars 2020

What is the clean way to integrate another service to django

I have a Django 3 application, using an LDAP service class, like this :

class LDAPService:
    def init(self, host: str, user: str, password: str, ssl: bool = True):
        ...
    def bind():  # The connection is done here, __init__ just sets values
        ....
    def create_ou(base: str, ou_name: str):
        ....

Where (or when) should I initialize the service to use it in views ? The bind step takes about 2 seconds to apply, I can not do it on every request. How can I keep an instance of this class shared, and not done every single time ? I may have a solution using singleton, and/or initializing it in like settings files, but i think there is a better way to do it.

I know in production, there may be multiple workers, so multiple instances, but i am ok with it.

Another question: How can everything above be done, with connections credentials from a database model (so not at django startup, but at anytime)

I am totally new to the django ecosystem, the things i have found about a service layer were all about django models. I want to do the same interface i would do for models in a regular service layer, but working on something else than django models.

I think the LDAP connection itself should not be there, only the CRUD methods, but i do not know where to place it, and how to make django interact with.

Thanks in advance for your suggestions :)

Aucun commentaire:

Enregistrer un commentaire