I have the following method that returns a Future[Source[List[String]]] (the first two lines of a CSV file):
def get(url: String, charset: String, delimiter: Char, quote: Char, escape: Char) = {
val scanner = CsvParsing.lineScanner(
delimiter.toByte,
quote.toByte,
escape.toByte
)
val request = HttpRequest(GET, Uri(url)).withHeaders(`User-Agent`(UserAgent))
Http(system)
.singleRequest(request)
.map { response =>
response.entity.withoutSizeLimit.dataBytes
.viaMat(scanner)(Keep.left)
.map(row =>
row.map(bs =>
bs.decodeString(charset)
)
)
.take(2)
}
}
The returned Future is passed to complete, which marshals it to a JSON array of arrays using:
implicit val jsonStreamingSupport: JsonEntityStreamingSupport = EntityStreamingSupport.json()
However, I'd like to inspect response and return a different HttpResponse if it isn't a 200. It seems like the best way to do this would be to marshal the Future[Source[...]] to an HttpResponse in this method, and then its return type would be HttpResponse either way.
How do I do that? Or is there a better way?