mercredi 13 février 2019

How to solve circular dependency scenario while executing http calls which require authentication?

I am working on a project which requires me to execute standard HTTP calls with session tokens. I am building a custom HTTP client, with a custom authenticator, something like this:

Client client = Client.Builder().withConfig().withAuthenticator(Authenticator);

and my authenticator is an interface

public interface Authenticator{
  SessionToken getSessionToken(); // so that different authentication methods can be supported
}

Now, for executing a request, I need to get a session token, which I obtain by calling authenticator.getSessionToken(). However, the authenticator has to call the backend to get the token, for which it requires a client, leading to a circular dependency. How do I solve this? One approach is to create a separate authentication client, which I pass in the constructor of the Authenticator Implementation, but the package imports would still show circular dependency. Is there a better way to design this?

Aucun commentaire:

Enregistrer un commentaire