与 Vapor 客户端作斗争

Struggling with Vapor Client

我正在尝试从我的 vapor 网络服务向 Google 个地点 API 发出简单的获取请求。

这是我的控制器的样子:

import Vapor
import HTTP
import VaporPostgreSQL

final class MainController {

var currentDroplet: Droplet!

func addRoutes(drop: Droplet) {

    currentDroplet = drop
    drop.get("places",String.self, String.self, handler: getNearbyPlaces)

}

func getNearbyPlaces(request: Request, lat: String, long: String) throws -> ResponseRepresentable {

    let googleAPIKey = "MY_KEY"
    let googlePlacesBaseURL = "https://maps.googleapis.com/maps/api/place/nearbysearch"

    let url = googlePlacesBaseURL + "/json?location=\(lat),\(long)&radius=500&types=food&key=" + googleAPIKey

    print(url)

    let apiResponse = try drop.client.get(url)

    print(apiResponse)

    return apiResponse.json != nil ? apiResponse.json! : "Something went bad"


   }
}

应该就这么简单,但是当我调用它时,请求一直挂了很长时间然后returns 500。 请注意,控制台中打印的 url 确实可以直接在浏览器中正常工作。 我也想不出一个有用的方法来捕获和调试任何错误。

我需要将 import Foundationdrop.client = FoundationClient.self 添加到 main.swift 以获得类似的工作呼叫。