Pachyderm: Fix pagination

This commit is contained in:
Shadowfacts 2018-09-14 11:35:00 -04:00
parent 6f424666d8
commit d46b61595e
Signed by: 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 { extension Timeline {
func request(range: RequestRange) -> Request<[Status]> { var endpoint: String {
var request: Request<[Status]>
switch self { switch self {
case .home: case .home:
request = Request(method: .get, path: "/api/v1/timelines/home") return "/api/v1/timelines/home"
case let .public(local): case .public:
request = Request(method: .get, path: "/api/v1/timelines/public") return "/api/v1/timelines/public"
if local {
request.queryParameters.append("local" => true)
}
case let .tag(hashtag): case let .tag(hashtag):
request = Request(method: .get, path: "/api/v1/timeliens/tag/\(hashtag)") return "/api/v1/timelines/tag/\(hashtag)"
case let .list(id): case let .list(id):
request = Request(method: .get, path: "/api/v1/timelines/list/\(id)") return "/api/v1/timelines/list/\(id)"
case .direct: 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 request.range = range
return request return request

View File

@ -74,7 +74,8 @@ extension Array where Element == Parameter {
var queryItems: [URLQueryItem] { var queryItems: [URLQueryItem] {
return compactMap { 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 { set {
let rangeParams = newValue.queryParameters let rangeParams = newValue.queryParameters
let max = rangeParams.first { $0.name == "max_id" } if let max = rangeParams.first(where: { $0.name == "max_id" }) {
let since = rangeParams.first { $0.name == "since_id" } if let i = queryParameters.firstIndex(where: { $0.name == "max_id" }) {
let count = rangeParams.first { $0.name == "count" } queryParameters[i] = max
if let max = max, let i = queryParameters.firstIndex(where: { $0.name == "max_id" }) { } else {
queryParameters[i] = max queryParameters.append(max)
}
} }
if let since = since, let i = queryParameters.firstIndex(where: { $0.name == "since_id" }) { if let since = rangeParams.first(where: { $0.name == "since_id" }) {
queryParameters[i] = since 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" }) { if let count = rangeParams.first(where: { $0.name == "count" }) {
queryParameters[i] = 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 { var range: RequestRange {
switch kind { switch kind {
case .next: case .next:
return .after(id: id, count: limit)
case .prev:
return .before(id: id, count: limit) return .before(id: id, count: limit)
case .prev:
return .after(id: id, count: limit)
} }
} }