1
1
package services
2
2
3
+ import cats .data .EitherT
4
+ import cats .syntax ._
3
5
import com .google .inject .Inject
4
6
import models .Person
5
7
import play .api .http .Status
6
8
import play .api .libs .json .Json
7
9
import play .api .libs .ws .{WSAuthScheme , WSClient }
8
10
import play .api .{Configuration , Logging }
9
11
10
- import java .time .Instant
11
12
import scala .concurrent .{ExecutionContext , Future }
12
13
13
14
class PeopleService @ Inject ()(configuration : Configuration ,
@@ -28,35 +29,42 @@ class PeopleService @Inject()(configuration: Configuration,
28
29
private val clientSecret : String = configuration.get[String ](" lunagraph.client.secret" )
29
30
private val clientIssuer : String = configuration.get[String ](" lunagraph.client.issuer" )
30
31
31
- def getPersons : Future [Either [String , Seq [Person ]]] = for {
32
- token <- getToken()
33
- wsRes <- ws.url(s " ${baseUrl}/graphql " )
34
- .withHttpHeaders(" Authorization" -> s " Bearer ${token}" )
32
+ def getPersons : Future [Either [String , Seq [Person ]]] = {
33
+ for {
34
+ token <- EitherT (getToken())
35
+ people <- EitherT (getPersonsWithToken(token))
36
+ } yield people
37
+ }.value
38
+
39
+ private def getPersonsWithToken (token : String ): Future [Either [String , Seq [Person ]]] = {
40
+ ws.url(s " ${baseUrl}/graphql " )
41
+ .withHttpHeaders(" Authorization" -> s " Bearer $token" )
35
42
.post(Json .toJsObject(Map (" query" -> query)))
36
- res = wsRes.status match {
37
- case Status .OK =>
38
- val jsonResult = Json .parse(wsRes.body)
39
- Right ((jsonResult \ " data" \ " people" ).get.as[Seq [Person ]])
40
- case _ => logger.error(wsRes.body)
41
- Left (wsRes.body)
42
- }
43
- } yield res
44
-
45
-
46
- private def getToken (): Future [Either [String , String ]] = {
47
- for {
48
- clientResponse <- ws.url(s " ${clientIssuer}/protocol/openid-connect/token " )
49
- .withAuth(clientId, clientSecret, WSAuthScheme .BASIC )
50
- .post(Map (" grant_type" -> Seq (" client_credentials" )))
51
- parsedResponse = clientResponse.status match {
52
- case Status .OK =>
53
- val jsonResponse = Json .parse(clientResponse.body)
54
- val token = (jsonResponse \ " access_token" ).get.as[String ]
55
- Right (token)
56
- case _ =>
57
- logger.error(clientResponse.body)
58
- Left (clientResponse.body)
43
+ .map { wsRes =>
44
+ wsRes.status match {
45
+ case Status .OK =>
46
+ val jsonResult = Json .parse(wsRes.body)
47
+ Right ((jsonResult \ " data" \ " people" ).get.as[Seq [Person ]])
48
+ case _ => logger.error(s " ${wsRes.status} - ${wsRes.body}" )
49
+ Left (wsRes.body)
50
+ }
59
51
}
60
- } yield parsedResponse
61
52
}
53
+
54
+
55
+ private def getToken (): Future [Either [String , String ]] =
56
+ ws.url(s " ${clientIssuer}/protocol/openid-connect/token " )
57
+ .withAuth(clientId, clientSecret, WSAuthScheme .BASIC )
58
+ .post(Map (" grant_type" -> Seq (" client_credentials" )))
59
+ .map { clientResponse =>
60
+ clientResponse.status match {
61
+ case Status .OK =>
62
+ val jsonResponse = Json .parse(clientResponse.body)
63
+ val token = (jsonResponse \ " access_token" ).get.as[String ]
64
+ Right (token)
65
+ case _ =>
66
+ logger.error(s " ${clientResponse.status} - ${clientResponse.body}" )
67
+ Left (clientResponse.body)
68
+ }
69
+ }
62
70
}
0 commit comments