Skip to content

Instantly share code, notes, and snippets.

@penland365
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save penland365/0b2bcc4b019c299090c8 to your computer and use it in GitHub Desktop.
Save penland365/0b2bcc4b019c299090c8 to your computer and use it in GitHub Desktop.
Super basic finch-client type layout.
package io.finch
import argonaut._, Argonaut._
import com.twitter.finagle.httpx.path.Path
package object client {
implicit def decodeStrResource: DecodeResource[String] = new DecodeResource[String] {
def apply(r: Resource): String = r.body
}
implicit def decodeJsonResource: DecodeResource[Json] = new DecodeResource[Json] {
def apply(r: Resource): Json = r.body.parseOption.getOrElse(jEmptyObject)
}
case class Resource(body: String) {
def as[A](implicit d: DecodeResource[A]): A = d(this)
}
trait DecodeResource[A] {
def apply(resource: Resource): A
}
case class FinchRequest(conn: Connection, path: Path)
}
val fReq = FinchRequest(Connection("api.github.com"), Path("/users/penland365"))
val result = Get(fReq).as[String]
val result = Get(fReq).as[Json]
package io.finch
package client
sealed trait Verb extends Service[FinchRequest, Resource] {
def apply(fReq: FinchRequest): Future[Resource] = {
val req = RequestBuilder()
.url("https://" + fReq.conn.host + fReq.path)
.addHeader("Host", fReq.conn.host)
.addHeader("User-Agent", "finch/0.7.0")
.build(httpVerb(), None)
fReq.conn(req) map { r =>
val body = Utf8.unapply(r.content) match {
case Some(x) => x
case None => ""
}
new Resource(body)
}
}
private[this] def httpVerb(): Method = this match {
case Get => Method.Get
}
}
case object Get extends Verb
case class Connection(host: String, port: Int = 443) extends Service[Request, Response] {
val client = new Httpx.Client()
.withTlsWithoutValidation()
.newService(host + ":" + port.toString, host)
def apply(req: Request): Future[Response] = client(req)
}
@penland365
Copy link
Author

The next step I'm working through on this is to have the Verb Service return a type Result = Future[Either[String, Resource]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment