|
| 1 | +// |
| 2 | +// Purchase.swift |
| 3 | +// IPATool |
| 4 | +// |
| 5 | +// Created by Majd Alfhaily on 22.03.22. |
| 6 | +// |
| 7 | + |
| 8 | +import ArgumentParser |
| 9 | +import Foundation |
| 10 | +import Networking |
| 11 | +import StoreAPI |
| 12 | +import Persistence |
| 13 | + |
| 14 | +struct Purchase: AsyncParsableCommand { |
| 15 | + static var configuration: CommandConfiguration { |
| 16 | + return .init(abstract: "Obtain a license for the app from the App Store.") |
| 17 | + } |
| 18 | + |
| 19 | + @Option(name: [.short, .long], help: "The bundle identifier of the target iOS app.") |
| 20 | + private var bundleIdentifier: String |
| 21 | + |
| 22 | + @Option( |
| 23 | + name: [.customShort("c"), .customLong("country")], |
| 24 | + help: "The two-letter (ISO 3166-1 alpha-2) country code for the iTunes Store." |
| 25 | + ) |
| 26 | + private var countryCode: String = "US" |
| 27 | + |
| 28 | + @Option(name: [.short, .long], help: "The device family to limit the search query to.") |
| 29 | + private var deviceFamily: DeviceFamily = .phone |
| 30 | + |
| 31 | + @Option(name: [.long], help: "The log level.") |
| 32 | + private var logLevel: LogLevel = .info |
| 33 | + |
| 34 | + lazy var logger = ConsoleLogger(level: logLevel) |
| 35 | +} |
| 36 | + |
| 37 | +extension Purchase { |
| 38 | + private mutating func app(with bundleIdentifier: String) async -> iTunesResponse.Result { |
| 39 | + logger.log("Creating HTTP client...", level: .debug) |
| 40 | + let httpClient = HTTPClient(session: URLSession.shared) |
| 41 | + |
| 42 | + logger.log("Creating iTunes client...", level: .debug) |
| 43 | + let itunesClient = iTunesClient(httpClient: httpClient) |
| 44 | + |
| 45 | + do { |
| 46 | + logger.log("Querying the iTunes Store for '\(bundleIdentifier)' in country '\(countryCode)'...", level: .info) |
| 47 | + return try await itunesClient.lookup( |
| 48 | + bundleIdentifier: bundleIdentifier, |
| 49 | + countryCode: countryCode, |
| 50 | + deviceFamily: deviceFamily |
| 51 | + ) |
| 52 | + } catch { |
| 53 | + logger.log("\(error)", level: .debug) |
| 54 | + |
| 55 | + switch error { |
| 56 | + case iTunesClient.Error.appNotFound: |
| 57 | + logger.log("Could not find app.", level: .error) |
| 58 | + default: |
| 59 | + logger.log("An unknown error has occurred.", level: .error) |
| 60 | + } |
| 61 | + |
| 62 | + _exit(1) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private mutating func purchase(app: iTunesResponse.Result, account: Account) async { |
| 67 | + logger.log("Creating HTTP client...", level: .debug) |
| 68 | + let httpClient = HTTPClient(session: URLSession.shared) |
| 69 | + |
| 70 | + logger.log("Creating App Store client...", level: .debug) |
| 71 | + let storeClient = StoreClient(httpClient: httpClient) |
| 72 | + |
| 73 | + do { |
| 74 | + logger.log("Obtaining a license for '\(app.identifier)' from the App Store...", level: .info) |
| 75 | + try await storeClient.purchase( |
| 76 | + identifier: "\(app.identifier)", |
| 77 | + directoryServicesIdentifier: account.directoryServicesIdentifier, |
| 78 | + passwordToken: account.passwordToken, |
| 79 | + countryCode: countryCode |
| 80 | + ) |
| 81 | + } catch { |
| 82 | + logger.log("\(error)", level: .debug) |
| 83 | + |
| 84 | + switch error { |
| 85 | + case StoreClient.Error.purchaseFailed: |
| 86 | + logger.log("Purchase failed.", level: .error) |
| 87 | + case StoreClient.Error.duplicateLicense: |
| 88 | + logger.log("A license already exists for this item.", level: .error) |
| 89 | + case StoreResponse.Error.invalidCountry: |
| 90 | + logger.log("The country provided does not match with the account you are using. Supply a valid country using the \"--country\" flag.", level: .error) |
| 91 | + case StoreResponse.Error.passwordTokenExpired: |
| 92 | + logger.log("Token expired. Login again using the \"auth\" command.", level: .error) |
| 93 | + default: |
| 94 | + logger.log("An unknown error has occurred.", level: .error) |
| 95 | + } |
| 96 | + |
| 97 | + _exit(1) |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + mutating func run() async throws { |
| 103 | + // Authenticate with the App Store |
| 104 | + let keychainStore = KeychainStore(service: "ipatool.service") |
| 105 | + |
| 106 | + guard let account: Account = try keychainStore.value(forKey: "account") else { |
| 107 | + logger.log("Authentication required. Run \"ipatool auth --help\" for help.", level: .error) |
| 108 | + _exit(1) |
| 109 | + } |
| 110 | + logger.log("Authenticated as '\(account.name)'.", level: .info) |
| 111 | + |
| 112 | + // Query for app |
| 113 | + let app: iTunesResponse.Result = await app(with: bundleIdentifier) |
| 114 | + logger.log("Found app: \(app.name) (\(app.version)).", level: .debug) |
| 115 | + |
| 116 | + // Obtain a license |
| 117 | + await purchase(app: app, account: account) |
| 118 | + logger.log("Obtained a license for '\(app.identifier)'.", level: .debug) |
| 119 | + logger.log("Done.", level: .info) |
| 120 | + } |
| 121 | +} |
0 commit comments