-
Notifications
You must be signed in to change notification settings - Fork 32
feat: add missing fields in usage response #87
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
Open
eliot488995568
wants to merge
4
commits into
DeepL:main
Choose a base branch
from
eliot488995568:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+213
−3
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bda1232
feat: add missing fields in usage response
eliot488995568 a5079eb
test: fix speech limit fixture replacement
eliot488995568 5815265
refactor: change order to fallback for have limit reached
eliot488995568 775f8e6
test: add new test with limit reached
eliot488995568 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?php | ||
|
|
||
| // Copyright 2022 DeepL SE (https://www.deepl.com) | ||
| // Use of this source code is governed by an MIT | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| namespace DeepL; | ||
|
|
||
| /** | ||
| * Usage for one product, as reported in the products field of the usage response. | ||
| */ | ||
| class UsageProduct | ||
| { | ||
| /** | ||
| * @var string The product this usage refers to, for example 'translate' or 'speechToText'. | ||
| */ | ||
| public $productType; | ||
|
|
||
| /** | ||
| * @var string The unit the product is billed in, for example 'characters' or 'minutes'. | ||
| */ | ||
| public $billingUnit; | ||
|
|
||
| /** | ||
| * @var int The amount used by this API key, expressed in the billing unit. | ||
| */ | ||
| public $apiKeyUnitCount; | ||
|
|
||
| /** | ||
| * @var int The amount used by the whole account, expressed in the billing unit. | ||
| */ | ||
| public $accountUnitCount; | ||
|
|
||
| /** | ||
| * @var int The characters used by this API key, 0 for products not billed in characters. | ||
| */ | ||
| public $apiKeyCharacterCount; | ||
|
|
||
| /** | ||
| * @var int The characters used by the whole account, 0 for products not billed in characters. | ||
| */ | ||
| public $characterCount; | ||
|
|
||
| public function __construct(array $json) | ||
| { | ||
| $this->productType = $json['product_type'] ?? ''; | ||
| $this->billingUnit = $json['billing_unit'] ?? ''; | ||
| $this->apiKeyUnitCount = $json['api_key_unit_count'] ?? 0; | ||
| $this->accountUnitCount = $json['account_unit_count'] ?? 0; | ||
| $this->apiKeyCharacterCount = $json['api_key_character_count'] ?? 0; | ||
| $this->characterCount = $json['character_count'] ?? 0; | ||
| } | ||
| } |
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,95 @@ | ||
| <?php | ||
|
|
||
| // Copyright 2022 DeepL SE (https://www.deepl.com) | ||
| // Use of this source code is governed by an MIT | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| namespace DeepL; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class UsageTest extends TestCase | ||
| { | ||
| private const PRO_RESPONSE = <<<'JSON' | ||
| { | ||
| "character_count": 5947223, | ||
| "character_limit": 1000000000000, | ||
| "products": [ | ||
| { | ||
| "product_type": "translate", | ||
| "billing_unit": "characters", | ||
| "api_key_unit_count": 636, | ||
| "account_unit_count": 5941580, | ||
| "api_key_character_count": 636, | ||
| "character_count": 5941580 | ||
| }, | ||
| { | ||
| "product_type": "speechToText", | ||
| "billing_unit": "minutes", | ||
| "api_key_unit_count": 30, | ||
| "account_unit_count": 30, | ||
| "api_key_character_count": 0, | ||
| "character_count": 0 | ||
| } | ||
| ], | ||
| "api_key_character_count": 636, | ||
| "api_key_character_limit": 1000000000000, | ||
| "speech_to_text_milliseconds_count": 0, | ||
| "speech_to_text_milliseconds_limit": 0, | ||
| "speech_to_text_minutes_count": 30, | ||
| "speech_to_text_minutes_limit": 600, | ||
| "speech_to_speech_minutes_count": 12, | ||
| "speech_to_speech_minutes_limit": 600, | ||
| "start_time": "2025-05-13T09:18:42Z", | ||
| "end_time": "2025-06-13T09:18:42Z" | ||
| } | ||
| JSON; | ||
|
|
||
| public function testProAccountFields() | ||
| { | ||
| $usage = new Usage(self::PRO_RESPONSE); | ||
|
|
||
| $this->assertEquals(636, $usage->apiKeyCharacter->count); | ||
| $this->assertEquals(30, $usage->speechToTextMinutes->count); | ||
| $this->assertEquals(600, $usage->speechToSpeechMinutes->limit); | ||
| $this->assertEquals(0, $usage->speechToTextMilliseconds->limit); | ||
| $this->assertNull($usage->document); | ||
| $this->assertEquals('2025-05-13T09:18:42Z', $usage->startTime); | ||
| $this->assertEquals('speechToText', $usage->products[1]->productType); | ||
| $this->assertEquals('minutes', $usage->products[1]->billingUnit); | ||
| $this->assertEquals(30, $usage->products[1]->apiKeyUnitCount); | ||
| // Milliseconds are reported with a limit of 0, they must not count as a reached limit. | ||
| $this->assertFalse($usage->anyLimitReached()); | ||
| $this->assertStringContainsString('Speech-to-text minutes: 30 of 600', strval($usage)); | ||
| } | ||
|
|
||
| public function testSpeechLimitReached() | ||
| { | ||
| $json = str_replace('"speech_to_speech_minutes_count": 12', '"speech_to_speech_minutes_count": 600', self::PRO_RESPONSE); | ||
| $this->assertNotEquals($json, self::PRO_RESPONSE); | ||
| $this->assertTrue((new Usage($json))->anyLimitReached()); | ||
| } | ||
|
|
||
| public function testApiKeyAndAccountLimitReached() | ||
| { | ||
| $usage = new Usage( | ||
| '{"character_count":500001,"character_limit":500000,"api_key_character_count":500001,"api_key_character_limit":500000}' | ||
| ); | ||
|
|
||
| $this->assertTrue($usage->character->limitReached()); | ||
| $this->assertTrue($usage->apiKeyCharacter->limitReached()); | ||
| $this->assertTrue($usage->anyLimitReached()); | ||
| } | ||
|
|
||
| public function testFreeAccountResponseUnchanged() | ||
| { | ||
| $usage = new Usage('{"character_count":180,"character_limit":500000}'); | ||
|
|
||
| $this->assertEquals(180, $usage->character->count); | ||
| $this->assertNull($usage->apiKeyCharacter); | ||
| $this->assertNull($usage->speechToTextMinutes); | ||
| $this->assertSame([], $usage->products); | ||
| $this->assertNull($usage->startTime); | ||
| $this->assertFalse($usage->anyLimitReached()); | ||
| } | ||
| } | ||
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.