diff --git a/Sources/MasKit/Controllers/StoreSearch.swift b/Sources/MasKit/Controllers/StoreSearch.swift index 9960f53e3..304570179 100644 --- a/Sources/MasKit/Controllers/StoreSearch.swift +++ b/Sources/MasKit/Controllers/StoreSearch.swift @@ -73,19 +73,16 @@ extension StoreSearch { /// - Parameter appName: MAS app identifier. /// - Returns: URL for the search service or nil if appName can't be encoded. 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 { - return "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(urlEncodedAppName)" + guard var components = URLComponents(string: "https://itunes.apple.com/search") else { + return nil } - return nil + + components.queryItems = [ + URLQueryItem(name: "media", value: "software"), + URLQueryItem(name: "entity", value: "macSoftware"), + URLQueryItem(name: "term", value: appName), + ] + return components.url } /// Builds the lookup URL for an app. @@ -93,15 +90,11 @@ extension StoreSearch { /// - Parameter appId: MAS app identifier. /// - Returns: URL for the lookup service or nil if appId can't be encoded. func lookupURL(forApp appId: Int) -> URL? { - guard let urlString = lookupURLString(forApp: appId) else { return nil } - return URL(string: urlString) - } + guard var components = URLComponents(string: "https://itunes.apple.com/lookup") else { + return nil + } - /// Builds the lookup URL for an app. - /// - /// - Parameter appId: MAS app identifier. - /// - Returns: String URL for the lookup service. - func lookupURLString(forApp appId: Int) -> String? { - "https://itunes.apple.com/lookup?id=\(appId)" + components.queryItems = [URLQueryItem(name: "id", value: "\(appId)")] + return components.url } } diff --git a/Sources/MasKit/Extensions/String+PercentEncoding.swift b/Sources/MasKit/Extensions/String+PercentEncoding.swift deleted file mode 100644 index 121ae3be5..000000000 --- a/Sources/MasKit/Extensions/String+PercentEncoding.swift +++ /dev/null @@ -1,16 +0,0 @@ -// -// String+PercentEncoding.swift -// MasKit -// -// Created by Ben Chatelain on 1/5/19. -// Copyright © 2019 mas-cli. All rights reserved. -// - -import Foundation - -extension String { - /// Return an URL encoded string - public var urlEncodedString: String? { - addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) - } -} diff --git a/Tests/MasKitTests/Controllers/StoreSearchSpec.swift b/Tests/MasKitTests/Controllers/StoreSearchSpec.swift index dd90ef323..4e5bf6e07 100644 --- a/Tests/MasKitTests/Controllers/StoreSearchSpec.swift +++ b/Tests/MasKitTests/Controllers/StoreSearchSpec.swift @@ -28,20 +28,20 @@ class StoreSearchSpec: QuickSpec { describe("url string") { it("contains the app name") { let appName = "myapp" - let urlString = storeSearch.searchURLString(forApp: appName) + let urlString = storeSearch.searchURL(for: appName)?.absoluteString expect(urlString) == "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appName)" } it("contains the encoded app name") { let appName = "My App" let appNameEncoded = "My%20App" - let urlString = storeSearch.searchURLString(forApp: appName) + let urlString = storeSearch.searchURL(for: appName)?.absoluteString expect(urlString) == "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appNameEncoded)" } // Find a character that causes addingPercentEncoding(withAllowedCharacters to return nil xit("is nil when app name cannot be url encoded") { let appName = "`~!@#$%^&*()_+ 💩" - let urlString = storeSearch.searchURLString(forApp: appName) + let urlString = storeSearch.searchURL(for: appName)?.absoluteString expect(urlString).to(beNil()) } }