[英]Handling and manipulating a list of http responses
I'm currently trying to implement API logic to fetch multiple images from a server.我目前正在尝试实现 API 逻辑以从服务器获取多个图像。 This server accepts an image id and return an HTTP response that contains the image in PNG format as an entity.
此服务器接受图像 ID 并返回 HTTP 响应,其中包含 PNG 格式的图像作为实体。
Right now, we want to add a new endpoint that accepts a list of images IDs and return a list of all the images:现在,我们要添加一个新的端点,它接受图像 ID 列表并返回所有图像的列表:
I have done the following:我做了以下事情:
def getImagesFromIds(IdsList: List[String]): Future[List[HttpResponse]] = {
Future.sequence {
IdsList.map(
id => getImageById(id)
)
}
}
this function will receive a list of ids and will call the getImageById to fetch all the images, it will return a list of HttpResponse.这个 function 将收到一个 id 列表,并将调用 getImageById 来获取所有图像,它将返回一个 HttpResponse 列表。
And for the route definition, I have done the following:对于路线定义,我做了以下工作:
def getImagesByIdsListRoute: Route = get {
path("by-ids-list") {
entity(as[List[String]]){
upcs =>
complete(getImagesFromIds(upcs))
}
}
}
But I'm getting the following error message:但我收到以下错误消息:
no implicits found for parameter m: marshalling.toresponsemarshallable[list[httpresponse]]没有找到参数 m 的隐式:marshalling.toresponsemarshallable[list[httpresponse]]
Does Any one know how we can marshall a list of http responses, or if there is any way to improve this logic to fetch multiple http responses?有没有人知道我们如何编组 http 响应列表,或者是否有任何方法可以改进此逻辑以获取多个 http 响应?
If I understand correctly, you want to download multiple images and return them as a HTTP response.如果我理解正确的话,你想要下载多张图片并将它们作为 HTTP 响应返回。
getImageById
returns a HttpResponse
.getImageById
调用 API 返回一个HttpResponse
。 You can't be sure what is the result of this API call.List[HttpResponse]
as your response.List[HttpResponse]
作为您的响应。 How should this response be serialized?getImageById
so that it checks what is in the HttpResponse
and returns the entity bytes.getImageById
以便它检查HttpResponse
中的内容并返回实体字节。 Example:例子:
response match {
case HttpResponse(StatusCodes.OK, _, entity, _) =>
entity.dataBytes
case resp @ HttpResponse(code, _, _, _) =>
// Response failed and we don't care about the response entity
// Details: https://doc.akka.io/docs/akka-http/current/implications-of-streaming-http-entity.html
resp.discardEntityBytes()
// Decide yourself how you want to handle failures
throw new RuntimeException("Request failed, response code: " + code)
}
dataBytes
returns a Source
so you'll end up with a List
of Sources
. dataBytes
返回一个Source
所以你最终会得到一个List
of Sources
。 You have to concatenate them via, for example via concat
.concat
。Compression.gzip
.Compression.gzip
。complete
method of getImagesByIdsListRoute
.getImagesByIdsListRoute
的complete
方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.