-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add Alibaba Token Plan Personal/Solo variants (mainland + international) #2487
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
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
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
115 changes: 115 additions & 0 deletions
115
Sources/CodexBarCore/Providers/Alibaba/AlibabaTokenPlanPersonalUsageParser.swift
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,115 @@ | ||
| import Foundation | ||
|
|
||
| /// Shared parser for Alibaba Token Plan Personal/Solo rolling-window responses. | ||
| /// Both mainland and international variants expose the same payload contract. | ||
| enum AlibabaTokenPlanPersonalUsageParser { | ||
| static func parse( | ||
| from usageData: Data, | ||
| subscriptionData: Data?, | ||
| quotaConfigData: Data?, | ||
| now: Date) throws -> AlibabaTokenPlanUsageSnapshot | ||
| { | ||
| guard !usageData.isEmpty else { | ||
| throw AlibabaTokenPlanUsageError.parseFailed("Empty response body") | ||
| } | ||
|
|
||
| let raw: Any | ||
| do { | ||
| raw = try JSONSerialization.jsonObject(with: usageData) | ||
| } catch { | ||
| if let text = String(data: usageData, encoding: .utf8)?.lowercased(), | ||
| text.contains("<html"), | ||
| text.contains("login") || text.contains("sign in") || text.contains("signin") | ||
| { | ||
| throw AlibabaTokenPlanUsageError.loginRequired | ||
| } | ||
| throw AlibabaTokenPlanUsageError.parseFailed("Invalid JSON response") | ||
| } | ||
|
|
||
| let expanded = OneConsoleJSON.expandEmbeddedJSON(raw) | ||
| guard let dictionary = expanded as? [String: Any] else { | ||
| throw AlibabaTokenPlanUsageError.parseFailed("Unexpected payload") | ||
| } | ||
| try AlibabaTokenPlanUsageFetcher.throwIfErrorPayload(dictionary) | ||
| guard let usage = OneConsoleJSON.findObject( | ||
| containingAnyOf: ["per5HourPercentage", "per1WeekPercentage"], | ||
| in: expanded) | ||
| else { | ||
| throw AlibabaTokenPlanUsageError.parseFailed("Missing Personal usage windows") | ||
| } | ||
|
|
||
| let fiveHourPercent = OneConsoleJSON.percentagePoints( | ||
| fromRatio: OneConsoleJSON.number(usage["per5HourPercentage"])) | ||
| let weeklyPercent = OneConsoleJSON.percentagePoints( | ||
| fromRatio: OneConsoleJSON.number(usage["per1WeekPercentage"])) | ||
| guard fiveHourPercent != nil || weeklyPercent != nil else { | ||
| throw AlibabaTokenPlanUsageError.parseFailed("Missing Personal usage windows") | ||
| } | ||
|
|
||
| let planCode = subscriptionData.flatMap(self.planCode) | ||
| let quota = quotaConfigData.flatMap { | ||
| self.quotaTotals(from: $0, planCode: planCode) | ||
| } | ||
| return AlibabaTokenPlanUsageSnapshot( | ||
| planName: planCode.map(self.displayPlanName) ?? "Personal", | ||
| usedQuota: nil, | ||
| totalQuota: nil, | ||
| remainingQuota: nil, | ||
| resetsAt: nil, | ||
| fiveHourUsedPercent: fiveHourPercent, | ||
| fiveHourTotalQuota: quota?.fiveHour, | ||
| fiveHourResetsAt: OneConsoleJSON.date(usage["per5HourResetTime"]), | ||
| weeklyUsedPercent: weeklyPercent, | ||
| weeklyTotalQuota: quota?.weekly, | ||
| weeklyResetsAt: OneConsoleJSON.date(usage["per1WeekResetTime"]), | ||
| updatedAt: now) | ||
| } | ||
|
|
||
| private static func planCode(from data: Data) -> String? { | ||
| guard let raw = try? JSONSerialization.jsonObject(with: data) else { return nil } | ||
| let expanded = OneConsoleJSON.expandEmbeddedJSON(raw) | ||
| guard let plan = OneConsoleJSON.findObject( | ||
| containingAnyOf: ["specCode", "spec_code", "planName", "plan_name"], | ||
| in: expanded) | ||
| else { | ||
| return nil | ||
| } | ||
| for key in ["specCode", "spec_code", "planName", "plan_name"] { | ||
| if let value = OneConsoleJSON.string(plan[key])?.lowercased(), !value.isEmpty { | ||
| return value | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| private static func displayPlanName(_ planCode: String) -> String { | ||
| switch planCode { | ||
| case "lite": "Lite" | ||
| case "standard": "Standard" | ||
| case "pro": "Pro" | ||
| case "max": "Max" | ||
| default: planCode | ||
| } | ||
| } | ||
|
|
||
| private static func quotaTotals( | ||
| from data: Data, | ||
| planCode: String?) -> (fiveHour: Double?, weekly: Double?)? | ||
| { | ||
| guard let planCode, | ||
| let raw = try? JSONSerialization.jsonObject(with: data) | ||
| else { | ||
| return nil | ||
| } | ||
| let expanded = OneConsoleJSON.expandEmbeddedJSON(raw) | ||
| guard let value = OneConsoleJSON.findFirstValue(forKeys: [planCode], in: expanded), | ||
| let quota = value as? [String: Any] | ||
| else { | ||
| return nil | ||
| } | ||
| let fiveHour = OneConsoleJSON.number(quota["five_hour"] ?? quota["fiveHour"]) | ||
| let weekly = OneConsoleJSON.number(quota["weekly"]) | ||
| guard fiveHour != nil || weekly != nil else { return nil } | ||
| return (fiveHour, weekly) | ||
| } | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a user selects the International — Personal/Solo variant, the Personal request body sends this
consoleSitevalue throughIntlBroadScopeAspnGateway, but the existing Model Studio OneConsole path inAlibabaCodingPlanAPIRegion.consoleSiteusesMODELSTUDIO_ALIBABACLOUD. The new tests only assert the mainland Personal request body, so this typo can leave the international Personal gateway receiving an unrecognized site id while the mainland variant still works.Useful? React with 👍 / 👎.