You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.1 KiB
107 lines
3.1 KiB
package wow.doge.chatto.service
|
|
|
|
import scala.concurrent.ExecutionContext
|
|
import scala.concurrent.ExecutionContext.Implicits.global
|
|
import scala.async.Async.{async, await}
|
|
import sttp.client.json4s._
|
|
import org.json4s._
|
|
import sttp.client._
|
|
import scala.concurrent.Future
|
|
import sttp.client.asynchttpclient.WebSocketHandler
|
|
import javax.inject.Inject
|
|
import scala.util.Success
|
|
import scala.util.Failure
|
|
import wow.doge.chatto.AppData
|
|
import wow.doge.chatto.types.AppTypes.HttpBackend
|
|
import com.typesafe.scalalogging.LazyLogging
|
|
import org.scalafx.extras._
|
|
import wow.doge.chatto.ApplicationController
|
|
import wow.doge.chatto.UserCredentials
|
|
import javax.inject._
|
|
import wow.doge.chatto.AppDataHandler
|
|
import org.json4s.jackson.JsonMethods._
|
|
import org.json4s.ext.JavaTimeSerializers
|
|
import java.time.ZonedDateTime
|
|
import org.json4s.jackson.Serialization._
|
|
|
|
class UserService @Inject() (appDataHandler: AppDataHandler)(
|
|
implicit backend: HttpBackend
|
|
) extends LazyLogging {
|
|
private implicit lazy val serialization = org.json4s.jackson.Serialization
|
|
private implicit lazy val formats =
|
|
DefaultFormats ++ JavaTimeSerializers.all
|
|
private val domain = "http://localhost:8080"
|
|
private lazy val baseUrl = uri"$domain/api/chat"
|
|
// private lazy val authBasicRequest = (credentials: UserCredentials) =>
|
|
// basicRequest.auth
|
|
// .basic(credentials.username, credentials.password)
|
|
// .header("X-AUTH-TOKEN", credentials.token)
|
|
private lazy val tokenBasicRequest = (token: String) => {
|
|
basicRequest.header("X-AUTH-TOKEN", token)
|
|
}
|
|
|
|
def func1() = async {
|
|
val willBeResponse = func2()
|
|
val r = await { willBeResponse }
|
|
r.body.map(println)
|
|
}
|
|
|
|
def func2() =
|
|
basicRequest
|
|
.get(uri"https://httpbin.org/get")
|
|
.response(asJson[HttpBinResponse])
|
|
.send()
|
|
|
|
private def endpoint(uri: String) = uri"$baseUrl/$uri"
|
|
|
|
def getUsers(credentials: UserCredentials) = async {
|
|
// logger.debug(s"${appDataHandler.appData}")
|
|
// println(
|
|
// write[ActiveUser](
|
|
// ActiveUser("hmm what is it", true, Some(ZonedDateTime.now()))
|
|
// )
|
|
// )
|
|
await {
|
|
// authBasicRequest(credentials)
|
|
tokenBasicRequest(credentials.token)
|
|
.get(uri"http://localhost:8080/api/chat/get/users")
|
|
.response(asJson[List[String]])
|
|
.send()
|
|
}
|
|
}
|
|
|
|
def getMessages(credentials: UserCredentials) = async {
|
|
// logger.debug(s"${appDataHandler.appData}")
|
|
await {
|
|
// authBasicRequest(credentials)
|
|
tokenBasicRequest(credentials.token)
|
|
.get(uri"http://localhost:8080/api/chat/get/users")
|
|
.response(asJson[List[String]])
|
|
.send()
|
|
}
|
|
}
|
|
|
|
def getActiveUsers(credentials: UserCredentials) =
|
|
// authBasicRequest(credentials)
|
|
tokenBasicRequest(credentials.token)
|
|
.get(uri"http://localhost:8080/api/chat/get/active-users")
|
|
.response(asJson[List[ActiveUser]])
|
|
.send()
|
|
|
|
}
|
|
|
|
final case class HttpBinResponse(
|
|
url: String,
|
|
origin: String,
|
|
headers: Map[String, String]
|
|
)
|
|
|
|
final case class ActiveUser(
|
|
userName: String,
|
|
online: Boolean,
|
|
lastActive: Option[ZonedDateTime]
|
|
)
|
|
|
|
object ActiveUser {
|
|
def empty = ActiveUser("empty", false, None)
|
|
}
|