我有以下方法返回一个
Future[Source[List[String]]]
(CSV文件的前两行):
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)
}
}
返回的
Future
传递给
complete
,它使用以下命令将其封送到数组的JSON数组:
implicit val jsonStreamingSupport: JsonEntityStreamingSupport = EntityStreamingSupport.json()
response
并返回不同的
HttpResponse
如果不是200。这样做的最好方法似乎是将
Future[Source[...]]
HttpResponse
在该方法中,则其返回类型为
HttpResponse