Repository navigation
[JET] Add Alibaba and Xiaomi MiMo Token Plan quota readers #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import Foundation | ||
| import SwiftUI | ||
| import QuotaCore | ||
|
|
||
| actor MiMoQuotaCredentialStore { | ||
| static let shared = MiMoQuotaCredentialStore() | ||
| private let account = "quota-api-key-v1" | ||
| private let service = TokenStore.serviceName(suffix: "mimo-quota-api-key", bundleIdentifier: Bundle.main.bundleIdentifier) | ||
| private lazy var cache = TokenCache(calls: .live, bounds: .init(), ownServices: [service], log: { _ in }) | ||
|
|
||
| func load() async -> MiMoTokenPlanCredential? { | ||
| guard let raw = await cache.token(server: account, service: service), | ||
| let credential = try? JSONDecoder().decode(MiMoTokenPlanCredential.self, from: Data(raw.utf8)), | ||
| credential.isValid else { return nil } | ||
| return credential | ||
| } | ||
|
|
||
| func save(_ credential: MiMoTokenPlanCredential) async throws { | ||
| guard credential.isValid else { throw CredentialError.invalid } | ||
| let raw = String(decoding: try JSONEncoder().encode(credential), as: UTF8.self) | ||
| try await cache.save(raw, server: account, service: service) | ||
| } | ||
|
|
||
| enum CredentialError: Error { case invalid } | ||
|
|
||
| func clear() async throws { | ||
| try await cache.delete(server: account, service: service) | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| struct MiMoQuotaSettings: View { | ||
| @ObservedObject var model: MonitorModel | ||
| @State private var draft = "" | ||
| @State private var region = MiMoTokenPlanRegion.singapore | ||
| @State private var message: String? | ||
| @State private var busy = false | ||
|
|
||
| var body: some View { | ||
| VStack(alignment: .leading, spacing: 6) { | ||
| Picker("MiMo Token Plan region", selection: $region) { | ||
| ForEach(MiMoTokenPlanRegion.allCases, id: \.self) { value in | ||
| Text(value.title).tag(value) | ||
| } | ||
| } | ||
| SecureField("Individual Token Plan key", text: $draft) | ||
| HStack { | ||
| Button("Save MiMo Key") { save() } | ||
| .disabled(busy || !MiMoTokenPlanCredential(key: draft, region: region).isValid) | ||
| Button("Remove MiMo Key") { clear() } | ||
| .disabled(busy) | ||
| } | ||
| if let message { Text(message).font(.caption) } | ||
| Text("The key stays in this Mac’s Keychain. Choose the region shown on your MiMo Token Plan page.") | ||
| .font(.caption) | ||
| .foregroundStyle(.secondary) | ||
| } | ||
| .task { | ||
| if let saved = await MiMoQuotaCredentialStore.shared.load() { region = saved.region } | ||
| } | ||
| } | ||
|
|
||
| private func save() { | ||
| let credential = MiMoTokenPlanCredential(key: draft, region: region) | ||
| guard credential.isValid else { return } | ||
| busy = true | ||
| Task { @MainActor in | ||
| defer { busy = false } | ||
| do { | ||
| try await MiMoQuotaCredentialStore.shared.save(credential) | ||
| draft = "" | ||
| model.tokenPlanCredentialChanged(enabled: true) | ||
| message = "MiMo key saved." | ||
| } catch { message = "CodeCaps could not save the MiMo key in Keychain." } | ||
| } | ||
| } | ||
|
|
||
| private func clear() { | ||
| busy = true | ||
| Task { @MainActor in | ||
| defer { busy = false } | ||
| do { | ||
| try await MiMoQuotaCredentialStore.shared.clear() | ||
| draft = "" | ||
| model.tokenPlanCredentialChanged(enabled: false) | ||
| message = "MiMo key removed." | ||
| } catch { message = "CodeCaps could not remove the MiMo key from Keychain." } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.