在http4s后端和Binding.scala前端之间进行通信

时间:2017-06-20 01:04:01

标签: scala scala.js http4s binding.scala

我正在尝试将Binding.scala与现有的http4s后端服务一起使用,但有点失去了它们如何组合在一起。我不确定如何用Binding.scala“绑定”说fs2 Task或cat-effect IO

1 个答案:

答案 0 :(得分:0)

我从未使用过http4s,但提示可能是FutureBinding

只要你有一个未来,你可以很好地使用它:

这是我调用异步webservice(ajax-call)的例子:

  @dom def afClients(): Binding[HTMLElement] = {
    val apiPath = s"/calendar/afClients"

    FutureBinding(Ajax.get(apiPath))
      .bind match {
      case None =>
        <div class="ui active inverted dimmer front">
          <div class="ui large text loader">Loading</div>
        </div>
      case Some(Success(response)) =>
        val json = Json.parse(response.responseText)
        info(s"Json received List[AFClientConfig]: ${json.toString().take(20)}")
        json.validate[List[AFClientConfig]] match {
          case JsSuccess(u, _) =>
            changeAFClients(u)
            <div>
            </div>
          case JsError(errors) =>
            <div>
              {s"Problem parsing User: ${errors.map(e => s"${e._1} -> ${e._2}")}"}
            </div>
        }

      case Some(Failure(exception)) =>
        error(exception, s"Problem accessing $apiPath")
        <div>
          {exception.getMessage}
        </div>
    }
  }