Skip to content

Instantly share code, notes, and snippets.

@kmuralidharan91
Created July 25, 2018 06:00
Show Gist options
  • Save kmuralidharan91/36b6ca746c293745d7fbcb64ff9e43c9 to your computer and use it in GitHub Desktop.
Save kmuralidharan91/36b6ca746c293745d7fbcb64ff9e43c9 to your computer and use it in GitHub Desktop.
APIRequestFetcher.swift
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