Skip to content

Commit

Permalink
feat: Apply changes to Report API (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
misantron authored Oct 23, 2023
1 parent 3efcf3b commit 1e4e4e2
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 93 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ coverage.xml
.phpdoc
phpdoc
phpDocumentor.phar
.phpunit.result.cache
20 changes: 19 additions & 1 deletion src/CrowdinApiClient/Api/Enterprise/GroupApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CrowdinApiClient\Api\AbstractApi;
use CrowdinApiClient\Model\Enterprise\Group;
use CrowdinApiClient\Model\Report;
use CrowdinApiClient\ModelCollection;

/**
Expand Down Expand Up @@ -64,7 +65,7 @@ public function create(array $data): ?Group
*/
public function update(Group $group): Group
{
return $this->_update('groups/' . $group->getId(), $group);
return $this->_update('groups/' . $group->getId(), $group);
}

/**
Expand All @@ -78,4 +79,21 @@ public function delete(int $groupID)
{
return $this->_delete('groups/' . $groupID);
}

/**
* Generate Group Report
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.groups.reports.post API Documentation
*
* @param int $groupID
* @param array $data
* string $data[name]<br>
* array $data[schema]
* @return Report|null
*/
public function report(int $groupID, array $data): ?Report
{
$path = sprintf('groups/%d/reports', $groupID);

return $this->_post($path, Report::class, $data);
}
}
31 changes: 31 additions & 0 deletions src/CrowdinApiClient/Api/Enterprise/ReportApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace CrowdinApiClient\Api\Enterprise;

use CrowdinApiClient\Api\AbstractApi;
use CrowdinApiClient\Model\Report;

/**
* Reports help to estimate costs, calculate translation costs, and identify the top members.
* Use API to generate Cost Estimate, Translation Cost, and Top Members reports.
* You can then export reports in .xlsx or .csv file formats.
* Report generation is an asynchronous operation and shall be completed with a sequence of API methods.
*
* @package Crowdin\Api\Enterprise
*/
class ReportApi extends AbstractApi
{
/**
* Generate Organization Report
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.reports.post API Documentation
*
* @param array $data
* string $data[name]<br>
* array $data[schema]
* @return Report|null
*/
public function generate(array $data): ?Report
{
return $this->_post('reports', Report::class, $data);
}
}
2 changes: 1 addition & 1 deletion src/CrowdinApiClient/Crowdin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @property \CrowdinApiClient\Api\Enterprise\WorkflowTemplateApi workflowTemplate
* @property \CrowdinApiClient\Api\Enterprise\WorkflowStepApi workflowStep
* @property \CrowdinApiClient\Api\FileApi|\CrowdinApiClient\Api\Enterprise\FileApi file
* @property \CrowdinApiClient\Api\ReportApi report
* @property \CrowdinApiClient\Api\Enterprise\ReportApi|\CrowdinApiClient\Api\ReportApi report
* @property \CrowdinApiClient\Api\SourceStringApi sourceString
* @property \CrowdinApiClient\Api\TranslationMemoryApi translationMemory
* @property \CrowdinApiClient\Api\WebhookApi webhook
Expand Down
76 changes: 75 additions & 1 deletion tests/CrowdinApiClient/Api/Enterprise/GroupApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CrowdinApiClient\Tests\Api\Enterprise;

use CrowdinApiClient\Model\Enterprise\Group;
use CrowdinApiClient\Model\Report;
use CrowdinApiClient\ModelCollection;

class GroupApiTest extends AbstractTestApi
Expand Down Expand Up @@ -41,7 +42,6 @@ public function testList()
$groups = $this->crowdin->group->list();

$this->assertInstanceOf(ModelCollection::class, $groups);
;
$this->assertCount(1, $groups);
$this->assertInstanceOf(Group::class, $groups[0]);
}
Expand Down Expand Up @@ -132,4 +132,78 @@ public function testDelete()

$this->crowdin->group->delete(1);
}

public function testReport(): void
{
$this->mockRequest([
'path' => '/groups/123/reports',
'method' => 'post',
'response' => '{
"identifier": "50fb3506-4127-4ba8-8296-f97dc7e3e0c3",
"status": "finished",
"progress": 100,
"attributes": {
"projectIds": [0],
"format": "xlsx",
"reportName": "costs-estimation",
"schema": {}
},
"createdAt": "2019-09-23T11:26:54+00:00",
"updatedAt": "2019-09-23T11:26:54+00:00",
"startedAt": "2019-09-23T11:26:54+00:00",
"finishedAt": "2019-09-23T11:26:54+00:00"
}'
]);

$data = [
'name' => 'group-translation-costs-pe',
'schema' => [
'projectIds' => [13],
'unit' => 'words',
'currency' => 'USD',
'format' => 'xlsx',
'baseRates' => [
'fullTranslation' => 0.1,
'proofread' => 0.12,
],
'individualRates' => [
[
'languageIds' => ['uk'],
'userIds' => [1],
'fullTranslation' => 0.1,
'proofread' => 0.12,
],
],
'netRateSchemes' => [
'tmMatch' => [
[
'matchType' => 'perfect',
'price' => 0.1,
]
],
'mtMatch' => [
[
'matchType' => '100',
'price' => 0.1,
]
],
'suggestionMatch' => [
[
'matchType' => '100',
'price' => 0.1,
]
],
],
'groupBy' => 'user',
'dateFrom' => '2019-09-23T07:00:14+00:00',
'dateTo' => '2019-09-27T07:00:14+00:00',
'userIds' => [13],
],
];

