gpt4 book ai didi

Swift 从 NSHTTPURLResponse 的标题中获取下一页

转载 作者:搜寻专家 更新时间:2023-11-01 06:13:02 26 4
gpt4 key购买 nike

我正在使用一个 API,该 API 在一个名为 Link 的字段内为我提供页眉中的下一页。 (例如 Github 也是这样做的,所以这并不奇怪。Github Doc )

我正在使用的服务通过以下方式检索分页数据: pagination info正如我们在“链接”中看到的那样,给我下一页,与 $0.response?.allHeaderFields["Link"] :我得到</api/games?page=1&size=20>; rel="next",</api/games?page=25&size=20>; rel="last",</api/games?page=0&size=20>; rel="first" .

我找到了下面的代码来读取页面,但是它很脏......我想知道是否有人处理过同样的问题或者是否有一种标准的方式来面对它。 (我也搜索过 alamofire 是否支持任何类型的功能,但我还没有找到)

  // MARK: - Pagination
private func getNextPageFromHeaders(response: NSHTTPURLResponse?) -> String? {
if let linkHeader = response?.allHeaderFields["Link"] as? String {
/* looks like:
<https://api.github.com/user/20267/gists?page=2>; rel="next", <https://api.github.com/user/20267/gists?page=6>; rel="last"
*/
// so split on "," the on ";"
let components = linkHeader.characters.split {$0 == ","}.map { String($0) }
// now we have 2 lines like '<https://api.github.com/user/20267/gists?page=2>; rel="next"'
// So let's get the URL out of there:
for item in components {
// see if it's "next"
let rangeOfNext = item.rangeOfString("rel=\"next\"", options: [])
if rangeOfNext != nil {
let rangeOfPaddedURL = item.rangeOfString("<(.*)>;", options: .RegularExpressionSearch)
if let range = rangeOfPaddedURL {
let nextURL = item.substringWithRange(range)
// strip off the < and >;
let startIndex = nextURL.startIndex.advancedBy(1) //advance as much as you like
let endIndex = nextURL.endIndex.advancedBy(-2)
let urlRange = startIndex..<endIndex
return nextURL.substringWithRange(urlRange)
}
}
}
}
return nil
}

最佳答案

我认为 forEach()可能有更好的解决方案,但这是我得到的:

let linkHeader = "</api/games?page=1&size=20>; rel=\"next\",</api/games?page=25&size=20>; rel=\"last\",</api/games?page=0&size=20>; rel=\"first\""

let links = linkHeader.components(separatedBy: ",")

var dictionary: [String: String] = [:]
links.forEach({
let components = $0.components(separatedBy:"; ")
let cleanPath = components[0].trimmingCharacters(in: CharacterSet(charactersIn: "<>"))
dictionary[components[1]] = cleanPath
})

if let nextPagePath = dictionary["rel=\"next\""] {
print("nextPagePath: \(nextPagePath)")
}

//Bonus
if let lastPagePath = dictionary["rel=\"last\""] {
print("lastPagePath: \(lastPagePath)")
}
if let firstPagePath = dictionary["rel=\"first\""] {
print("firstPagePath: \(firstPagePath)")
}

控制台输出:

$> nextPagePath: /api/games?page=1&size=20
$> lastPagePath: /api/games?page=25&size=20
$> firstPagePath: /api/games?page=0&size=20

我用了components(separatedBy:)而不是 split()避免 String()转换在最后。我创建了一个 Dictionary对于要保留的值并删除了 <>带修剪。

关于Swift 从 NSHTTPURLResponse 的标题中获取下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51088747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com