Created
August 29, 2018 01:37
-
-
Save poborin/7abd2f9dc79ef5915e55a7d3bd6f45b3 to your computer and use it in GitHub Desktop.
iTunes search lab
This file contains hidden or 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
//: Playground - noun: a place where people can play | |
import Foundation | |
import PlaygroundSupport | |
var url = URL(string: "https://itunes.apple.com/search?")! | |
struct MusicTrack: Decodable { | |
enum CodingKeys: String, CodingKey { | |
case artistID = "artistId" | |
case collectionID = "collectionId" | |
case trackID = "trackId" | |
case artistName | |
case trackName | |
} | |
let artistID: Int | |
let collectionID: Int | |
let trackID: Int | |
let artistName: String | |
let trackName: String | |
} | |
struct StoreItemClientResponse: Decodable { | |
let resultCount: Int | |
let results: [MusicTrack] | |
} | |
enum QueryParameter { | |
case term(String) | |
case limit(Int) | |
case entity(Entity) | |
case country(Country) | |
enum Country { | |
case australia | |
case us | |
var countryCode: String { | |
switch self { | |
case .australia: | |
return "AU" | |
case .us: | |
return "US" | |
} | |
} | |
} | |
enum Entity: String { | |
case musicTrack | |
case software | |
} | |
var queryItem: URLQueryItem { | |
switch self { | |
case let .term(value): | |
return URLQueryItem(name: "term", value: value) | |
case let .limit(value): | |
return URLQueryItem(name: "limit", value: String(value)) | |
case let .entity(value): | |
return URLQueryItem(name: "entity", value: value.rawValue) | |
case let .country(value): | |
return URLQueryItem(name: "country", value: value.countryCode) | |
} | |
} | |
} | |
func url(fromQueryParameters queryParameters: QueryParameter...) -> URL! { | |
var components = URLComponents(url: url, resolvingAgainstBaseURL: true) | |
components?.queryItems = queryParameters.map { $0.queryItem } | |
return components?.url | |
} | |
func searchResult(forSearchTerm searchTerm: String, withCompletionHandler completionHandler: @escaping (Data) -> Void) { | |
let escapedTerm = searchTerm.replacingOccurrences(of: " ", with: "+") | |
let task = URLSession.shared.dataTask(with: url(fromQueryParameters: .term(escapedTerm), .limit(10), .entity(.musicTrack), .country(.australia))) { (data, response, error) in | |
guard error == nil, | |
let data = data else { | |
return | |
} | |
completionHandler(data) | |
} | |
task.resume() | |
} | |
searchResult(forSearchTerm: "swift") { (data) in | |
// print(String.init(data: data, encoding: String.Encoding.utf8)) | |
let decoder = JSONDecoder() | |
let response = try! decoder.decode(StoreItemClientResponse.self, from: data) | |
print(response.results) | |
} | |
PlaygroundPage.current.needsIndefiniteExecution = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment