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+search.swift
40 lines (35 loc) · 1.78 KB
/
Session+search.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
//
// Session+search.swift
// reddift
//
// Created by sonson on 2015/05/19.
// Copyright (c) 2015年 sonson. All rights reserved.
//
import Foundation
extension Session {
/**
Search link with query. If subreddit is nil, this method searched links from all of reddit.com.
- parameter subreddit: Specified subreddit to which you would like to limit your search.
- parameter query: The search keywords, must be less than 512 characters.
- parameter paginator: Paginator object for paging.
- parameter sort: Sort type, specified by SearchSortBy.
- 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 getSearch(_ subreddit: Subreddit?, query: String, paginator: Paginator, sort: SearchSortBy, completion: @escaping (Result<Listing>) -> Void) throws -> URLSessionDataTask {
let parameter = paginator.dictionaryByAdding(parameters: ["q": query, "sort": sort.path])
var path = "/search"
if let subreddit = subreddit { path = subreddit.url + "search" }
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)
}
}