简体   繁体   English

如何使用akka-http将Future [Source […]]编组到HttpResponse?

[英]How to marshal a Future[Source[…]] to an HttpResponse with akka-http?

I have the following method that returns a Future[Source[List[String]]] (the first two lines of a CSV file): 我有以下方法返回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)
        }
}

The returned Future is passed to complete , which marshals it to a JSON array of arrays using: 返回的Future传递给complete ,它使用以下命令将其封送至JSON数组数组:

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. 但是,我想检查response ,如果不是200,则返回另一个HttpResponse 。似乎最好的方法是将Future[Source[...]]编组到HttpResponse中。方法,然后它的返回类型都是HttpResponse

How do I do that? 我怎么做? Or is there a better way? 或者,还有更好的方法?

Okay, so I got there in the end with a different approach. 好的,最后我采用了另一种方法。

Http(system).singleRequest(request)
    .flatMap { response =>
        response.status match {
            case StatusCodes.OK =>
                val compression = CompressionChooser.choose(url, gzip, response)
                response.entity.withoutSizeLimit.dataBytes
                    .via(compression.decoder.decoderFlow)
                    .viaMat(scanner)(Keep.left)
                    .map(_.map(_.decodeString(charset)))
                    .take(2)
                    .runWith(Sink.seq)
                    .map { rows =>
                        val json = Json.toJson(rows)
                        HttpResponse(
                            StatusCodes.OK,
                            entity = HttpEntity(ContentTypes.`application/json`, json.toString)
                        )
                    }

            case _ => Future successful HttpResponse(StatusCodes.BadRequest, entity = "Error")
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM