xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / DeveloperServices / DeveloperServicesTeamFetcher.swift
DeveloperServicesTeamFetcher.swift59 lines · 1.9 KiBRaw
| 1 | import Foundation |
| 2 | |
| 3 | private actor DeveloperServicesTeamFetcher { |
| 4 | static let shared = DeveloperServicesTeamFetcher() |
| 5 | |
| 6 | private var cache: [DeveloperServicesTeam.ID: Task<DeveloperServicesTeam, Error>] = [:] |
| 7 | |
| 8 | private init() {} |
| 9 | |
| 10 | func teams(forXcodeLogin auth: XcodeAuthData) async throws -> DeveloperServicesTeam { |
| 11 | let teamID = auth.teamID |
| 12 | if let cached = cache[teamID] { |
| 13 | return try await cached.value |
| 14 | } |
| 15 | let task = Task { |
| 16 | let client = DeveloperServicesClient(authData: auth) |
| 17 | let teams = try await client.send(DeveloperServicesListTeamsRequest()) |
| 18 | guard let team = teams.first(where: { $0.id == teamID }) |
| 19 | else { throw Errors.teamNotFound(teamID) } |
| 20 | return team |
| 21 | } |
| 22 | cache[auth.teamID] = task |
| 23 | return try await task.value |
| 24 | } |
| 25 | |
| 26 | enum Errors: LocalizedError { |
| 27 | case teamNotFound(DeveloperServicesTeam.ID) |
| 28 | |
| 29 | public var errorDescription: String? { |
| 30 | switch self { |
| 31 | case .teamNotFound(let id): |
| 32 | return id.rawValue.withCString { |
| 33 | String.localizedStringWithFormat( |
| 34 | NSLocalizedString( |
| 35 | "add_app_operation.error.team_not_found", |
| 36 | value: "A team with the ID '%s' could not be found. Please select another team.", |
| 37 | comment: "" |
| 38 | ), $0 |
| 39 | ) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | extension XcodeAuthData { |
| 47 | public func team() async throws -> DeveloperServicesTeam { |
| 48 | try await DeveloperServicesTeamFetcher.shared.teams(forXcodeLogin: self) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | extension DeveloperAPIAuthData { |
| 53 | public func team() async throws -> DeveloperServicesTeam? { |
| 54 | switch self { |
| 55 | case .appStoreConnect: nil |
| 56 | case .xcode(let auth): try await auth.team() |
| 57 | } |
| 58 | } |
| 59 | } |