我有一个vapor服务器应用程序(托管在heroku上),它通过urlsession数据任务连接到外部api服务。
到目前为止,它一直工作得很好(在我的mac上本地运行时仍然可以工作),但是自从上次服务器重新启动以来,datatask每次都会抛出以下错误:
URLError(_nsError: The operation could not be completed)
.
我听说swift在linux上曾经遇到过urlsession的问题,但这些问题基本上都解决了。有人能解决这个问题吗?谢谢您!
代码:
func foo(task: RepeatedTask) {
guard let client = try? app.client() else {
return
}
var urlComponents = URLComponents(string: Globals.API_URL)!
urlComponents.queryItems = [
URLQueryItem(name: "resource_id", value: Globals.RESOURCE_ID),
URLQueryItem(name: "apikey", value: Globals.API_KEY),
URLQueryItem(name: "type", value: "1")]
let url = urlComponents.url
let response = client.get(url!)
let tempModel = response.flatMap(to: Model.self) { response -> EventLoopFuture<Model> in
return try response.content.decode(Model.self)
}.map(to: Model.self) { tempModel -> Model in
tempModel.busArray.forEach {
$0.vehicleID = $0.Lines! + $0.Brigade!
$0.angle = 0
}
if Globals.myModel.busArray.isEmpty {
Globals.myModel.busArray = tempModel.busArray
} else {
Globals.myModel.busArray.updateVehicleArray(using: tempModel.busArray)
Globals.myModel.busArray.updateCoordinates(using: tempModel.busArray)
}
return tempModel
}
}
生成的URL
urlComponents
是
https://api.um.warszawa.pl/api/action/busestrams_get/?resource_id=f2e5503e-927d-4ad3-9500-4ab9e55deb59&apikey=a86c68be-0117-4a88-838c-bcc2f6ba85f4&type=1