xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / DeveloperServices / DeveloperServicesAPIVersion.swift
DeveloperServicesAPIVersion.swift119 lines · 4.1 KiBRaw
| 1 | // |
| 2 | // DeveloperServicesAPIVersion.swift |
| 3 | // XKit |
| 4 | // |
| 5 | // Created by Kabir Oberai on 10/04/20. |
| 6 | // Copyright © 2020 Kabir Oberai. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | import Foundation |
| 10 | import Superutils |
| 11 | |
| 12 | public protocol DeveloperServicesAPIVersion: Sendable { |
| 13 | func url(forAction action: String) -> URL? |
| 14 | var contentType: String { get } |
| 15 | var accept: String { get } |
| 16 | func body(withParameters parameters: [String: Any]) throws -> Data |
| 17 | func decode<R: Decodable>(response: Data) throws -> R |
| 18 | } |
| 19 | |
| 20 | public struct DeveloperServicesAPIVersionLegacy: DeveloperServicesAPIVersion { |
| 21 | |
| 22 | public struct Error: LocalizedError { |
| 23 | public let code: Int |
| 24 | public let reason: String? |
| 25 | |
| 26 | public var errorDescription: String? { |
| 27 | return "\(code)".withCString { codeC in |
| 28 | if let reason = reason { |
| 29 | return reason.withCString { reasonC in |
| 30 | String.localizedStringWithFormat( |
| 31 | NSLocalizedString( |
| 32 | "developer_services_api.old.error.reason_and_code", |
| 33 | value: "%s (%s)", |
| 34 | comment: "First reason, then code" |
| 35 | ), |
| 36 | reasonC, codeC |
| 37 | ) |
| 38 | } |
| 39 | } else { |
| 40 | return String.localizedStringWithFormat( |
| 41 | NSLocalizedString( |
| 42 | "developer_services_api.old.error.only_code", |
| 43 | value: "An unknown error occurred (%s)", |
| 44 | comment: "" |
| 45 | ), |
| 46 | codeC |
| 47 | ) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | private struct Response<T: Decodable>: Decodable { |
| 54 | private let resultCode: PossiblyStringifiedNumber |
| 55 | private let resultString: String? |
| 56 | private let userString: String? |
| 57 | private let inner: T? |
| 58 | |
| 59 | private enum CodingKeys: String, CodingKey { |
| 60 | case resultCode |
| 61 | case resultString |
| 62 | case userString |
| 63 | } |
| 64 | |
| 65 | init(from decoder: Decoder) throws { |
| 66 | let container = try decoder.container(keyedBy: CodingKeys.self) |
| 67 | self.resultCode = try container.decode(PossiblyStringifiedNumber.self, forKey: .resultCode) |
| 68 | self.resultString = try container.decodeIfPresent(String.self, forKey: .resultString) |
| 69 | self.userString = try container.decodeIfPresent(String.self, forKey: .userString) |
| 70 | |
| 71 | self.inner = resultCode.value == 0 ? |
| 72 | try decoder.singleValueContainer().decode(T.self) : |
| 73 | nil |
| 74 | } |
| 75 | |
| 76 | var result: Result<T, Swift.Error> { |
| 77 | if let inner = inner { |
| 78 | return .success(inner) |
| 79 | } else { |
| 80 | return .failure(Error( |
| 81 | code: resultCode.value, |
| 82 | reason: userString ?? resultString |
| 83 | )) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | private static let serviceProtocolVersion = "QH65B2" |
| 89 | private static let clientID = "XABBG36SBA" |
| 90 | private static let decoder = PropertyListDecoder() |
| 91 | |
| 92 | public func url(forAction action: String) -> URL? { |
| 93 | let urlString = """ |
| 94 | https://developerservices2.apple.com/services/\ |
| 95 | \(Self.serviceProtocolVersion)/\(action).action?\ |
| 96 | clientId=\(Self.clientID) |
| 97 | """ |
| 98 | return URL(string: urlString) |
| 99 | } |
| 100 | |
| 101 | public let contentType = "text/x-xml-plist" |
| 102 | public let accept = "text/x-xml-plist" |
| 103 | |
| 104 | public func body(withParameters parameters: [String: Any]) throws -> Data { |
| 105 | var parameters = parameters |
| 106 | parameters["requestId"] = UUID().uuidString |
| 107 | parameters["clientId"] = Self.clientID |
| 108 | parameters["protocolVersion"] = Self.serviceProtocolVersion |
| 109 | parameters["userLocale"] = [Locale.current.identifier] |
| 110 | return try PropertyListSerialization.data(fromPropertyList: parameters, format: .xml, options: 0) |
| 111 | } |
| 112 | |
| 113 | public func decode<R: Decodable>(response: Data) throws -> R { |
| 114 | try Self.decoder |
| 115 | .decode(Response<R>.self, from: response) |
| 116 | .result.get() |
| 117 | } |
| 118 | |
| 119 | } |