Alamofire 验证 Json 具有多个 Urls 的结果

Alamofire Validate Json Results with multiple Urls

我使用 youtube api 并且 运行 超出配额。

我想用列表中的另一个 URLS 更改 URL (https://example.com/get) 如果 Json return 错误,直到我得到一个成功的 Json 结果。

Alamofire.request("https://example.com/get").validate().responseJSON { response in
    switch response.result {
    case .success:
        print("Validation Successful")
    case .failure(let error):
        print(error.localizedDescription)
    }
}

尝试

let urls = ["https://example.com/get","https://example.com/get","https://example.com/get"]
var current = 0
func callAgain(){
    let url = urls[current]
    Alamofire.request(url).validate().responseJSON { response in
        switch response.result {
        case .success:
            print("Validation Successful ",url)
        case .failure(let error):
            print(error.localizedDescription)
            current += 1
            if current < urls.count {
               callAgain()
            }
        }
    }
}