Created
January 5, 2021 08:00
-
-
Save GrafBlutwurst/8ac447c707291398e12ccd181c4e27b0 to your computer and use it in GitHub Desktop.
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
package example | |
import zio.RIO | |
import zio.ZIO | |
import zio.duration._ | |
import zio.interop.catz._ | |
import okhttp3.OkHttpClient | |
import org.http4s.client.okhttp.OkHttpBuilder | |
import cats.effect.Blocker | |
import cats.implicits._ | |
import cats.effect.implicits._ | |
import cats.effect.ExitCode | |
import org.http4s.Method | |
import org.http4s.Request | |
import org.http4s.Uri | |
import cats.effect.implicits._ | |
import cats.implicits._ | |
import org.http4s.Request | |
import org.http4s.Uri | |
import io.circe.Json | |
import org.http4s.circe._ | |
import org.http4s.Method | |
import org.http4s.EntityEncoder | |
import io.circe.syntax._ | |
import org.http4s.MediaType | |
import org.http4s.Charset | |
import org.http4s.headers.`Content-Type` | |
import org.http4s.HttpVersion | |
import org.http4s.Headers | |
import org.http4s.Header | |
import cats.effect.Resource | |
import org.http4s.client.Client | |
import zio.clock.Clock | |
import okhttp3.ConnectionPool | |
import java.util.concurrent.TimeUnit | |
object MainZIO extends zio.App { | |
val testContent = scala.io.Source | |
.fromFile( | |
"/home/grafblutwurst/projects/rivero/amiko-workspace/tmp/testcontent" | |
) | |
.getLines() | |
.toList | |
.mkString("") | |
val payload = s"""{ | |
"fileName": "300053100469_attach1.jpg", | |
"mimeType": "image/jpeg", | |
"content": "$testContent", | |
"cardId": "0587300801956315", | |
"cardholderId": "058730", | |
"caseId": "10", | |
"digest": "7416b95782b6ab8f7e3d4cb4af7d8823" | |
}""" | |
val payload2 = """{ | |
"fileName": "300053100469_attach1.jpg", | |
"mimeType": "image/jpeg", | |
"content": "eyJmb28iOiJiYXIifQo=", | |
"cardId": "0587300801956315", | |
"cardholderId": "058730", | |
"caseId": "10", | |
"digest": "7416b95782b6ab8f7e3d4cb4af7d8823" | |
}""" | |
def makeRequest(client: Client[RIO[Clock, *]]): RIO[Clock, Json] = | |
client | |
.run( | |
Request[RIO[Clock, *]]( | |
Method.POST, | |
Uri.uri( | |
//"https://postman-echo.com/post" | |
"http://localhost:8000/document" | |
//"http://localhost:3000" | |
), | |
HttpVersion.`HTTP/1.1` | |
) | |
.withHeaders(Header("api-key", "1234")) | |
.withEntity(payload) | |
.withContentType( | |
`Content-Type`( | |
MediaType.application.json, | |
Some(Charset.`UTF-8`) | |
) | |
) | |
) | |
.use( | |
_.as[Json].flatMap(j => RIO.fromEither(j.as[Json])) | |
) | |
override def run(args: List[String]) = | |
zio.ZIO.runtime[Clock].flatMap { implicit runtime => | |
Blocker[RIO[Clock, *]] | |
.flatMap(blocker => | |
for { | |
client <- | |
OkHttpBuilder | |
.apply[RIO[Clock, *]]( | |
new OkHttpClient().newBuilder().connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS)).build(), | |
blocker | |
) | |
.resource | |
//client <- builder.resource | |
loggedClient = org.http4s.client.middleware.Logger[RIO[Clock, *]]( | |
true, | |
true, | |
_ => false, | |
Some((s: String) => RIO.effect(println(s))) | |
)(client) | |
} yield (client, loggedClient) | |
) | |
.use { tpl => | |
val (client, loggedClient) = tpl | |
for { | |
//_ <- { 0 to 100 }.toList.traverse(_ => makeRequest(client)) | |
_ <- makeRequest(client) | |
_ <- ZIO.sleep(2.seconds) | |
_ <- makeRequest(client) | |
} yield () | |
} | |
.as(zio.ExitCode.success) | |
.orDie | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment