Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First set of third-party additions. #14

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions third-party/build.sbt
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,9 @@
name := "third-party"

description :=
"Third party client library to give easy use of dispatch for common services."

libraryDependencies ++= Seq(
"net.databinder.dispatch" %% "core" % "0.9.0",
"org.specs2" %% "specs2" % "1.9" % "test"
)
37 changes: 37 additions & 0 deletions third-party/src/main/scala/foursquare.scala
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,37 @@
package dispatch.clients

import dispatch._
import com.ning.http.client.RequestBuilder

class Foursquare(params: (String, String)*) extends FoursquareHelper {
protected val svc = "https://api.foursquare.com"
protected var urlParams = Map[String, String]()
protected var version = ""

urlParams ++= params.toList
}

object Foursquare {
def apply(creds: OAuth2Creds) =
new Foursquare("client_id" -> creds.client_key, "client_secret" -> creds.client_secret)
}

trait FoursquareHelper {
this: Foursquare =>

def setVersion(newVersion: String) = {
version = "/" + newVersion
}

def call(path: String, params: Traversable[(String, String)] = List()) = {
executeHttp(buildUri(path, params))
}

def buildUri(fullPath: String, params: Traversable[(String, String)]) = {
url(svc + version + fullPath) <<? (urlParams ++ params)
}

def executeHttp(uri: RequestBuilder) = {
Http(uri OK as.String)
}
}
6 changes: 6 additions & 0 deletions third-party/src/main/scala/models.scala
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,6 @@
package dispatch.clients

import dispatch._

sealed trait OAuth
case class OAuth2Creds(client_key: String, client_secret: String) extends OAuth
42 changes: 42 additions & 0 deletions third-party/src/test/scala/foursquare.scala
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,42 @@
package dispatch.clients

import dispatch._
import org.specs2.mutable.Specification
import org.specs2.specification.Scope

object FoursquareSpec extends Specification {

"A basic foursquare request that requires auth" should {
"be able to be formed normally" in new foursquareContext {
val request = fsq.buildUri("/venues/search", Map("q" -> "drinks"))
val builtReq = request.build().getUrl()
println(builtReq)

builtReq.contains("https://") must beTrue
builtReq.contains("api.foursquare.com/v2") must beTrue
builtReq.contains("/venues/search") must beTrue
builtReq.contains("client_id=aa") must beTrue
builtReq.contains("client_secret=bb") must beTrue
builtReq.contains("q=drinks") must beTrue
}

"executing a request should give us back a standard http code" in new foursquareContext {
val request = for {
req <- fsq.call("/venues/search", Map("q" -> "drinks"))
} yield (req)

request onComplete {
case ret =>
isInstanceOf[Some[_]] must beTrue.eventually
}
}
}

}

trait foursquareContext extends Scope {
val creds = OAuth2Creds("aa", "bb")
val fsq = Foursquare(creds)
fsq.setVersion("v2")
}