forked from asuc-octo/berkeley-mobile-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchDataSource.swift
More file actions
40 lines (33 loc) · 1.27 KB
/
SearchDataSource.swift
File metadata and controls
40 lines (33 loc) · 1.27 KB
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
//
// SearchDataSource.swift
// berkeleyMobileiOS
//
// Created by Maya Reddy on 2/26/17.
// Copyright © 2017 org.berkeleyMobile. All rights reserved.
//
import Foundation
import Alamofire
import SwiftyJSON
fileprivate let kSearchEndpoint = secretKAPIURL + "/search_items"
class SearchDataSource {
typealias ResourceType = Library
// Fetch the list of libraries and report back to the completionHandler.
static func fetchSearchItems(_ completion: @escaping ([SearchItem]?) -> Void)
{
Alamofire.request(kSearchEndpoint).responseJSON
{ response in
if response.result.isFailure {
print("[Error @ SearchDataSource.fetchSearchItems()]: request failed")
return
}
let searchItems = JSON(data: response.data!)["search_list"].map { (_, child) in parseSearchItem(child) }
completion(searchItems)
}
}
// Return a SearchItem object parsed from JSON.
private static func parseSearchItem(_ json: JSON) -> SearchItem
{
let searchItem = SearchItem(name: json["name"].stringValue, category: json["category"].stringValue, query: json["query"].stringValue)
return searchItem
}
}