Quantcast
Channel: Akka Libraries - Discussion Forum for Akka technologies
Viewing all articles
Browse latest Browse all 1366

Can not get variable values inside event sourced entity

$
0
0

Hello

I have an event sourced entity (TokenRefreshBaseEntity). When it receives TokenRefreshServiceCommand, it makes a json call using Http().singleRequest. After the response, it should set theProcessResultNewAccessToken, theProcessResultNewRefreshToken values and persist them with some other variables. Although http requests returns respective values of m.access_token and m.refresh_token succesfully, they cannot be assigned to the variables theProcessResultNewAccessToken and theProcessResultNewRefreshToken within oncomplete of the responseFuture. In this link (Request-Level Client-Side API • Akka HTTP), it says if it is an actor then I should use “.pipeTo(self)” But this is not an actor, it is an entity object called from main like : TokenRefreshBaseEntity.init(system)

Can you help me find why I cannot assign values of var values inside the future?

Regards, Ozcan

private def handleCommand(ServiceHandleCommandEntityIdParam: String, state: State, command: Command): ReplyEffect[Event, State] =
{
log.info(“TokenRefreshAllRefreshedTokensProjectionHandler Pre OK”)

command match
{
  case TokenRefreshServiceCommand(ServiceHandleCommandEntityIdParam,grantType, refreshToken, stateScope, userName, password, clientId, clientSecret, replyTo) =>

    log.info("TokenRefreshAllRefreshedTokensProjectionHandler OK")

    var theProcessResultNewAccessToken = "EMPTY"
    var theProcessResultNewRefreshToken="EMPTY"
    val theProcessResultTokenType="EMPTY"
    val theProcessResultExpiresIn="EMPTY"

    val dataAsJson = "{\n\"tokenData\":{\n\"refresh_token\":\"" + refreshToken +  "\"}\n}"
    println(dataAsJson)

    implicit val formats = DefaultFormats
    case class TokenQueryResult(refresh_token: String, access_token: String)

    val request = HttpRequest(
      method = HttpMethods.POST,
      uri = "http://alpha.website.com/KRSRefreshToken/",
      entity = HttpEntity(
        ContentTypes.`application/json`,
        dataAsJson
      )
    )

    implicit val system = ActorSystem(Behaviors.empty, "SingleRequest")
    implicit val executionContext = system.executionContext
    log.info("request {}", request)

    val responseFuture = Http().singleRequest(request)
      .flatMap(_.entity.toStrict(2.seconds))
      .map(_.data.utf8String)

    responseFuture
      .onComplete {
        case Success(res) =>log.info(s"Title: yields content: ${res}")
          val json = parse(res)
          val m = json.extract[TokenQueryResult]
          theProcessResultNewAccessToken=m.access_token
          println(m.access_token)
          theProcessResultNewRefreshToken=m.refresh_token
          println(m.refresh_token)
        case Failure(_) => log.info(s"Done with title err")
          theProcessResultNewAccessToken="NOTHING"
          theProcessResultNewRefreshToken="NOTHING"
      }

    Effect
        .persist(TokenRefreshServiceDoneEvent(ServiceHandleCommandEntityIdParam, grantType, refreshToken, stateScope, userName, password, clientId, clientSecret, theProcessResultNewAccessToken,theProcessResultTokenType,theProcessResultExpiresIn,theProcessResultNewRefreshToken))
        .thenReply(replyTo)
        {
           someCommandDoneState =>
            StatusReply.Success(TokenRefreshServiceCommandResult(
              someCommandDoneState.ServiceStateEntityId, someCommandDoneState.ServiceStateGrantType ,someCommandDoneState.ServiceStateRefreshToken,someCommandDoneState.ServiceStateScope
              ,someCommandDoneState.ServiceStateUserName,someCommandDoneState.ServiceStatePassword,someCommandDoneState.ServiceStateClientId,someCommandDoneState.ServiceStateClientSecret
              , theProcessResultNewAccessToken,theProcessResultTokenType,theProcessResultExpiresIn,theProcessResultNewRefreshToken))
        }
  case Get(replyTo) =>
    Effect.reply(replyTo)(state.toSummary)
}

}

4 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 1366

Trending Articles