diff options
author | Adam Warski <adam@warski.org> | 2017-08-30 13:40:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-30 13:40:55 +0200 |
commit | 18d3ae2a0c2c7b73b747004127d7362edfbeee8c (patch) | |
tree | d75f864ffbc5f484d9ceb29dbd2980d6581560eb /okhttp-client-handler | |
parent | e7297b85f79d6531cb487550994dbde277779f9b (diff) | |
parent | 6f878ed7e1bb6e4e8b3a83944b4967839af16a17 (diff) | |
download | sttp-18d3ae2a0c2c7b73b747004127d7362edfbeee8c.tar.gz sttp-18d3ae2a0c2c7b73b747004127d7362edfbeee8c.tar.bz2 sttp-18d3ae2a0c2c7b73b747004127d7362edfbeee8c.zip |
Merge pull request #31 from aeons/master
Close Ok Http bodies
Diffstat (limited to 'okhttp-client-handler')
2 files changed, 18 insertions, 5 deletions
diff --git a/okhttp-client-handler/monix/src/main/scala/com/softwaremill/sttp/okhttp/monix/OkHttpMonixClientHandler.scala b/okhttp-client-handler/monix/src/main/scala/com/softwaremill/sttp/okhttp/monix/OkHttpMonixClientHandler.scala index 8bce372..e46e0b9 100644 --- a/okhttp-client-handler/monix/src/main/scala/com/softwaremill/sttp/okhttp/monix/OkHttpMonixClientHandler.scala +++ b/okhttp-client-handler/monix/src/main/scala/com/softwaremill/sttp/okhttp/monix/OkHttpMonixClientHandler.scala @@ -32,7 +32,10 @@ class OkHttpMonixClientHandler private (client: OkHttpClient)( override def responseBodyToStream( res: okhttp3.Response): Try[Observable[ByteBuffer]] = Success( - Observable.fromInputStream(res.body().byteStream()).map(ByteBuffer.wrap)) + Observable + .fromInputStream(res.body().byteStream()) + .map(ByteBuffer.wrap) + .doAfterTerminate(_ => res.close())) private def toIterable[T](observable: Observable[T])( implicit s: Scheduler): Iterable[T] = diff --git a/okhttp-client-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala b/okhttp-client-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala index 9bc865e..022e3f1 100644 --- a/okhttp-client-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala +++ b/okhttp-client-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala @@ -101,14 +101,24 @@ abstract class OkHttpClientHandler[R[_], S](client: OkHttpClient) new EagerResponseHandler[S] { override def handleBasic[T](bra: BasicResponseAs[T, S]): Try[T] = bra match { - case IgnoreResponse => Try(res.body().close()) + case IgnoreResponse => + Try(res.close()) case ResponseAsString(encoding) => - Try(res.body().source().readString(Charset.forName(encoding))) - case ResponseAsByteArray => Try(res.body().bytes()) + val body = Try( + res.body().source().readString(Charset.forName(encoding))) + res.close() + body + case ResponseAsByteArray => + val body = Try(res.body().bytes()) + res.close() + body case ras @ ResponseAsStream() => responseBodyToStream(res).map(ras.responseIsStream) case ResponseAsFile(file, overwrite) => - Try(ResponseAs.saveFile(file, res.body().byteStream(), overwrite)) + val body = Try( + ResponseAs.saveFile(file, res.body().byteStream(), overwrite)) + res.close() + body } } |