Pachyderm: Fix pagination

This commit is contained in:
Shadowfacts 2018-09-14 11:35:00 -04:00
parent 6f424666d8
commit d46b61595e
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 36 additions and 23 deletions

View File

@ -17,22 +17,25 @@ public enum Timeline {
}
extension Timeline {
func request(range: RequestRange) -> Request<[Status]> {
var request: Request<[Status]>
var endpoint: String {
switch self {
case .home:
request = Request(method: .get, path: "/api/v1/timelines/home")
case let .public(local):
request = Request(method: .get, path: "/api/v1/timelines/public")
if local {
request.queryParameters.append("local" => true)
}
return "/api/v1/timelines/home"
case .public:
return "/api/v1/timelines/public"
case let .tag(hashtag):
request = Request(method: .get, path: "/api/v1/timeliens/tag/\(hashtag)")
return "/api/v1/timelines/tag/\(hashtag)"
case let .list(id):
request = Request(method: .get, path: "/api/v1/timelines/list/\(id)")
return "/api/v1/timelines/list/\(id)"
case .direct:
request = Request(method: .get, path: "/api/v1/timelines/direct")
return "/api/v1/timelines/direct"
}
}
func request(range: RequestRange) -> Request<[Status]> {
var request = Request<[Status]>(method: .get, path: endpoint)
if case .public(true) = self {
request.queryParameters.append("local" => true)
}
request.range = range
return request

View File

@ -74,7 +74,8 @@ extension Array where Element == Parameter {
var queryItems: [URLQueryItem] {
return compactMap {
URLQueryItem(name: $0.name, value: $0.value)
guard let value = $0.value else { return nil }
return URLQueryItem(name: $0.name, value: value)
}
}
}

View File

@ -40,17 +40,26 @@ extension Request {
}
set {
let rangeParams = newValue.queryParameters
let max = rangeParams.first { $0.name == "max_id" }
let since = rangeParams.first { $0.name == "since_id" }
let count = rangeParams.first { $0.name == "count" }
if let max = max, let i = queryParameters.firstIndex(where: { $0.name == "max_id" }) {
queryParameters[i] = max
if let max = rangeParams.first(where: { $0.name == "max_id" }) {
if let i = queryParameters.firstIndex(where: { $0.name == "max_id" }) {
queryParameters[i] = max
} else {
queryParameters.append(max)
}
}
if let since = since, let i = queryParameters.firstIndex(where: { $0.name == "since_id" }) {
queryParameters[i] = since
if let since = rangeParams.first(where: { $0.name == "since_id" }) {
if let i = queryParameters.firstIndex(where: { $0.name == "since_id" }) {
queryParameters[i] = since
} else {
queryParameters.append(since)
}
}
if let count = count, let i = queryParameters.firstIndex(where: { $0.name == "count" }) {
queryParameters[i] = count
if let count = rangeParams.first(where: { $0.name == "count" }) {
if let i = queryParameters.firstIndex(where: { $0.name == "count" }) {
queryParameters[i] = count
} else {
queryParameters.append(count)
}
}
}
}

View File

@ -30,9 +30,9 @@ extension Pagination {
var range: RequestRange {
switch kind {
case .next:
return .after(id: id, count: limit)
case .prev:
return .before(id: id, count: limit)
case .prev:
return .after(id: id, count: limit)
}
}