🚚 Move lookup into StoreSearch

This commit is contained in:
Ben Chatelain 2019-01-01 21:44:33 -08:00
parent e54fc3b9df
commit e746306eb6
4 changed files with 52 additions and 28 deletions

View file

@ -17,33 +17,26 @@ public struct HomeCommand: CommandProtocol {
public let function = "Opens MAS Preview app page in a browser" public let function = "Opens MAS Preview app page in a browser"
private let storeSearch: StoreSearch private let storeSearch: StoreSearch
private let urlSession: URLSession
/// Designated initializer. /// Designated initializer.
public init(storeSearch: StoreSearch = MasStoreSearch(), public init(storeSearch: StoreSearch = MasStoreSearch()) {
urlSession: URLSession = URLSession.shared) {
self.storeSearch = storeSearch self.storeSearch = storeSearch
self.urlSession = urlSession
} }
/// Runs the command. /// Runs the command.
public func run(_ options: HomeOptions) -> Result<(), MASError> { public func run(_ options: HomeOptions) -> Result<(), MASError> {
guard let homeURLString = storeSearch.lookupURLString(forApp: options.appId), do {
let jsonData = urlSession.requestSynchronousDataWithURLString(homeURLString) guard let result = try storeSearch.lookup(app: options.appId)
else { else {
return .failure(.searchFailed) print("No results found")
} return .failure(.noSearchResultsFound)
}
guard let results = try? JSONDecoder().decode(SearchResultList.self, from: jsonData), dump(result)
results.resultCount > 0 }
else { catch {
print("No results found") return .failure(.searchFailed)
return .failure(.noSearchResultsFound)
} }
dump(results.resultCount)
let first = results.results.first!
dump(first)
return .success(()) return .success(())
} }

View file

@ -8,7 +8,12 @@
/// Manages searching the MAS catalog through the iTunes Search and Lookup APIs. /// Manages searching the MAS catalog through the iTunes Search and Lookup APIs.
public class MasStoreSearch : StoreSearch { 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. /// Builds the lookup URL for an app.
/// ///
@ -20,4 +25,29 @@ public class MasStoreSearch : StoreSearch {
} }
return nil 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
}
} }

View file

@ -6,13 +6,13 @@
// Copyright © 2018 mas-cli. All rights reserved. // Copyright © 2018 mas-cli. All rights reserved.
// //
struct SearchResult: Decodable { public struct SearchResult: Decodable {
var bundleId: String public var bundleId: String
var price: Double public var price: Double
var sellerName: String public var sellerName: String
var sellerUrl: String public var sellerUrl: String
var trackId: Int public var trackId: Int
var trackName: String public var trackName: String
var trackViewUrl: String public var trackViewUrl: String
var version: String public var version: String
} }

View file

@ -9,4 +9,5 @@
/// Protocol for searching the MAS catalog. /// Protocol for searching the MAS catalog.
public protocol StoreSearch { public protocol StoreSearch {
func lookupURLString(forApp: String) -> String? func lookupURLString(forApp: String) -> String?
func lookup(app appId: String) throws -> SearchResult?
} }