Created
October 22, 2014 17:46
-
-
Save hamnis/9962423a14a7d3d818db to your computer and use it in GitHub Desktop.
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 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