2

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).

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
annedroiid
  • 6,267
  • 11
  • 31
  • 54

1 Answers1

3

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.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • How would you then run this application? I'm using Intellij – annedroiid Aug 24 '16 at 01:28
  • As in my aim is to have something where I can just click run and run the entire thing using Intellij, I don't need all of the application stuff. This is just a side class to the main application. – annedroiid Aug 24 '16 at 01:32
  • Are you using SBT to compile and run your application? SBT has a plugin for IntelliJ that can be used to start applications. I can't give you a lot of guidance without knowing a lot more. – Nathaniel Ford Aug 24 '16 at 01:34
  • Yes I'm using SBT but I don't want to make a full application. I just want to be able to run this class separately. This project already has a different application in it. Or can I have multiple applications in the one project? – annedroiid Aug 24 '16 at 01:39
  • @annedroiid Updated my answer; last paragraph. Let me know if that helps! – Nathaniel Ford Aug 24 '16 at 02:30