🌍 Localize queries

This commit is contained in:
Chris Araman 2021-12-15 12:46:41 -08:00 committed by Chris Araman
parent fb2218d951
commit 560c89af2c
2 changed files with 29 additions and 3 deletions

View file

@ -31,6 +31,11 @@ extension StoreSearch {
URLQueryItem(name: "entity", value: "macSoftware"), URLQueryItem(name: "entity", value: "macSoftware"),
URLQueryItem(name: "term", value: appName), URLQueryItem(name: "term", value: appName),
] ]
if let country = country {
components.queryItems!.append(country)
}
return components.url return components.url
} }
@ -44,6 +49,24 @@ extension StoreSearch {
} }
components.queryItems = [URLQueryItem(name: "id", value: "\(appId)")] components.queryItems = [URLQueryItem(name: "id", value: "\(appId)")]
if let country = country {
components.queryItems!.append(country)
}
return components.url return components.url
} }
private var country: URLQueryItem? {
// CommerceKit and StoreFoundation don't seem to expose the region of the Apple ID signed
// into the App Store. Instead, we'll make an educated guess that it matches the currently
// selected locale in macOS. This obviously isn't always going to match, but it's probably
// better than passing no "country" at all to the iTunes Search API.
// https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/
guard let region = Locale.autoupdatingCurrent.regionCode else {
return nil
}
return URLQueryItem(name: "country", value: region)
}
} }

View file

@ -6,6 +6,7 @@
// Copyright © 2019 mas-cli. All rights reserved. // Copyright © 2019 mas-cli. All rights reserved.
// //
import Foundation
import Nimble import Nimble
import PromiseKit import PromiseKit
import Quick import Quick
@ -26,19 +27,21 @@ struct StoreSearchForTesting: StoreSearch {
public class StoreSearchSpec: QuickSpec { public class StoreSearchSpec: QuickSpec {
override public func spec() { override public func spec() {
let storeSearch = StoreSearchForTesting() let storeSearch = StoreSearchForTesting()
let region = Locale.autoupdatingCurrent.regionCode!
describe("url string") { describe("url string") {
it("contains the app name") { it("contains the app name") {
let appName = "myapp" let appName = "myapp"
let urlString = storeSearch.searchURL(for: appName)?.absoluteString let urlString = storeSearch.searchURL(for: appName)?.absoluteString
expect(urlString) == "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appName)" expect(urlString) == "https://itunes.apple.com/search?"
+ "media=software&entity=macSoftware&term=\(appName)&country=\(region)"
} }
it("contains the encoded app name") { it("contains the encoded app name") {
let appName = "My App" let appName = "My App"
let appNameEncoded = "My%20App" let appNameEncoded = "My%20App"
let urlString = storeSearch.searchURL(for: appName)?.absoluteString let urlString = storeSearch.searchURL(for: appName)?.absoluteString
expect(urlString) expect(urlString) == "https://itunes.apple.com/search?"
== "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appNameEncoded)" + "media=software&entity=macSoftware&term=\(appNameEncoded)&country=\(region)"
} }
// Find a character that causes addingPercentEncoding(withAllowedCharacters to return nil // Find a character that causes addingPercentEncoding(withAllowedCharacters to return nil
xit("is nil when app name cannot be url encoded") { xit("is nil when app name cannot be url encoded") {