$report = $this->crowdin->group->report(123, $data);

$this->assertInstanceOf(Report::class, $report);
$this->assertEquals('50fb3506-4127-4ba8-8296-f97dc7e3e0c3', $report->getIdentifier());
}
}
103 changes: 45 additions & 58 deletions tests/CrowdinApiClient/Api/Enterprise/ReportApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@

namespace CrowdinApiClient\Tests\Api\Enterprise;

use CrowdinApiClient\Model\DownloadFile;
use CrowdinApiClient\Model\Report;

class ReportApiTest extends AbstractTestApi
{
public function testGenerate()
public function testGenerate(): void
{
$this->mockRequest([
'path' => '/projects/124/reports',
'path' => '/reports',
'method' => 'post',
'response' => '{
"identifier": "50fb3506-4127-4ba8-8296-f97dc7e3e0c3",
"status": "finished",
"progress": 100,
"attributes": {
"organizationId": 10,
"projectId": 124,
"projectIds": [0],
"format": "xlsx",
"reportName": "costs-estimation",
"schema": {}
Expand All @@ -30,64 +28,53 @@ public function testGenerate()
}'
]);

$report = $this->crowdin->report->generate(124, ['name' => 'costs-estimation', ['schema' => [
"unit" => "words",
"currency" => "USD",
"languageId" => "ach",
"format" => "xlsx",
"stepTypes" => [
$report = $this->crowdin->report->generate([
'name' => 'group-translation-costs-pe',
'schema' => [
'projectIds' => [13],
'unit' => 'words',
'currency' => 'USD',
'format' => 'xlsx',
'baseRates' => [
'fullTranslation' => 0.1,
'proofread' => 0.12,
],
'individualRates' => [
[
"type" => "Translate",
"mode" => "simple",
"regularRates" => [
[
"mode" => "tm_match",
"value" => 0.1
]
'languageIds' => ['uk'],
'userIds' => [1],
'fullTranslation' => 0.1,
'proofread' => 0.12,
],
],
'netRateSchemes' => [
'tmMatch' => [
[
'matchType' => 'perfect',
'price' => 0.1,
]
]
]
]]
],
'mtMatch' => [
[
'matchType' => '100',
'price' => 0.1,
]
],
'suggestionMatch' => [
[
'matchType' => '100',
'price' => 0.1,
]
],
],
'groupBy' => 'user',
'dateFrom' => '2019-09-23T07:00:14+00:00',
'dateTo' => '2019-09-27T07:00:14+00:00',
'userIds' => [13],
]
]);

$this->assertInstanceOf(Report::class, $report);
$this->assertEquals('50fb3506-4127-4ba8-8296-f97dc7e3e0c3', $report->getIdentifier());
}

public function testGet()
{
$this->mockRequestGet('/projects/124/reports/50fb3506-4127-4ba8-8296-f97dc7e3e0c3', '{
"identifier": "50fb3506-4127-4ba8-8296-f97dc7e3e0c3",
"status": "finished",
"progress": 100,
"attributes": {
"organizationId": 10,
"projectId": 124,
"format": "xlsx",
"reportName": "costs-estimation",
"schema": {}
},
"createdAt": "2019-09-23T11:26:54+00:00",
"updatedAt": "2019-09-23T11:26:54+00:00",
"startedAt": "2019-09-23T11:26:54+00:00",
"finishedAt": "2019-09-23T11:26:54+00:00"
}');

$report = $this->crowdin->report->get(124, '50fb3506-4127-4ba8-8296-f97dc7e3e0c3');
$this->assertInstanceOf(Report::class, $report);
$this->assertEquals('50fb3506-4127-4ba8-8296-f97dc7e3e0c3', $report->getIdentifier());
}

public function testDownload()
{
$this->mockRequestGet('/projects/124/reports/50fb3506-4127-4ba8-8296-f97dc7e3e0c3/download', '{
"data": {
"url": "https://production-enterprise-importer.downloads.crowdin.com/992000002/2/14.xliff?response-content-disposition=attachment%3B%20filename%3D%22APP.xliff%22&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIGJKLQV66ZXPMMEA%2F20190920%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190920T093121Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3600&X-Amz-Signature=439ebd69a1b7e4c23e6d17891a491c94f832e0c82e4692dedb35a6cd1e624b62",
"expireIn": "2019-09-20T10:31:21+00:00"
}
}');
$downloadFile = $this->crowdin->report->download(124, '50fb3506-4127-4ba8-8296-f97dc7e3e0c3');
$this->assertInstanceOf(DownloadFile::class, $downloadFile);
$this->assertEquals('https://production-enterprise-importer.downloads.crowdin.com/992000002/2/14.xliff?response-content-disposition=attachment%3B%20filename%3D%22APP.xliff%22&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIGJKLQV66ZXPMMEA%2F20190920%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190920T093121Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3600&X-Amz-Signature=439ebd69a1b7e4c23e6d17891a491c94f832e0c82e4692dedb35a6cd1e624b62', $downloadFile->getUrl());
}
}
Loading

0 comments on commit 1e4e4e2

Please sign in to comment.