Last active
August 29, 2015 14:22
-
-
Save jnd71/0b2bcc4b019c299090c8 to your computer and use it in GitHub Desktop.
Super basic finch-client type layout.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val fReq = FinchRequest(Connection("api.github.com"), Path("/users/penland365")) | |
val result = Get(fReq).as[String] | |
val result = Get(fReq).as[Json] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The next step I'm working through on this is to have the
Verb Service
return atype Result = Future[Either[String, Resource]]