Skip to content

Instantly share code, notes, and snippets.

@kalexander
Last active August 29, 2015 14:22
Show Gist options
  • Save kalexander/527650fcf216c33f287d to your computer and use it in GitHub Desktop.
Save kalexander/527650fcf216c33f287d to your computer and use it in GitHub Desktop.
simple proxying
PROBLEM:
The Product Importer has lots of crazy report formats that vary wildly from report to report. To reduce
the strain on requiring TWIKI to model these in JSON marshallers/unmarshallers, I have created a happy
path proxy scenario that just passes along the response of a request.
This is using the NEWMAN framework https://github.com/stackmob/newman.. The TwikiService
has an existing example of the use of Newman.
In a route, add something like:
import spray.http.ContentTypes._
...
} ~ path("someproxypath"){
get{
complete{
// url you need to proxy to
val url = new URL("https://api.github.com/users/mralexgray/repos")
val resp = Await.result(GET(url).apply, 20.second)
HttpEntity(`application/json`, resp.bodyString)
}
}
}
You might want to wrap in a future to make this non-blocking. Feel free to modify the route to add auth,
handle non-happy scenarios, etc. But this is the bulk of the work!
} ~ path("extra"){
get{
complete{
val url = new URL("https://api.github.com/users/mralexgray/repos")
val resp = Await.result(GET(url).apply, 20.second)
resp.code match {
case HttpResponseCode.Ok => HttpEntity(`application/json`, resp.bodyString)
case _ => throw new Exception("this is a travesty!")
}
}
}
}
enjoy,
-kevin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment