aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Warski <adam@warski.org>2017-10-31 15:48:03 +0100
committerGitHub <noreply@github.com>2017-10-31 15:48:03 +0100
commita2a6db30bceb7a798ad36673cfcbe61088c7083f (patch)
tree83a00429861386cdf5bacbf29ae7896b64ac8d74
parentae0d7f698c407413339b45067f959f611fbe90ef (diff)
parent8854c9c6d0f21df00875464c9d969575c4e91c54 (diff)
downloadsttp-a2a6db30bceb7a798ad36673cfcbe61088c7083f.tar.gz
sttp-a2a6db30bceb7a798ad36673cfcbe61088c7083f.tar.bz2
sttp-a2a6db30bceb7a798ad36673cfcbe61088c7083f.zip
Merge pull request #39 from atais/master
Loading system proxy values by default
-rw-r--r--core/src/main/scala/com/softwaremill/sttp/SttpBackendOptions.scala37
-rw-r--r--docs/conf/proxy.rst11
-rw-r--r--docs/credits.rst1
-rw-r--r--project/build.properties2
4 files changed, 45 insertions, 6 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/SttpBackendOptions.scala b/core/src/main/scala/com/softwaremill/sttp/SttpBackendOptions.scala
index dbf64b2..026d4d0 100644
--- a/core/src/main/scala/com/softwaremill/sttp/SttpBackendOptions.scala
+++ b/core/src/main/scala/com/softwaremill/sttp/SttpBackendOptions.scala
@@ -2,9 +2,11 @@ package com.softwaremill.sttp
import java.net.InetSocketAddress
-import scala.concurrent.duration._
import com.softwaremill.sttp.SttpBackendOptions._
+import scala.concurrent.duration._
+import scala.util.Try
+
case class SttpBackendOptions(
connectionTimeout: FiniteDuration,
proxy: Option[Proxy]
@@ -28,6 +30,7 @@ object SttpBackendOptions {
sealed trait ProxyType {
def asJava: java.net.Proxy.Type
}
+
object ProxyType {
case object Http extends ProxyType {
override def asJava: java.net.Proxy.Type = java.net.Proxy.Type.HTTP
@@ -37,12 +40,38 @@ object SttpBackendOptions {
}
}
- val Default: SttpBackendOptions = SttpBackendOptions(30.seconds, None)
+ private val Empty: SttpBackendOptions =
+ SttpBackendOptions(30.seconds, None)
+
+ val Default: SttpBackendOptions =
+ Empty.copy(proxy = loadSystemProxy)
def connectionTimeout(ct: FiniteDuration): SttpBackendOptions =
Default.connectionTimeout(ct)
+
def httpProxy(host: String, port: Int): SttpBackendOptions =
- Default.httpProxy(host, port)
+ Empty.httpProxy(host, port)
+
def socksProxy(host: String, port: Int): SttpBackendOptions =
- Default.socksProxy(host, port)
+ Empty.socksProxy(host, port)
+
+ private def loadSystemProxy: Option[Proxy] = {
+ def system(hostProp: String,
+ portProp: String,
+ make: (String, Int) => Proxy,
+ defaultPort: Int) = {
+ val host = Option(System.getProperty(hostProp))
+ def port = Try(System.getProperty(portProp).toInt).getOrElse(defaultPort)
+ host.map(make(_, port))
+ }
+
+ def proxy(t: ProxyType)(host: String, port: Int) = Proxy(host, port, t)
+
+ import ProxyType._
+ val socks = system("socksProxyHost", "socksProxyPort", proxy(Socks), 1080)
+ val http = system("http.proxyHost", "http.proxyPort", proxy(Http), 80)
+
+ Seq(socks, http).find(_.isDefined).flatten
+ }
+
}
diff --git a/docs/conf/proxy.rst b/docs/conf/proxy.rst
index 85b140a..bcb1b3a 100644
--- a/docs/conf/proxy.rst
+++ b/docs/conf/proxy.rst
@@ -1,7 +1,16 @@
Proxy support
=============
-A proxy can be specified when creating a backend::
+sttp library by default checks for your System proxy properties (`docs <https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html>`_):
+
+Following settings are checked:
+
+1. ``socksProxyHost`` and ``socksProxyPort`` *(default: 1080)*
+2. ``http.proxyHost`` and ``http.proxyPort`` *(default: 80)*
+
+Settings are loaded **in given order** and the **first existing value** is being used.
+
+Otherwise, proxy values can be specified manually when creating a backend::
import com.softwaremill.sttp._
diff --git a/docs/credits.rst b/docs/credits.rst
index 29d733c..233d885 100644
--- a/docs/credits.rst
+++ b/docs/credits.rst
@@ -9,3 +9,4 @@ Credits
* `Piotr Gabara <https://github.com/bhop>`_
* `Gabriele Petronella <https://github.com/gabro>`_
* `Paweł Stawicki <https://github.com/amorfis>`_
+* `Michał Siatkowski <https://github.com/atais>`_
diff --git a/project/build.properties b/project/build.properties
index 369929b..ee9d7cd 100644
--- a/project/build.properties
+++ b/project/build.properties
@@ -1 +1 @@
-sbt.version=1.0.2 \ No newline at end of file
+sbt.version=1.0.3 \ No newline at end of file