aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoradamw <adam@warski.org>2017-09-04 17:00:10 +0200
committeradamw <adam@warski.org>2017-09-04 17:00:10 +0200
commit46ee0318823f79546d2b81722f4c13ecafa65a7e (patch)
tree4b27ce7c6524d1dc93dfb12250a7ebbe92feb615
parent52e0d0440ea2f8689353c04f605f08628e3b5e20 (diff)
downloadsttp-46ee0318823f79546d2b81722f4c13ecafa65a7e.tar.gz
sttp-46ee0318823f79546d2b81722f4c13ecafa65a7e.tar.bz2
sttp-46ee0318823f79546d2b81722f4c13ecafa65a7e.zip
Only close the okhttp client if it was created by the handler
-rw-r--r--okhttp-handler/monix/src/main/scala/com/softwaremill/sttp/okhttp/monix/OkHttpMonixHandler.scala23
-rw-r--r--okhttp-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala54
2 files changed, 55 insertions, 22 deletions
diff --git a/okhttp-handler/monix/src/main/scala/com/softwaremill/sttp/okhttp/monix/OkHttpMonixHandler.scala b/okhttp-handler/monix/src/main/scala/com/softwaremill/sttp/okhttp/monix/OkHttpMonixHandler.scala
index bcca7c4..31f4546 100644
--- a/okhttp-handler/monix/src/main/scala/com/softwaremill/sttp/okhttp/monix/OkHttpMonixHandler.scala
+++ b/okhttp-handler/monix/src/main/scala/com/softwaremill/sttp/okhttp/monix/OkHttpMonixHandler.scala
@@ -16,8 +16,11 @@ import okio.BufferedSink
import scala.concurrent.Future
import scala.util.{Failure, Success, Try}
-class OkHttpMonixHandler private (client: OkHttpClient)(implicit s: Scheduler)
- extends OkHttpAsyncHandler[Task, Observable[ByteBuffer]](client, TaskMonad) {
+class OkHttpMonixHandler private (client: OkHttpClient, closeClient: Boolean)(
+ implicit s: Scheduler)
+ extends OkHttpAsyncHandler[Task, Observable[ByteBuffer]](client,
+ TaskMonad,
+ closeClient) {
override def streamToRequestBody(
stream: Observable[ByteBuffer]): Option[OkHttpRequestBody] =
@@ -77,11 +80,19 @@ class OkHttpMonixHandler private (client: OkHttpClient)(implicit s: Scheduler)
}
object OkHttpMonixHandler {
- def apply(
- okhttpClient: OkHttpClient = OkHttpHandler.buildClientNoRedirects())(
- implicit s: Scheduler = Scheduler.Implicits.global)
+ private def apply(client: OkHttpClient, closeClient: Boolean)(
+ implicit s: Scheduler): SttpHandler[Task, Observable[ByteBuffer]] =
+ new FollowRedirectsHandler(new OkHttpMonixHandler(client, closeClient)(s))
+
+ def apply()(implicit s: Scheduler = Scheduler.Implicits.global)
+ : SttpHandler[Task, Observable[ByteBuffer]] =
+ OkHttpMonixHandler(OkHttpHandler.buildClientNoRedirects(),
+ closeClient = true)(s)
+
+ def usingClient(client: OkHttpClient)(implicit s: Scheduler =
+ Scheduler.Implicits.global)
: SttpHandler[Task, Observable[ByteBuffer]] =
- new FollowRedirectsHandler(new OkHttpMonixHandler(okhttpClient)(s))
+ OkHttpMonixHandler(client, closeClient = false)(s)
}
private[monix] object TaskMonad extends MonadAsyncError[Task] {
diff --git a/okhttp-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala b/okhttp-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala
index ab2b844..2250859 100644
--- a/okhttp-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala
+++ b/okhttp-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala
@@ -24,7 +24,8 @@ import scala.concurrent.{ExecutionContext, Future}
import scala.language.higherKinds
import scala.util.{Failure, Try}
-abstract class OkHttpHandler[R[_], S](client: OkHttpClient)
+abstract class OkHttpHandler[R[_], S](client: OkHttpClient,
+ closeClient: Boolean)
extends SttpHandler[R, S] {
private[okhttp] def convertRequest[T](request: Request[T, S]): OkHttpRequest = {
val builder = new OkHttpRequest.Builder()
@@ -135,7 +136,9 @@ abstract class OkHttpHandler[R[_], S](client: OkHttpClient)
def responseBodyToStream(res: OkHttpResponse): Try[S] =
Failure(new IllegalStateException("Streaming isn't supported"))
- override def close(): Unit = client.dispatcher().executorService().shutdown()
+ override def close(): Unit = if (closeClient) {
+ client.dispatcher().executorService().shutdown()
+ }
}
object OkHttpHandler {
@@ -146,8 +149,8 @@ object OkHttpHandler {
.build()
}
-class OkHttpSyncHandler private (client: OkHttpClient)
- extends OkHttpHandler[Id, Nothing](client) {
+class OkHttpSyncHandler private (client: OkHttpClient, closeClient: Boolean)
+ extends OkHttpHandler[Id, Nothing](client, closeClient) {
override def send[T](r: Request[T, Nothing]): Response[T] = {
val request = convertRequest(r)
val response = client.newCall(request).execute()
@@ -158,14 +161,23 @@ class OkHttpSyncHandler private (client: OkHttpClient)
}
object OkHttpSyncHandler {
- def apply(okhttpClient: OkHttpClient = OkHttpHandler.buildClientNoRedirects())
- : SttpHandler[Id, Nothing] =
- new FollowRedirectsHandler[Id, Nothing](new OkHttpSyncHandler(okhttpClient))
+ private def apply(client: OkHttpClient,
+ closeClient: Boolean): SttpHandler[Id, Nothing] =
+ new FollowRedirectsHandler[Id, Nothing](
+ new OkHttpSyncHandler(client, closeClient))
+
+ def apply(): SttpHandler[Id, Nothing] =
+ OkHttpSyncHandler(OkHttpHandler.buildClientNoRedirects(),
+ closeClient = true)
+
+ def usingClient(client: OkHttpClient): SttpHandler[Id, Nothing] =
+ OkHttpSyncHandler(client, closeClient = false)
}
abstract class OkHttpAsyncHandler[R[_], S](client: OkHttpClient,
- rm: MonadAsyncError[R])
- extends OkHttpHandler[R, S](client) {
+ rm: MonadAsyncError[R],
+ closeClient: Boolean)
+ extends OkHttpHandler[R, S](client, closeClient) {
override def send[T](r: Request[T, S]): R[Response[T]] = {
val request = convertRequest(r)
@@ -189,15 +201,25 @@ abstract class OkHttpAsyncHandler[R[_], S](client: OkHttpClient,
override def responseMonad: MonadError[R] = rm
}
-class OkHttpFutureHandler private (client: OkHttpClient)(
+class OkHttpFutureHandler private (client: OkHttpClient, closeClient: Boolean)(
implicit ec: ExecutionContext)
- extends OkHttpAsyncHandler[Future, Nothing](client, new FutureMonad) {}
+ extends OkHttpAsyncHandler[Future, Nothing](client,
+ new FutureMonad,
+ closeClient) {}
object OkHttpFutureHandler {
- def apply(
- okhttpClient: OkHttpClient = OkHttpHandler.buildClientNoRedirects())(
- implicit ec: ExecutionContext = ExecutionContext.Implicits.global)
- : SttpHandler[Future, Nothing] =
+ private def apply(client: OkHttpClient, closeClient: Boolean)(
+ implicit ec: ExecutionContext): SttpHandler[Future, Nothing] =
new FollowRedirectsHandler[Future, Nothing](
- new OkHttpFutureHandler(okhttpClient))
+ new OkHttpFutureHandler(client, closeClient))
+
+ def apply()(implicit ec: ExecutionContext = ExecutionContext.Implicits.global)
+ : SttpHandler[Future, Nothing] =
+ OkHttpFutureHandler(OkHttpHandler.buildClientNoRedirects(),
+ closeClient = true)
+
+ def usingClient(client: OkHttpClient)(implicit ec: ExecutionContext =
+ ExecutionContext.Implicits.global)
+ : SttpHandler[Future, Nothing] =
+ OkHttpFutureHandler(client, closeClient = false)
}