mirror of
https://github.com/mas-cli/mas
synced 2024-11-22 11:33:13 +00:00
🚚 Move lookup into StoreSearch
This commit is contained in:
parent
e54fc3b9df
commit
e746306eb6
4 changed files with 52 additions and 28 deletions
|
@ -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(())
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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?
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue