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
| def loadNumberOfItems(url: String): IO[Either[Exception, Int]] = | |
| Url(url) | |
| .toRight(InvalidUrl) | |
| .map(request(_)) | |
| .traverse(_.map(_.getFieldOption("number").flatMap(_.as[Int]).toRight(InvalidJson))) | |
| .map(_.flatten) |
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
| def loadNumberOfItems(url: String): Int = { | |
| val json: Json = request(url) | |
| json.getField("number").asInstanceOf[Int] | |
| } |
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
| sealed trait Exception | |
| case object InvalidUrl extends Exception | |
| case object InvalidJson extends Exception |
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
| loadNumberOfItems("foo.bar") shouldBe a [Int] | |
| // InvalidUrlException | |
| loadNumberOfItems("http://google.com") shouldBe a [Int] | |
| // InvalidPayloadException | |
| loadNumberOfItems("http://actual.server.endpoint.com/number") shouldBe a [Int] | |
| // ParsingException | |
| loadNumberOfItems("http://actual.server.endpoint.com/number") shouldBe a [Int] | |
| // ClassCastException |
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
| import java.io.{ByteArrayOutputStream, ByteArrayInputStream} | |
| import java.util.zip.{GZIPOutputStream, GZIPInputStream} | |
| import scala.util.Try | |
| object Gzip { | |
| def compress(input: Array[Byte]): Array[Byte] = { | |
| val bos = new ByteArrayOutputStream(input.length) | |
| val gzip = new GZIPOutputStream(bos) |
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
| {"lastUpload":"2020-10-16T15:12:58.640Z","extensionVersion":"v3.4.3"} |
OlderNewer