Created
September 10, 2017 12:52
-
-
Save d10xa/d73cd089ce03cf96af93b1ec15ed3cac to your computer and use it in GitHub Desktop.
Akka. Support for HTTP(S) proxies with Authorization
This file contains 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
import java.net.InetSocketAddress | |
import akka.actor.ActorSystem | |
import akka.http.scaladsl.ClientTransport | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.model.HttpRequest | |
import akka.http.scaladsl.model.headers | |
import akka.http.scaladsl.settings.ConnectionPoolSettings | |
import akka.http.scaladsl.unmarshalling.Unmarshal | |
import akka.stream.ActorMaterializer | |
import com.typesafe.config.ConfigFactory | |
import scala.util.Failure | |
import scala.util.Success | |
object AkkaHttpProxyCredentialsMain { | |
def main(args: Array[String]): Unit = { | |
implicit val system = ActorSystem("akka-proxy-auth") | |
implicit val mat = ActorMaterializer() | |
import system.dispatcher | |
val config = ConfigFactory.load().getConfig("proxy") | |
val host = config.getString("host") | |
val port = config.getInt("port") | |
val proxyAddress = InetSocketAddress.createUnresolved(host, port) | |
val username = config.getString("username") | |
val password = config.getString("password") | |
val auth = headers.BasicHttpCredentials(username, password) | |
val httpsProxyTransport = ClientTransport.httpsProxy(proxyAddress, auth) | |
val settings = ConnectionPoolSettings(system).withTransport(httpsProxyTransport) | |
val result = for { | |
response <- Http().singleRequest(HttpRequest(uri = "http://ifconfig.co/json"), settings = settings) | |
json <- Unmarshal(response.entity).to[String] | |
_ <- Http().shutdownAllConnectionPools() | |
_ <- system.terminate() | |
} yield json | |
result onComplete { | |
case Success(json) => | |
println(json) | |
case Failure(e) => | |
println(e.getMessage) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment