Skip to content

Instantly share code, notes, and snippets.

@hamnis
Created October 22, 2014 17:46
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 hamnis/9962423a14a7d3d818db to your computer and use it in GitHub Desktop.
Save hamnis/9962423a14a7d3d818db to your computer and use it in GitHub Desktop.
package emsnav
import dispatch._, Defaults._
import java.net.URI
import net.hamnaberg.json.collection._
import concurrent.Await
import concurrent.duration._
object Main extends App {
val MediaType = "application/vnd.collection+json"
val eventList = for {
rootOpt <- collectionRequest(URI.create(args.head))
events <- (for {
root <- rootOpt
link <- root.findLinkByRel("event collection")
} yield link.follow()).getOrElse(Future.successful(None))
} yield events
val events = Await.result(eventList, 5.seconds)
println(events)
def collectionRequest(_uri: URI): Future[Option[JsonCollection]] = {
for {
res <- Http(uri(_uri) <:< Map("Accept" -> MediaType))
} yield {
if (res.getContentType == MediaType) {
NativeJsonCollectionParser.parseCollection(res.getResponseBodyAsStream()).right.toOption
} else None
}
}
object uri extends (URI => Req) {
def apply(uri: URI) = {
Req(_.setUrl(uri.toString))
}
}
implicit class LinkOps(val link: Link) extends AnyVal {
def follow(): Future[Option[JsonCollection]] = collectionRequest(link.href)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment