This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathSession+listings.swift
301 lines (275 loc) · 15.3 KB
/
Session+listings.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//
// Session+listings.swift
// reddift
//
// Created by sonson on 2015/05/19.
// Copyright (c) 2015年 sonson. All rights reserved.
//
import Foundation
/**
The sort method for listing Link object, "/r/[subreddit]/[sort]" or "/[sort]".
*/
enum PrivateLinkSortBy {
case controversial
case top
var path: String {
switch self {
case .controversial:
return "/controversial"
case .top:
return "/hot"
}
}
}
extension Session {
/**
Get the comment tree for a given Link article.
If supplied, comment is the ID36 of a comment in the comment tree for article.
This comment will be the (highlighted) focal point of the returned view and context will be the number of parents shown.
A comment tree often includes "More" objects and vacant comment objects.
"More" objects have children comment objects.
This API is used for expand of them.
And vacant comment objects must be re-downloaded again using this API.
- parameter link: Link from which comment will be got.
- parameter sort: The type of sorting.
- parameter comments: If supplied, comment is the ID36 of a comment in the comment tree for article. When you want to expand "more" object or vacant comment objects, you have to specify it. Default is nil.
- parameter depth: The maximum depth of subtrees in the thread. Default is nil.
- parameter limit: The maximum number of comments to return. Default is nil.
- parameter completion: The completion handler to call when the load request is complete.
- paramater context: Number of context comments to show (in a url looks like ?context=5)
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func getArticles(_ link: Link, sort: CommentSort, comments: [String]? = nil, depth: Int? = nil, limit: Int? = nil, context: Int? = nil, completion: @escaping (Result<(Listing, Listing)>) -> Void) throws -> URLSessionDataTask {
var parameter = ["sort": sort.type, "showmore": "True"]
if let depth = depth {
parameter["depth"] = "\(depth)"
}
if let limit = limit {
parameter["limit"] = "\(limit)"
}
if let comments = comments {
let commaSeparatedIDString = comments.joined(separator: ",")
parameter["comment"] = commaSeparatedIDString
}
if let context = context {
parameter["context"] = String(context)
}
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/comments/" + link.id + ".json", parameter: parameter, method: "GET", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<(Listing, Listing)> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
.flatMap(json2RedditAny)
.flatMap(redditAny2ListingTuple)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
/**
Get Links from all subreddits or user specified subreddit.
- parameter paginator: Paginator object for paging contents.
- parameter subreddit: Subreddit from which Links will be gotten.
- parameter integratedSort: The original type of sorting a list, .Controversial, .Top, .Hot, or .New.
- parameter TimeFilterWithin: The type of filtering contents. When integratedSort is .Hot or .New, this parameter is ignored.
- parameter limit: The maximum number of comments to return. Default is 25.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func getList(_ paginator: Paginator, subreddit: SubredditURLPath?, sort: LinkSortType, timeFilterWithin: TimeFilterWithin, limit: Int = 25, completion: @escaping (Result<Listing>) -> Void) throws -> URLSessionDataTask {
do {
switch sort {
case .controversial:
return try getList(paginator, subreddit: subreddit, privateSortType: .controversial, timeFilterWithin: timeFilterWithin, limit: limit, completion: completion)
case .top:
return try getList(paginator, subreddit: subreddit, privateSortType: .top, timeFilterWithin: timeFilterWithin, limit: limit, completion: completion)
case .new:
return try getNewOrHotList(paginator, subreddit: subreddit, type: "new", limit: limit, completion: completion)
case .hot:
return try getNewOrHotList(paginator, subreddit: subreddit, type: "hot", limit: limit, completion: completion)
}
} catch { throw error }
}
/**
Get Links from all subreddits or user specified subreddit.
- parameter paginator: Paginator object for paging contents.
- parameter subreddit: Subreddit from which Links will be gotten.
- parameter sort: The type of sorting a list.
- parameter TimeFilterWithin: The type of filtering contents.
- parameter limit: The maximum number of comments to return. Default is 25.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
func getList(_ paginator: Paginator, subreddit: SubredditURLPath?, privateSortType: LinkSortType, timeFilterWithin: TimeFilterWithin, limit: Int = 25, completion: @escaping (Result<Listing>) -> Void) throws -> URLSessionDataTask {
let parameter = paginator.dictionaryByAdding(parameters: [
"limit": "\(limit)",
"show": "all",
// "sr_detail": "true",
"t": timeFilterWithin.param
])
var path = "\(privateSortType.path).json"
if let subreddit = subreddit { path = "\(subreddit.path)\(privateSortType.path).json" }
guard let request = URLRequest.requestForOAuth(with: baseURL, path: path, parameter: parameter, method: "GET", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<Listing> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
.flatMap(json2RedditAny)
.flatMap(redditAny2Object)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
/**
Get hot Links from all subreddits or user specified subreddit.
- parameter paginator: Paginator object for paging contents.
- parameter subreddit: Subreddit from which Links will be gotten.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
func getHotList(_ paginator: Paginator, subreddit: SubredditURLPath?, limit: Int = 25, completion: @escaping (Result<Listing>) -> Void) throws -> URLSessionDataTask {
do {
return try getNewOrHotList(paginator, subreddit: subreddit, type: "hot", limit: limit, completion: completion)
} catch { throw error }
}
/**
Get new Links from all subreddits or user specified subreddit.
- parameter paginator: Paginator object for paging contents.
- parameter subreddit: Subreddit from which Links will be gotten.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
func getNewList(_ paginator: Paginator, subreddit: SubredditURLPath?, limit: Int = 25, completion: @escaping (Result<Listing>) -> Void) throws -> URLSessionDataTask {
do {
return try getNewOrHotList(paginator, subreddit: subreddit, type: "new", limit: limit, completion: completion)
} catch { throw error }
}
/**
Get hot or new Links from all subreddits or user specified subreddit.
- parameter paginator: Paginator object for paging contents.
- parameter subreddit: Subreddit from which Links will be gotten.
- parameter type: "new" or "hot" as type.
- parameter limit: The maximum number of comments to return. Default is 25.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
func getNewOrHotList(_ paginator: Paginator, subreddit: SubredditURLPath?, type: String, limit: Int = 25, completion: @escaping (Result<Listing>) -> Void) throws -> URLSessionDataTask {
let parameter = paginator.dictionaryByAdding(parameters: [
"limit": "\(limit)",
// "sr_detail": "true",
"show": "all"
])
var path = "\(type).json"
if let subreddit = subreddit { path = "\(subreddit.path)/\(type).json" }
guard let request = URLRequest.requestForOAuth(with: baseURL, path: path, parameter: parameter, method: "GET", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<Listing> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
.flatMap(json2RedditAny)
.flatMap(redditAny2Object)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
/**
The Serendipity content.
But this endpoints return invalid redirect URL...
I don't know how this URL should be handled....
- parameter subreddit: Specified subreddit to which you would like to get random link
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func getRandom(_ subreddit: Subreddit? = nil, completion: @escaping (Result<(Listing, Listing)>) -> Void) throws -> URLSessionDataTask {
var path = "/random"
if let subreddit = subreddit { path = subreddit.url + "/random" }
guard let request = URLRequest.requestForOAuth(with: baseURL, path: path, method: "GET", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<(Listing, Listing)> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
.flatMap(json2RedditAny)
.flatMap(redditAny2ListingTuple)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
// MARK: BDT does not cover following methods.
/**
Related page: performs a search using title of article as the search query.
- parameter paginator: Paginator object for paging contents.
- parameter thing: Thing object to which you want to obtain the contents that are related.
- parameter limit: The maximum number of comments to return. Default is 25.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func getRelatedArticles(_ paginator: Paginator, thing: Thing, limit: Int = 25, completion: @escaping (Result<(Listing, Listing)>) -> Void) throws -> URLSessionDataTask {
let parameter = paginator.dictionaryByAdding(parameters: [
"limit": "\(limit)",
// "sr_detail": "true",
"show": "all"
])
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/related/" + thing.id + ".json", parameter: parameter, method: "GET", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<(Listing, Listing)> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
.flatMap(json2RedditAny)
.flatMap(redditAny2ListingTuple)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
/**
Return a list of other submissions of the same URL.
- parameter paginator: Paginator object for paging contents.
- parameter thing: Thing object by which you want to obtain the same URL is mentioned.
- parameter limit: The maximum number of comments to return. Default is 25.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func getDuplicatedArticles(_ paginator: Paginator, thing: Thing, limit: Int = 25, completion: @escaping (Result<(Listing, Listing)>) -> Void) throws -> URLSessionDataTask {
let parameter = paginator.dictionaryByAdding(parameters: [
"limit": "\(limit)",
// "sr_detail": "true",
"show": "all"
])
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/duplicates/" + thing.id + ".json", parameter: parameter, method: "GET", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<(Listing, Listing)> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
.flatMap(json2RedditAny)
.flatMap(redditAny2ListingTuple)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
/**
Get a listing of links by fullname.
:params: links A list of Links
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func getLinksById(_ links: [Link], completion: @escaping (Result<Listing>) -> Void) throws -> URLSessionDataTask {
let fullnameList = links.map({ (link: Link) -> String in link.name })
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/by_id/" + fullnameList.joined(separator: ",") + ".json", method: "GET", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<Listing> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
.flatMap(json2RedditAny)
.flatMap(redditAny2Object)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
}