Hi Folks question here on using Dry Effects with Auto Inject and Container. I have a situation where I am providing a dependency to a series of HTTP requests where each request needs to be authenticated with a JWT token. Right now each http request by default will open the connection to the auth endpoint and get a valid JWT, but for chained requests, this causes a lot of extra JWT requests. I could re-write my injection to provide a singleton instance of the authenticated connection, but I was also thinking I could provide it as an effect and that might be a nicer solution. I’m stumbling a little on how to actually do that though if your app isn’t built explicitly on middleware. Here is a subset of my code:
module Documents class ServiceAccount include Dry::Monads[:result] include Import["service_account_token", "box_client"] def call service_account_token.get_token.bind(box_client) end end module Documents class Container register "service_account" do ServiceAccount.new end end Import = Dry::AutoInject(Container) end
so inside some handler I can do:
include Import["service_account"] ... connection = yield service_account.call result = yield connection.some_client_method
inside tests, I just stub the container dependency.
The effects documentation says that its compatible with the container, but I’m a little unsure on how to take the existing code here and move towards providing that dependency as an effect since most of the examples assume a class you new up in the middleware, and the only place my container is used and an instance is created is presumably the auto inject import constructor:
Import = Dry::AutoInject(Container)
Thanks in advance!