Created
July 25, 2018 06:00
-
-
Save kmuralidharan91/36b6ca746c293745d7fbcb64ff9e43c9 to your computer and use it in GitHub Desktop.
APIRequestFetcher.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import SwiftyJSON | |
import Alamofire | |
enum NetworkError: Error { | |
case failure | |
case success | |
} | |
class APIRequestFetcher { | |
var searchResults = [JSON]() | |
func search(searchText: String, completionHandler: @escaping ([JSON]?, NetworkError) -> ()) { | |
let urlToSearch = "https://en.wikipedia.org//w/api.php?action=query&format=json&prop=pageimages%7Cpageterms&generator=prefixsearch&redirects=1&formatversion=2&piprop=thumbnail&pithumbsize=50&pilimit=10&wbptterms=description&gpssearch=\(searchText)&gpslimit=10" | |
Alamofire.request(urlToSearch).responseJSON { response in | |
guard let data = response.data else { | |
completionHandler(nil, .failure) | |
return | |
} | |
let json = try? JSON(data: data) | |
let results = json?["query"]["pages"].arrayValue | |
guard let empty = results?.isEmpty, !empty else { | |
completionHandler(nil, .failure) | |
return | |
} | |
completionHandler(results, .success) | |
} | |
} | |
func fetchImage(url: String, completionHandler: @escaping (UIImage?, NetworkError) -> ()) { | |
Alamofire.request(url).responseData { responseData in | |
guard let imageData = responseData.data else { | |
completionHandler(nil, .failure) | |
return | |
} | |
guard let image = UIImage(data: imageData) else { | |
completionHandler(nil, .failure) | |
return | |
} | |
completionHandler(image, .success) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment