2018-12-30 06:57:06 +00:00
|
|
|
//
|
|
|
|
// StoreSearch.swift
|
|
|
|
// MasKit
|
|
|
|
//
|
|
|
|
// Created by Ben Chatelain on 12/29/18.
|
|
|
|
// Copyright © 2018 mas-cli. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Protocol for searching the MAS catalog.
|
|
|
|
public protocol StoreSearch {
|
2019-01-18 05:06:25 +00:00
|
|
|
func lookup(app appId: Int) throws -> SearchResult?
|
2019-01-12 00:10:36 +00:00
|
|
|
func search(for appName: String) throws -> SearchResultList
|
2018-12-30 06:57:06 +00:00
|
|
|
}
|
2019-01-05 00:54:00 +00:00
|
|
|
|
2019-01-12 00:10:36 +00:00
|
|
|
// MARK: - Common methods
|
2019-01-05 00:54:00 +00:00
|
|
|
extension StoreSearch {
|
2019-01-12 00:10:36 +00:00
|
|
|
/// Builds the search URL for an app.
|
2019-01-05 00:54:00 +00:00
|
|
|
///
|
2019-01-12 00:10:36 +00:00
|
|
|
/// - Parameter appName: MAS app identifier.
|
|
|
|
/// - Returns: URL for the search service or nil if appName can't be encoded.
|
|
|
|
public func searchURL(for appName: String) -> URL? {
|
|
|
|
guard let urlString = searchURLString(forApp: appName) else { return nil }
|
|
|
|
return URL(string: urlString)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds the search URL for an app.
|
|
|
|
///
|
|
|
|
/// - Parameter appName: Name of app to find.
|
|
|
|
/// - Returns: String URL for the search service or nil if appName can't be encoded.
|
|
|
|
func searchURLString(forApp appName: String) -> String? {
|
|
|
|
if let urlEncodedAppName = appName.URLEncodedString {
|
2020-05-12 00:34:08 +00:00
|
|
|
return "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(urlEncodedAppName)"
|
2019-01-12 00:10:36 +00:00
|
|
|
}
|
|
|
|
return nil
|
2019-01-06 19:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds the lookup URL for an app.
|
|
|
|
///
|
|
|
|
/// - Parameter appId: MAS app identifier.
|
2019-01-12 00:10:36 +00:00
|
|
|
/// - Returns: URL for the lookup service or nil if appId can't be encoded.
|
2019-01-18 05:06:25 +00:00
|
|
|
public func lookupURL(forApp appId: Int) -> URL? {
|
2019-01-06 19:26:08 +00:00
|
|
|
guard let urlString = lookupURLString(forApp: appId) else { return nil }
|
|
|
|
return URL(string: urlString)
|
2019-01-05 00:54:00 +00:00
|
|
|
}
|
2019-01-12 01:06:02 +00:00
|
|
|
|
2019-01-12 00:10:36 +00:00
|
|
|
/// Builds the lookup URL for an app.
|
|
|
|
///
|
|
|
|
/// - Parameter appId: MAS app identifier.
|
2019-01-18 05:06:25 +00:00
|
|
|
/// - Returns: String URL for the lookup service.
|
|
|
|
func lookupURLString(forApp appId: Int) -> String? {
|
|
|
|
return "https://itunes.apple.com/lookup?id=\(appId)"
|
2019-01-12 00:10:36 +00:00
|
|
|
}
|
2019-01-05 00:54:00 +00:00
|
|
|
}
|