Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

request verbs

  • Loading branch information...
commit c96584b97f09bd1d8fd2c96ea7e288a14885248f 0 parents
Nathan Hamblen n8han authored
9 .gitignore
@@ -0,0 +1,9 @@
+.classpath
+.project
+.settings
+target
+scratch.scala
+src_managed
+*.test.properties
+.idea
+*.iml
3  build.sbt
@@ -0,0 +1,3 @@
+libraryDependencies ++= Seq(
+ "com.ning" % "async-http-client" % "1.6.5"
+)
1  src/main/scala/package.scala
@@ -0,0 +1 @@
+package object dispatch extends ImplicitRequestVerbs
45 src/main/scala/requests.scala
@@ -0,0 +1,45 @@
+package dispatch
+
+import com.ning.http.client.RequestBuilder
+
+object ImplicitRequestVerbs extends ImplicitRequestVerbs
+
+trait ImplicitRequestVerbs {
+ implicit def implyRequestVerbs(builder: RequestBuilder) =
+ new RequestVerbs(builder)
+}
+
+class RequestVerbs(val subject: RequestBuilder)
+extends MethodVerbs with PathVerbs with ParamVerbs
+
+object :/ {
+ def apply(host: String) =
+ new RequestBuilder().setUrl("http://%s/".format(host))
+ def apply(host: String, port: Int) =
+ new RequestBuilder().setUrl("http://%s:%d/".format(host, port))
+}
+
+trait MethodVerbs {
+ def subject: RequestBuilder
+ def HEAD = subject.setMethod("HEAD")
+ def GET = subject.setMethod("GET")
+ def POST = subject.setMethod("POST")
+ def PUT = subject.setMethod("PUT")
+ def DELETE = subject.setMethod("DELETE")
+}
+
+trait PathVerbs {
+ def subject: RequestBuilder
+ def / (path: String) = subject.setUrl(
+ java.net.URI.create(subject.build.getUrl()).resolve(path).toString
+ )
+}
+
+trait ParamVerbs {
+ def subject: RequestBuilder
+ def << (params: Traversable[(String,String)]) =
+ (subject.setMethod("POST") /: params) {
+ case (s, (key, value)) =>
+ s.addParameter(key, value)
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.