Skip to content

Instantly share code, notes, and snippets.

@isterin
Created July 9, 2017 03:21
Show Gist options
  • Save isterin/c920a488f9beaf5f31858aaa6eb52a58 to your computer and use it in GitHub Desktop.
Save isterin/c920a488f9beaf5f31858aaa6eb52a58 to your computer and use it in GitHub Desktop.
Scala automatic wrapper/delegation with implicits
class ProxiedHttpRequest(val req:HttpRequest) {
lazy val proxyServer = InetAddress.getByName(“some.proxy.com”)
}
object ProxiedHttpRequest {
implicit def delegateToOriginalHttpRequest(
r: ProxiedHttpRequest):HttpRequest = r.req
}
object Application {
def proxyRequest(req:ProxiedHttpRequest) {
val where = req.proxyServer
val headers = req.headers() // No more req.req ugliness
}
}
class ProxiedHttpRequest(val req:HttpRequest) {
lazy val proxyServer = InetAddress.getByName(“some.proxy.com”)
}
def proxyRequest(req:ProxiedHttpRequest) {
val where = req.proxyServer
/*
Here I need access to original request being wrapped to get the headers This is ugly and I’d prefer to access it as I would a regular HttpRequest req.headers()
*/
val headers = req.req.headers()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment