Created
July 27, 2018 11:19
-
-
Save bpoplauschi/09472a07dff279bb85711a5180a0d421 to your computer and use it in GitHub Desktop.
FlickrService
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 Moya | |
import SwiftyJSON | |
fileprivate enum FlickrAPI { | |
case photosSearch(lat: Double, lng: Double) | |
} | |
extension FlickrAPI: TargetType { | |
var baseURL: URL { return URL(string: "https://api.flickr.com/services/rest/")! } | |
var path: String { | |
switch self { | |
case .photosSearch: | |
return "" | |
} | |
} | |
var method: Moya.Method { | |
switch self { | |
case .photosSearch: | |
return .get | |
} | |
} | |
var task: Task { | |
switch self { | |
case .photosSearch(let lat, let lng): | |
let parameters: [String: Any] = ["method": "flickr.photos.search", | |
"api_key": "c76689472c10fa2a42a414091cb50588", | |
"format": "json", | |
"nojsoncallback": "1", | |
"extras": "url_l", | |
"per_page": "5", | |
"lat": lat, | |
"lon": lng] | |
return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString) | |
} | |
} | |
var sampleData: Data { | |
switch self { | |
case .photosSearch: | |
return "{}".utf8Encoded | |
} | |
} | |
var headers: [String: String]? { | |
return ["Content-type": "application/json"] | |
} | |
} | |
// MARK: - Helpers | |
fileprivate extension String { | |
var urlEscaped: String { | |
return addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)! | |
} | |
var utf8Encoded: Data { | |
return data(using: .utf8)! | |
} | |
} | |
protocol FlickrServiceInterface { | |
typealias GetPhotosHandler = ([String]?) -> Void | |
func getPhotos(latitude: Double, longitude: Double, getPhotosHandler: GetPhotosHandler?) | |
} | |
final class FlickrService: FlickrServiceInterface { | |
private let provider = MoyaProvider<FlickrAPI>() | |
func getPhotos(latitude: Double, longitude:Double, getPhotosHandler: GetPhotosHandler?) { | |
provider.request(.photosSearch(lat: latitude, lng: longitude)) { result in | |
switch result { | |
case let .failure(error): | |
print("API Error \(error)") | |
case let .success(response): | |
if let filteredResponse = try? response.filterSuccessfulStatusCodes(), let json = try? JSON(data: filteredResponse.data) { | |
var photoURLs: [String]? = nil | |
if json["stat"].stringValue != "ok" { | |
print("API Error \(response.statusCode) - please check the request to \(response.request?.url?.absoluteString ?? "")") | |
print("Response was: \(json.debugDescription)") | |
} else { | |
let photosDict = json["photos"].dictionaryValue | |
let photoArray = photosDict["photo"]?.arrayValue | |
photoURLs = photoArray?.map({$0["url_l"].stringValue}) | |
} | |
if let handler = getPhotosHandler { | |
handler(photoURLs) | |
} | |
} else { | |
print("API Error \(response.statusCode) - please check the request to\(response.request?.url?.absoluteString ?? "")") | |
if let responseString = String(data: response.data, encoding: .utf8) { | |
print("Response was: \(responseString)") | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment