Created
April 11, 2019 15:47
-
-
Save terjokhin/7b4c7f87d1c21905fcafcd386b0a4588 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 org.daron.html | |
import cats.syntax.either._ | |
import cats.syntax.flatMap._ | |
import cats.syntax.functor._ | |
import com.softwaremill.sttp.{SttpBackend, _} | |
import io.chrisdavenport.log4cats.Logger | |
trait HttpClient[F[_]] { | |
def getPageSource(uri: java.net.URI): F[String] | |
} | |
object HttpClient { | |
def apply[F[_] : cats.MonadError[?[_], Throwable] : Logger](implicit sttpBackend: SttpBackend[F, Nothing]): HttpClient[F] = | |
(uri: java.net.URI) => for { | |
_ <- Logger[F].debug(s"Getting source from page: ${uri.toString}") | |
resp <- sttp.get(Uri(uri)).send() | |
result <- resp.body.leftMap(new IllegalStateException(_): Throwable).raiseOrPure | |
} yield result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment