Created
March 23, 2020 22:00
-
-
Save FabiolaRamirez/9a1fe05f0b825337748b62de4f63fbbb to your computer and use it in GitHub Desktop.
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
// | |
// Service.swift | |
// Gallery | |
// | |
// Created by Fabiola Ramirez on 3/23/20. | |
// Copyright © 2020 Fabiola Ramirez. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
struct Service { | |
static let sharedInstance = Service() | |
func getSongs(success: @escaping(_ photoList: [[String: Any]]) -> (), failure: @escaping(_ errorResponse: ErrorMessage)-> ()){ | |
let request = URLRequest(url: URL(string: "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=f9cc01 4fa76b098f9e82f1c288379ea1&tags=kitten&page=1&format=json&nojsoncallback=1")!) | |
let session = URLSession.shared | |
let task = session.dataTask(with: request as URLRequest) { data, response, error in | |
guard (error == nil) else { | |
failure(.noConnection) | |
return | |
} | |
guard let statusCode = (response as? HTTPURLResponse)?.statusCode, statusCode >= 200 && statusCode <= 299 else { | |
failure(.statusCode) | |
return | |
} | |
guard let data = data else { | |
failure(.noFound) | |
return | |
} | |
guard let json = try? JSONSerialization.jsonObject(with: data, options: []) else { | |
failure(.unableToParse) | |
return | |
} | |
let parsedResult = try? JSONSerialization.jsonObject(with: data, options: []) | |
guard let dictionary = parsedResult as? [String: Any] else { | |
failure(.unableToConvert) | |
return | |
} | |
guard let results = dictionary["photo"] as? [[String: Any]] else { | |
failure(.noKeyFound) | |
return | |
} | |
let photoResults = dictionary["photo"] as? [[String: Any]] | |
/*var songList: [Song] = [] | |
for obj in songResults! { | |
let song = Song(dictionary: obj) | |
songList.append(song!) | |
} | |
success(songList)*/ | |
print("photos: \(photoResults)") | |
success(photoResults!) | |
} | |
task.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment