{"action":"created","comment":{"url":"https://api.github.com/repos/softwaremill/sttp/pulls/comments/132976012","pull_request_review_id":56107138,"id":132976012,"diff_hunk":"@@ -0,0 +1,79 @@\n+package com.softwaremill.sttp.okhttp.monix\n+\n+import java.nio.ByteBuffer\n+\n+import com.softwaremill.sttp._\n+import com.softwaremill.sttp.okhttp.OkHttpAsyncClientHandler\n+import monix.eval.Task\n+import monix.execution.{Cancelable, Scheduler}\n+import monix.reactive.{Consumer, Observable}\n+import okhttp3.{MediaType, OkHttpClient, RequestBody => OkHttpRequestBody}\n+import okio.BufferedSink\n+\n+import scala.concurrent.Await\n+import scala.concurrent.duration.Duration\n+import scala.language.higherKinds\n+import scala.util.{Failure, Success, Try}\n+\n+/**\n+  * Created by omainegra on 8/4/17.\n+  */\n+class OkHttpMonixClientHandler private (client: OkHttpClient)(\n+    implicit s: Scheduler)\n+    extends OkHttpAsyncClientHandler[Task, Observable[ByteBuffer]](client,\n+                                                                   TaskMonad) {\n+\n+  private lazy val io = Scheduler.io(\"sttp-monix-io\")\n+\n+  override def streamToRequestBody(\n+      stream: Observable[ByteBuffer]): Option[OkHttpRequestBody] =\n+    Some(new OkHttpRequestBody() {\n+      override def writeTo(sink: BufferedSink): Unit = {\n+        val f = stream\n+          .consumeWith(\n+            Consumer.foreach(chunk => sink.write(chunk.array()))\n+          )\n+          .runAsync(io)\n+\n+        // We could safely block until the observable is consumed because OkHttp execute\n+        // this method asynchronous in another ThreadPool.\n+        Await.ready(f, Duration.Inf)","path":"okhttp-client-handler/monix/src/main/scala/com/softwaremill/sttp/okhttp/monix/OkHttpMonixClientHandler.scala","position":40,"original_position":40,"commit_id":"c6c1f5a34930946e8ab4e9248b9255ce2e1464fe","original_commit_id":"c6c1f5a34930946e8ab4e9248b9255ce2e1464fe","user":{"login":"omainegra","id":7846263,"avatar_url":"https://avatars3.githubusercontent.com/u/7846263?v=4","gravatar_id":"","url":"https://api.github.com/users/omainegra","html_url":"https://github.com/omainegra","followers_url":"https://api.github.com/users/omainegra/followers","following_url":"https://api.github.com/users/omainegra/following{/other_user}","gists_url":"https://api.github.com/users/omainegra/gists{/gist_id}","starred_url":"https://api.github.com/users/omainegra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/omainegra/subscriptions","organizations_url":"https://api.github.com/users/omainegra/orgs","repos_url":"https://api.github.com/users/omainegra/repos","events_url":"https://api.github.com/users/omainegra/events{/privacy}","received_events_url":"https://api.github.com/users/omainegra/received_events","type":"User","site_admin":false},"body":"I improve this part with suggestion from Alexandru in Monix gitter channel. The tip was convert the `Observable` to an `Iterable`. About the blocking part that's not correct. The thread that call `send` is never blocked because `OkHttp` internally run that code in another thread pool. Please check this example I've prepared.\r\n\r\n```scala\r\n// Start HttpServer\r\nimplicit val actorSystem: ActorSystem = ActorSystem()\r\nimplicit val materializer = ActorMaterializer()\r\n\r\nval serverRoutes: Route =\r\n  path(\"echo\") {\r\n    post {\r\n      entity(as[String])(body => {\r\n        complete(body)\r\n      })\r\n    }\r\n  }\r\n\r\nval endpoint = uri\"http://localhost:31532\"\r\nAwait.ready(\r\n  Http().bindAndHandle(serverRoutes, endpoint.host, endpoint.port.get),\r\n  Duration.Inf)\r\n\r\n// Run Demo\r\nimplicit val scheduler = Scheduler.io(\"io\")\r\nimplicit val handler = OkHttpMonixClientHandler()\r\n\r\nval body = \"sample string\"\r\nval observable = Observable\r\n  .fromIterable(body.getBytes(\"utf-8\"))\r\n  .map(byte => ByteBuffer.wrap(Array(byte)))\r\n  .delayOnNext(1.second)\r\n  .doOnNext(byte => println2(s\"Item produced: $byte\"))\r\n\r\nval request = sttp.post(uri\"$endpoint/echo\").streamBody(observable)\r\nval response = request.response(asString).send().runAsync\r\n\r\nwhile (!response.isCompleted) {\r\n  println2(s\"Still waiting ...\")\r\n  Thread.sleep(1000)\r\n}\r\nprintln2(s\"Got response: ${response.value.get.get.body}\")\r\n\r\nAwait.ready(actorSystem.terminate(), Duration.Inf)\r\nassert(response.value.get.get.body == body)\r\n```","created_at":"2017-08-14T15:08:37Z","updated_at":"2017-08-14T15:08:37Z","html_url":"https://github.com/softwaremill/sttp/pull/24#discussion_r132976012","pull_request_url":"https://api.github.com/repos/softwaremill/sttp/pulls/24","_links":{"self":{"href":"https://api.github.com/repos/softwaremill/sttp/pulls/comments/132976012"},"html":{"href":"https://github.com/softwaremill/sttp/pull/24#discussion_r132976012"},"pull_request":{"href":"https://api.github.com/repos/softwaremill/sttp/pulls/24"}}},"pull_request":{"url":"https://api.github.com/repos/softwaremill/sttp/pulls/24","id":134268796,"html_url":"https://github.com/softwaremill/sttp/pull/24","diff_url":"https://github.com/softwaremill/sttp/pull/24.diff","patch_url":"https://github.com/softwaremill/sttp/pull/24.patch","issue_url":"https://api.github.com/repos/softwaremill/sttp/issues/24","number":24,"state":"open","locked":false,"title":"Okhttp3 monix","user":{"login":"omainegra","id":7846263,"avatar_url":"https://avatars3.githubusercontent.com/u/7846263?v=4","gravatar_id":"","url":"https://api.github.com/users/omainegra","html_url":"https://github.com/omainegra","followers_url":"https://api.github.com/users/omainegra/followers","following_url":"https://api.github.com/users/omainegra/following{/other_user}","gists_url":"https://api.github.com/users/omainegra/gists{/gist_id}","starred_url":"https://api.github.com/users/omainegra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/omainegra/subscriptions","organizations_url":"https://api.github.com/users/omainegra/orgs","repos_url":"https://api.github.com/users/omainegra/repos","events_url":"https://api.github.com/users/omainegra/events{/privacy}","received_events_url":"https://api.github.com/users/omainegra/received_events","type":"User","site_admin":false},"body":"Monix with OkHttp handler supporting `Observable[ByteBuffer]` streaming. This closes #17 ","created_at":"2017-08-04T23:40:43Z","updated_at":"2017-08-14T15:08:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f5e790fdea312661999752c5ca318c5892c093f3","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/softwaremill/sttp/pulls/24/commits","review_comments_url":"https://api.github.com/repos/softwaremill/sttp/pulls/24/comments","review_comment_url":"https://api.github.com/repos/softwaremill/sttp/pulls/comments{/number}","comments_url":"https://api.github.com/repos/softwaremill/sttp/issues/24/comments","statuses_url":"https://api.github.com/repos/softwaremill/sttp/statuses/c6c1f5a34930946e8ab4e9248b9255ce2e1464fe","head":{"label":"omainegra:okhttp3-monix","ref":"okhttp3-monix","sha":"c6c1f5a34930946e8ab4e9248b9255ce2e1464fe","user":{"login":"omainegra","id":7846263,"avatar_url":"https://avatars3.githubusercontent.com/u/7846263?v=4","gravatar_id":"","url":"https://api.github.com/users/omainegra","html_url":"https://github.com/omainegra","followers_url":"https://api.github.com/users/omainegra/followers","following_url":"https://api.github.com/users/omainegra/following{/other_user}","gists_url":"https://api.github.com/users/omainegra/gists{/gist_id}","starred_url":"https://api.github.com/users/omainegra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/omainegra/subscriptions","organizations_url":"https://api.github.com/users/omainegra/orgs","repos_url":"https://api.github.com/users/omainegra/repos","events_url":"https://api.github.com/users/omainegra/events{/privacy}","received_events_url":"https://api.github.com/users/omainegra/received_events","type":"User","site_admin":false},"repo":{"id":98461926,"name":"sttp","full_name":"omainegra/sttp","owner":{"login":"omainegra","id":7846263,"avatar_url":"https://avatars3.githubusercontent.com/u/7846263?v=4","gravatar_id":"","url":"https://api.github.com/users/omainegra","html_url":"https://github.com/omainegra","followers_url":"https://api.github.com/users/omainegra/followers","following_url":"https://api.github.com/users/omainegra/following{/other_user}","gists_url":"https://api.github.com/users/omainegra/gists{/gist_id}","starred_url":"https://api.github.com/users/omainegra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/omainegra/subscriptions","organizations_url":"https://api.github.com/users/omainegra/orgs","repos_url":"https://api.github.com/users/omainegra/repos","events_url":"https://api.github.com/users/omainegra/events{/privacy}","received_events_url":"https://api.github.com/users/omainegra/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/omainegra/sttp","description":"The Scala HTTP client you always wanted!","fork":true,"url":"https://api.github.com/repos/omainegra/sttp","forks_url":"https://api.github.com/repos/omainegra/sttp/forks","keys_url":"https://api.github.com/repos/omainegra/sttp/keys{/key_id}","collaborators_url":"https://api.github.com/repos/omainegra/sttp/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/omainegra/sttp/teams","hooks_url":"https://api.github.com/repos/omainegra/sttp/hooks","issue_events_url":"https://api.github.com/repos/omainegra/sttp/issues/events{/number}","events_url":"https://api.github.com/repos/omainegra/sttp/events","assignees_url":"https://api.github.com/repos/omainegra/sttp/assignees{/user}","branches_url":"https://api.github.com/repos/omainegra/sttp/branches{/branch}","tags_url":"https://api.github.com/repos/omainegra/sttp/tags","blobs_url":"https://api.github.com/repos/omainegra/sttp/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/omainegra/sttp/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/omainegra/sttp/git/refs{/sha}","trees_url":"https://api.github.com/repos/omainegra/sttp/git/trees{/sha}","statuses_url":"https://api.github.com/repos/omainegra/sttp/statuses/{sha}","languages_url":"https://api.github.com/repos/omainegra/sttp/languages","stargazers_url":"https://api.github.com/repos/omainegra/sttp/stargazers","contributors_url":"https://api.github.com/repos/omainegra/sttp/contributors","subscribers_url":"https://api.github.com/repos/omainegra/sttp/subscribers","subscription_url":"https://api.github.com/repos/omainegra/sttp/subscription","commits_url":"https://api.github.com/repos/omainegra/sttp/commits{/sha}","git_commits_url":"https://api.github.com/repos/omainegra/sttp/git/commits{/sha}","comments_url":"https://api.github.com/repos/omainegra/sttp/comments{/number}","issue_comment_url":"https://api.github.com/repos/omainegra/sttp/issues/comments{/number}","contents_url":"https://api.github.com/repos/omainegra/sttp/contents/{+path}","compare_url":"https://api.github.com/repos/omainegra/sttp/compare/{base}...{head}","merges_url":"https://api.github.com/repos/omainegra/sttp/merges","archive_url":"https://api.github.com/repos/omainegra/sttp/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/omainegra/sttp/downloads","issues_url":"https://api.github.com/repos/omainegra/sttp/issues{/number}","pulls_url":"https://api.github.com/repos/omainegra/sttp/pulls{/number}","milestones_url":"https://api.github.com/repos/omainegra/sttp/milestones{/number}","notifications_url":"https://api.github.com/repos/omainegra/sttp/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/omainegra/sttp/labels{/name}","releases_url":"https://api.github.com/repos/omainegra/sttp/releases{/id}","deployments_url":"https://api.github.com/repos/omainegra/sttp/deployments","created_at":"2017-07-26T20:10:57Z","updated_at":"2017-07-26T20:10:59Z","pushed_at":"2017-08-09T20:46:22Z","git_url":"git://github.com/omainegra/sttp.git","ssh_url":"git@github.com:omainegra/sttp.git","clone_url":"https://github.com/omainegra/sttp.git","svn_url":"https://github.com/omainegra/sttp","homepage":"https://softwaremill.com/open-source/","size":313,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"softwaremill:master","ref":"master","sha":"ff76a1737bb88c2664927db309d196e677ba3e98","user":{"login":"softwaremill","id":322765,"avatar_url":"https://avatars0.githubusercontent.com/u/322765?v=4","gravatar_id":"","url":"https://api.github.com/users/softwaremill","html_url":"https://github.com/softwaremill","followers_url":"https://api.github.com/users/softwaremill/followers","following_url":"https://api.github.com/users/softwaremill/following{/other_user}","gists_url":"https://api.github.com/users/softwaremill/gists{/gist_id}","starred_url":"https://api.github.com/users/softwaremill/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/softwaremill/subscriptions","organizations_url":"https://api.github.com/users/softwaremill/orgs","repos_url":"https://api.github.com/users/softwaremill/repos","events_url":"https://api.github.com/users/softwaremill/events{/privacy}","received_events_url":"https://api.github.com/users/softwaremill/received_events","type":"Organization","site_admin":false},"repo":{"id":95900154,"name":"sttp","full_name":"softwaremill/sttp","owner":{"login":"softwaremill","id":322765,"avatar_url":"https://avatars0.githubusercontent.com/u/322765?v=4","gravatar_id":"","url":"https://api.github.com/users/softwaremill","html_url":"https://github.com/softwaremill","followers_url":"https://api.github.com/users/softwaremill/followers","following_url":"https://api.github.com/users/softwaremill/following{/other_user}","gists_url":"https://api.github.com/users/softwaremill/gists{/gist_id}","starred_url":"https://api.github.com/users/softwaremill/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/softwaremill/subscriptions","organizations_url":"https://api.github.com/users/softwaremill/orgs","repos_url":"https://api.github.com/users/softwaremill/repos","events_url":"https://api.github.com/users/softwaremill/events{/privacy}","received_events_url":"https://api.github.com/users/softwaremill/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/softwaremill/sttp","description":"The Scala HTTP client you always wanted!","fork":false,"url":"https://api.github.com/repos/softwaremill/sttp","forks_url":"https://api.github.com/repos/softwaremill/sttp/forks","keys_url":"https://api.github.com/repos/softwaremill/sttp/keys{/key_id}","collaborators_url":"https://api.github.com/repos/softwaremill/sttp/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/softwaremill/sttp/teams","hooks_url":"https://api.github.com/repos/softwaremill/sttp/hooks","issue_events_url":"https://api.github.com/repos/softwaremill/sttp/issues/events{/number}","events_url":"https://api.github.com/repos/softwaremill/sttp/events","assignees_url":"https://api.github.com/repos/softwaremill/sttp/assignees{/user}","branches_url":"https://api.github.com/repos/softwaremill/sttp/branches{/branch}","tags_url":"https://api.github.com/repos/softwaremill/sttp/tags","blobs_url":"https://api.github.com/repos/softwaremill/sttp/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/softwaremill/sttp/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/softwaremill/sttp/git/refs{/sha}","trees_url":"https://api.github.com/repos/softwaremill/sttp/git/trees{/sha}","statuses_url":"https://api.github.com/repos/softwaremill/sttp/statuses/{sha}","languages_url":"https://api.github.com/repos/softwaremill/sttp/languages","stargazers_url":"https://api.github.com/repos/softwaremill/sttp/stargazers","contributors_url":"https://api.github.com/repos/softwaremill/sttp/contributors","subscribers_url":"https://api.github.com/repos/softwaremill/sttp/subscribers","subscription_url":"https://api.github.com/repos/softwaremill/sttp/subscription","commits_url":"https://api.github.com/repos/softwaremill/sttp/commits{/sha}","git_commits_url":"https://api.github.com/repos/softwaremill/sttp/git/commits{/sha}","comments_url":"https://api.github.com/repos/softwaremill/sttp/comments{/number}","issue_comment_url":"https://api.github.com/repos/softwaremill/sttp/issues/comments{/number}","contents_url":"https://api.github.com/repos/softwaremill/sttp/contents/{+path}","compare_url":"https://api.github.com/repos/softwaremill/sttp/compare/{base}...{head}","merges_url":"https://api.github.com/repos/softwaremill/sttp/merges","archive_url":"https://api.github.com/repos/softwaremill/sttp/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/softwaremill/sttp/downloads","issues_url":"https://api.github.com/repos/softwaremill/sttp/issues{/number}","pulls_url":"https://api.github.com/repos/softwaremill/sttp/pulls{/number}","milestones_url":"https://api.github.com/repos/softwaremill/sttp/milestones{/number}","notifications_url":"https://api.github.com/repos/softwaremill/sttp/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/softwaremill/sttp/labels{/name}","releases_url":"https://api.github.com/repos/softwaremill/sttp/releases{/id}","deployments_url":"https://api.github.com/repos/softwaremill/sttp/deployments","created_at":"2017-06-30T15:15:07Z","updated_at":"2017-08-14T03:23:03Z","pushed_at":"2017-08-10T08:58:36Z","git_url":"git://github.com/softwaremill/sttp.git","ssh_url":"git@github.com:softwaremill/sttp.git","clone_url":"https://github.com/softwaremill/sttp.git","svn_url":"https://github.com/softwaremill/sttp","homepage":"https://softwaremill.com/open-source/","size":336,"stargazers_count":194,"watchers_count":194,"language":"Scala","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":7,"mirror_url":null,"open_issues_count":10,"forks":7,"open_issues":10,"watchers":194,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/softwaremill/sttp/pulls/24"},"html":{"href":"https://github.com/softwaremill/sttp/pull/24"},"issue":{"href":"https://api.github.com/repos/softwaremill/sttp/issues/24"},"comments":{"href":"https://api.github.com/repos/softwaremill/sttp/issues/24/comments"},"review_comments":{"href":"https://api.github.com/repos/softwaremill/sttp/pulls/24/comments"},"review_comment":{"href":"https://api.github.com/repos/softwaremill/sttp/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/softwaremill/sttp/pulls/24/commits"},"statuses":{"href":"https://api.github.com/repos/softwaremill/sttp/statuses/c6c1f5a34930946e8ab4e9248b9255ce2e1464fe"}}}}
{"actor":{"display_login":"omainegra"}}