xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / DeveloperServices / DeveloperServicesClient.swift
DeveloperServicesClient.swift111 lines · 4.1 KiBRaw
| 1 | // |
| 2 | // DeveloperServicesClient.swift |
| 3 | // Supercharge |
| 4 | // |
| 5 | // Created by Kabir Oberai on 24/07/19. |
| 6 | // Copyright © 2019 Kabir Oberai. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | import Foundation |
| 10 | import Dependencies |
| 11 | |
| 12 | public struct DeveloperServicesClient: Sendable { |
| 13 | |
| 14 | public enum Error: LocalizedError { |
| 15 | case noData |
| 16 | case malformedRequest |
| 17 | case apiError(code: Int, reason: String?) |
| 18 | |
| 19 | public var errorDescription: String? { |
| 20 | switch self { |
| 21 | case .noData: |
| 22 | return NSLocalizedString( |
| 23 | "developer_services_client.error.no_data", value: "No data", comment: "" |
| 24 | ) |
| 25 | case .malformedRequest: |
| 26 | return NSLocalizedString( |
| 27 | "developer_services_client.error.malformed_request", value: "Malformed request", comment: "" |
| 28 | ) |
| 29 | case .apiError(let code, let reason?): |
| 30 | return reason.withCString { reasonC in |
| 31 | "\(code)".withCString { codeC in |
| 32 | String.localizedStringWithFormat( |
| 33 | NSLocalizedString( |
| 34 | "developer_services_client.error.sign_in_error.reason_and_code", |
| 35 | value: "%s (%s)", |
| 36 | comment: "First reason, then code" |
| 37 | ), |
| 38 | reasonC, codeC |
| 39 | ) |
| 40 | } |
| 41 | } |
| 42 | case .apiError(let code, _): |
| 43 | return "\(code)".withCString { |
| 44 | String.localizedStringWithFormat( |
| 45 | NSLocalizedString( |
| 46 | "developer_services_client.error.sign_in_error.only_code", |
| 47 | value: "An unknown error occurred (%s)", |
| 48 | comment: "" |
| 49 | ), |
| 50 | $0 |
| 51 | ) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | @Dependency(\.anisetteDataProvider) var anisetteDataProvider |
| 58 | @Dependency(\.httpClient) var httpClient |
| 59 | |
| 60 | public let loginToken: DeveloperServicesLoginToken |
| 61 | |
| 62 | public init(loginToken: DeveloperServicesLoginToken) { |
| 63 | self.loginToken = loginToken |
| 64 | } |
| 65 | |
| 66 | public init(authData: XcodeAuthData) { |
| 67 | self.loginToken = authData.loginToken |
| 68 | } |
| 69 | |
| 70 | private func send<R: DeveloperServicesRequest>( |
| 71 | _ request: R, |
| 72 | anisetteData: AnisetteData |
| 73 | ) async throws -> R.Value { |
| 74 | guard let url = request.apiVersion.url(forAction: request.action) else { |
| 75 | throw Error.malformedRequest |
| 76 | } |
| 77 | |
| 78 | var httpRequest = HTTPRequest(method: .post, url: url) |
| 79 | let acceptedLanguages = Locale.preferredLanguages.joined(separator: ", ") |
| 80 | |
| 81 | httpRequest.headerFields[.acceptLanguage] = acceptedLanguages |
| 82 | httpRequest.headerFields[.accept] = request.apiVersion.accept |
| 83 | httpRequest.headerFields[.contentType] = request.apiVersion.contentType |
| 84 | httpRequest.headerFields[.userAgent] = "Xcode" |
| 85 | request.methodOverride.map { httpRequest.headerFields[.init("X-HTTP-Method-Override")!] = $0 } |
| 86 | |
| 87 | httpRequest.headerFields[.init(DeviceInfo.xcodeVersionKey)!] = DeviceInfo.xcodeVersion |
| 88 | |
| 89 | httpRequest.headerFields[.init("X-Apple-I-Identity-Id")!] = loginToken.adsid |
| 90 | httpRequest.headerFields[.init("X-Apple-App-Info")!] = AppTokenKey.xcode.rawValue |
| 91 | httpRequest.headerFields[.init("X-Apple-GS-Token")!] = loginToken.token |
| 92 | |
| 93 | anisetteData.dictionary.forEach { httpRequest.headerFields[.init($0)!] = $1 } |
| 94 | |
| 95 | request.configure(urlRequest: &httpRequest) |
| 96 | |
| 97 | let body = try request.apiVersion.body(withParameters: request.parameters) |
| 98 | |
| 99 | let (_, data) = try await httpClient.makeRequest(httpRequest, body: body) |
| 100 | |
| 101 | let decoded: R.Response = try request.apiVersion.decode(response: data) |
| 102 | |
| 103 | return try await request.parse(decoded) |
| 104 | } |
| 105 | |
| 106 | public func send<R: DeveloperServicesRequest>(_ request: R) async throws -> R.Value { |
| 107 | let anisetteData = try await anisetteDataProvider.fetchAnisetteData() |
| 108 | return try await self.send(request, anisetteData: anisetteData) |
| 109 | } |
| 110 | |
| 111 | } |