refactoring

This commit is contained in:
Eugene Zadyra
2019-04-19 23:30:31 +02:00
parent 6801cd7d12
commit 823b409c92
3 changed files with 33 additions and 21 deletions

View File

@@ -0,0 +1,4 @@
package com.lbs.api.exception
class SessionExpiredException extends ApiException("Session expired")

View File

@@ -1,14 +1,14 @@
package com.lbs.api
import com.lbs.api.exception.{ApiException, GenericException, InvalidLoginOrPasswordException, ServiceIsAlreadyBookedException}
import com.lbs.api.exception.{ApiException, GenericException, InvalidLoginOrPasswordException, ServiceIsAlreadyBookedException, SessionExpiredException}
import com.lbs.api.json.JsonSerializer.extensions._
import com.lbs.api.json.model._
import com.lbs.common.Logger
import scalaj.http.{HttpRequest, HttpResponse}
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try
import scala.util.{Failure, Try}
package object http extends Logger {
@@ -46,7 +46,7 @@ package object http extends Logger {
val httpResponse = Try(httpRequest.asString)
debug(s"Received response:\n$httpResponse")
extractLuxmedError(httpResponse) match {
case Some(error) => Try(throw error)
case Some(error) => Failure(error)
case None => httpResponse.map(_.throwError)
}
}
@@ -62,6 +62,8 @@ package object http extends Logger {
new InvalidLoginOrPasswordException
else if (errorMessage.contains("already booked this service"))
new ServiceIsAlreadyBookedException
else if (errorMessage.contains("session has expired"))
new SessionExpiredException
else
new GenericException(ler.code, ler.statusLine, message)
}

View File

@@ -1,6 +1,7 @@
package com.lbs.server.service
import com.lbs.api.exception.SessionExpiredException
import com.lbs.api.json.model.LoginResponse
import com.lbs.common.{Logger, ParametrizedLock}
import com.lbs.server.exception.UserNotFoundException
@@ -32,32 +33,37 @@ trait SessionSupport extends Logger {
}
}
def session: Either[Throwable, Session] = {
def getSession: Either[Throwable, Session] = {
sessions.get(accountId) match {
case Some(sess) => Right(sess)
case None =>
auth match {
case Right(sess) =>
sessions.put(accountId, sess)
Right(sess)
case left => left
for {
session <- auth
} yield {
sessions.put(accountId, session)
session
}
}
}
session match {
case Right(s) =>
fn(s) match {
case Left(ex) if ex.getMessage.contains("session has expired") =>
debug(s"The session for account [#$accountId] has expired. Try to relogin")
sessions.remove(accountId)
session.flatMap(fn)
case another =>
debug(s"Call to remote api function has completed with result:\n$another")
another
}
case Left(ex) => Left(ex)
def doApiCall = {
for {
session <- getSession
result <- fn(session)
} yield result
}
for {
result <- doApiCall match {
case Left(_: SessionExpiredException) =>
debug(s"The session for account [#$accountId] has expired. Try to relogin")
sessions.remove(accountId)
doApiCall
case another =>
debug(s"Call to remote api function has completed with result:\n$another")
another
}
} yield result
}
def addSession(accountId: Long, accessToken: String, tokenType: String): Unit =