From df10b36a88f3ac517fb18217a750dd777139a372 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 14 Sep 2018 11:35:00 -0400 Subject: [PATCH] Pachyderm: Fix pagination --- Pachyderm/Model/Timeline.swift | 25 ++++++++++++++----------- Pachyderm/Request/Parameter.swift | 3 ++- Pachyderm/Request/Request.swift | 27 ++++++++++++++++++--------- Pachyderm/Response/Pagination.swift | 4 ++-- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/Pachyderm/Model/Timeline.swift b/Pachyderm/Model/Timeline.swift index e1eb7794..ba4eb047 100644 --- a/Pachyderm/Model/Timeline.swift +++ b/Pachyderm/Model/Timeline.swift @@ -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 diff --git a/Pachyderm/Request/Parameter.swift b/Pachyderm/Request/Parameter.swift index c6362765..8c03b9a5 100644 --- a/Pachyderm/Request/Parameter.swift +++ b/Pachyderm/Request/Parameter.swift @@ -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) } } } diff --git a/Pachyderm/Request/Request.swift b/Pachyderm/Request/Request.swift index c9b3a1b0..522369d3 100644 --- a/Pachyderm/Request/Request.swift +++ b/Pachyderm/Request/Request.swift @@ -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) + } } } } diff --git a/Pachyderm/Response/Pagination.swift b/Pachyderm/Response/Pagination.swift index c0ec4cec..07d86a54 100644 --- a/Pachyderm/Response/Pagination.swift +++ b/Pachyderm/Response/Pagination.swift @@ -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) } }