diff --git a/MasKit/Commands/Home.swift b/MasKit/Commands/Home.swift index 531d064..e8fee94 100644 --- a/MasKit/Commands/Home.swift +++ b/MasKit/Commands/Home.swift @@ -17,33 +17,26 @@ public struct HomeCommand: CommandProtocol { public let function = "Opens MAS Preview app page in a browser" private let storeSearch: StoreSearch - private let urlSession: URLSession /// Designated initializer. - public init(storeSearch: StoreSearch = MasStoreSearch(), - urlSession: URLSession = URLSession.shared) { + public init(storeSearch: StoreSearch = MasStoreSearch()) { self.storeSearch = storeSearch - self.urlSession = urlSession } /// Runs the command. public func run(_ options: HomeOptions) -> Result<(), MASError> { - guard let homeURLString = storeSearch.lookupURLString(forApp: options.appId), - let jsonData = urlSession.requestSynchronousDataWithURLString(homeURLString) - else { - return .failure(.searchFailed) - } + do { + guard let result = try storeSearch.lookup(app: options.appId) + else { + print("No results found") + return .failure(.noSearchResultsFound) + } - guard let results = try? JSONDecoder().decode(SearchResultList.self, from: jsonData), - results.resultCount > 0 - else { - print("No results found") - return .failure(.noSearchResultsFound) + dump(result) + } + catch { + return .failure(.searchFailed) } - - dump(results.resultCount) - let first = results.results.first! - dump(first) return .success(()) } diff --git a/MasKit/MasStoreSearch.swift b/MasKit/MasStoreSearch.swift index 42c9619..5866054 100644 --- a/MasKit/MasStoreSearch.swift +++ b/MasKit/MasStoreSearch.swift @@ -8,7 +8,12 @@ /// Manages searching the MAS catalog through the iTunes Search and Lookup APIs. public class MasStoreSearch : StoreSearch { - public init() {} + private let urlSession: URLSession + + /// Designated initializer. + public init(urlSession: URLSession = URLSession.shared) { + self.urlSession = urlSession + } /// Builds the lookup URL for an app. /// @@ -20,4 +25,29 @@ public class MasStoreSearch : StoreSearch { } return nil } + + /// 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. + public func lookup(app appId: String) throws -> SearchResult? { + guard let lookupURLString = lookupURLString(forApp: appId), + let jsonData = urlSession.requestSynchronousDataWithURLString(lookupURLString) + else { + // network error + throw MASError.searchFailed + } + + guard let results = try? JSONDecoder().decode(SearchResultList.self, from: jsonData) + else { + // parse error + throw MASError.searchFailed + } + + guard let result = results.results.first + else { return nil } + + return result + } } diff --git a/MasKit/SearchResult.swift b/MasKit/SearchResult.swift index f3eb8fe..daaa6d6 100644 --- a/MasKit/SearchResult.swift +++ b/MasKit/SearchResult.swift @@ -6,13 +6,13 @@ // Copyright © 2018 mas-cli. All rights reserved. // -struct SearchResult: Decodable { - var bundleId: String - var price: Double - var sellerName: String - var sellerUrl: String - var trackId: Int - var trackName: String - var trackViewUrl: String - var version: String +public struct SearchResult: Decodable { + public var bundleId: String + public var price: Double + public var sellerName: String + public var sellerUrl: String + public var trackId: Int + public var trackName: String + public var trackViewUrl: String + public var version: String } diff --git a/MasKit/StoreSearch.swift b/MasKit/StoreSearch.swift index 274bba1..a5bf1a4 100644 --- a/MasKit/StoreSearch.swift +++ b/MasKit/StoreSearch.swift @@ -9,4 +9,5 @@ /// Protocol for searching the MAS catalog. public protocol StoreSearch { func lookupURLString(forApp: String) -> String? + func lookup(app appId: String) throws -> SearchResult? }