pheymann / typedapi

Build your web API on the type level.
MIT License
161 stars 10 forks source link

Improve ammonite support #33

Closed pheymann closed 6 years ago

pheymann commented 6 years ago

Right now you have to do the following things to get a client in Ammonite:

import $ivy.`com.github.pheymann::typedapi-scalaj-http-client:0.1.0`
import $ivy.`org.scalaj::scalaj-http:2.4.1`

import typedapi._
import client._
import util._
import scalajhttp._
import scalaj.http.Http

val Api = api(Get[Json, User], Root)

val get = derive(Api)
val cm = ClientManager(Http, "host", 80)

implicit val decoder = Decoder[Blocking](raw => ??? // to User)

val response = get().run[Blockling].raw(cm) // Right(HttpResponse...)

You have to create a Decoder as ApiRequest combines both raw and decoded action but retrieves the decoder at the construction level. Furthermore, you have to use the Blocking[A] effect which is Either[Exception, A] even though the result will always be a Right.

With this PR this code reduces to:

import $ivy.`com.github.pheymann::typedapi-ammonite-client:0.2.0`

import typedapi._
import client._
import amm._

val Api = api(Get[Json, User], Root)

val get = derive(Api)
val cm = clientManager("host", 80)

val response = get().run[Id].raw(cm) // HttpResponse...

It is three imports less and you can access the response directly.