2018-12-29 22:57:06 -08:00
|
|
|
//
|
|
|
|
// MasStoreSearch.swift
|
|
|
|
// MasKit
|
|
|
|
//
|
|
|
|
// Created by Ben Chatelain on 12/29/18.
|
|
|
|
// Copyright © 2018 mas-cli. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Manages searching the MAS catalog through the iTunes Search and Lookup APIs.
|
2019-01-04 16:54:00 -08:00
|
|
|
public class MasStoreSearch: StoreSearch {
|
2019-01-06 12:26:08 -07:00
|
|
|
private let networkManager: NetworkManager
|
2019-01-01 21:44:33 -08:00
|
|
|
|
|
|
|
/// Designated initializer.
|
2019-01-06 12:26:08 -07:00
|
|
|
public init(networkManager: NetworkManager = NetworkManager()) {
|
|
|
|
self.networkManager = networkManager
|
2019-01-01 21:44:33 -08:00
|
|
|
}
|
2018-12-29 22:57:06 -08:00
|
|
|
|
2019-01-11 17:10:36 -07:00
|
|
|
/// Searches for an app.
|
|
|
|
///
|
|
|
|
/// - Parameter appName: MAS ID of app
|
|
|
|
/// - Returns: Search results list of app. List will have no records if there were no matches. Never nil.
|
|
|
|
/// - Throws: Error if there is a problem with the network request.
|
|
|
|
public func search(for appName: String) throws -> SearchResultList {
|
|
|
|
guard let url = searchURL(for: appName)
|
2019-01-29 23:15:24 -07:00
|
|
|
else { throw MASError.urlEncoding }
|
2019-01-11 17:10:36 -07:00
|
|
|
|
|
|
|
let result = networkManager.loadDataSync(from: url)
|
|
|
|
|
|
|
|
// Unwrap network result
|
|
|
|
guard case let .success(data) = result
|
2019-01-29 23:15:24 -07:00
|
|
|
else {
|
|
|
|
if case let .failure(error) = result {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
throw MASError.noData
|
2019-01-11 17:10:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
let results = try JSONDecoder().decode(SearchResultList.self, from: data)
|
|
|
|
return results
|
|
|
|
} catch {
|
|
|
|
throw MASError.jsonParsing(error: error as NSError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-01 21:44:33 -08:00
|
|
|
/// Looks up app details.
|
|
|
|
///
|
|
|
|
/// - Parameter appId: MAS ID of app
|
|
|
|
/// - Returns: Search result record of app or nil if no apps match the ID.
|
|
|
|
/// - Throws: Error if there is a problem with the network request.
|
2019-01-17 22:06:25 -07:00
|
|
|
public func lookup(app appId: Int) throws -> SearchResult? {
|
2019-01-06 12:26:08 -07:00
|
|
|
guard let url = lookupURL(forApp: appId)
|
2019-01-29 23:15:24 -07:00
|
|
|
else { throw MASError.urlEncoding }
|
2019-01-06 12:26:08 -07:00
|
|
|
|
|
|
|
let result = networkManager.loadDataSync(from: url)
|
|
|
|
|
|
|
|
// Unwrap network result
|
|
|
|
guard case let .success(data) = result
|
2019-01-29 23:15:24 -07:00
|
|
|
else {
|
|
|
|
if case let .failure(error) = result {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
throw MASError.noData
|
2019-01-01 21:44:33 -08:00
|
|
|
}
|
|
|
|
|
2019-01-11 17:10:36 -07:00
|
|
|
do {
|
|
|
|
let results = try JSONDecoder().decode(SearchResultList.self, from: data)
|
2019-01-01 21:44:33 -08:00
|
|
|
|
2019-01-11 17:10:36 -07:00
|
|
|
guard let searchResult = results.results.first
|
2019-01-29 23:15:24 -07:00
|
|
|
else { return nil }
|
2019-01-01 21:44:33 -08:00
|
|
|
|
2019-01-11 17:10:36 -07:00
|
|
|
return searchResult
|
|
|
|
} catch {
|
|
|
|
throw MASError.jsonParsing(error: error as NSError)
|
|
|
|
}
|
2019-01-01 21:44:33 -08:00
|
|
|
}
|
2018-12-29 22:57:06 -08:00
|
|
|
}
|