2
votes

I'm not sure if there's something really basic that I'm missing, but I can't figure out how to use WSClient. I've seen all of the examples saying you need to pass the WSClient to a class as a dependency, which I've done, but when I run the program what do I actually pass to my class?

For example, my class signature is:

class myClassName(ws: WSClient)

But when I instantiate the class what do I actually pass to it? I'm also happy to ignore the Play! framework stuff if that makes it easier and just use SBT to run it (which I'm more familiar with).

1

1 Answers

3
votes

It's unclear where you might be using a WSClient, but it is recommended that you let the Play framework 'manage' the instance of the client. When you instantiate your application, it gets injected:

class Application @Inject() (ws: WSClient) extends Controller {
  ...
}

What that means is that inside the ... you have access to ws as a value. You can instantiate myClassName using it:

class Application @Inject() (ws: WSClient) extends Controller {
  val myclass = myClassName(ws)  // passes the injected WSClient to myClassName
}

Or you can write a function that returns the WSClient, so some other area of your code can call into your Application object to get a object handler for it.

But the key is that the Application object gets that handle because of injection, which is the @Inject annotation.

If you need to generate a WSClient and manage it manually, there are good instructions here. The recommended implementation is reliant on Play! framework libraries, but doesn't depend on the Application.