Skip to content

Commit

Permalink
fix: If-None-Match header reset in Translation API (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhdnb authored Jan 16, 2025
1 parent 593e50e commit fd3c939
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/CrowdinApiClient/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function setHeaders(array $headers)
return $this;
}

public function hasHeader($header)
public function hasHeader($header): bool
{
return isset($this->headers[strtolower($header)]);
}
Expand Down
17 changes: 13 additions & 4 deletions src/CrowdinApiClient/Api/TranslationApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ public function getPreTranslation(int $projectId, string $preTranslationId): ?Pr
* boolean $params[preserveFolderHierarchy] Default: false<br>
* @return TranslationProjectDirectory|null
*/
public function buildProjectDirectoryTranslation(int $projectId, int $directoryId, array $params = []): ?TranslationProjectDirectory
{
public function buildProjectDirectoryTranslation(
int $projectId,
int $directoryId,
array $params = []
): ?TranslationProjectDirectory {
$path = sprintf('projects/%d/translations/builds/directories/%d', $projectId, $directoryId);

return $this->_post($path, TranslationProjectDirectory::class, $params);
Expand All @@ -96,12 +99,18 @@ public function buildProjectDirectoryTranslation(int $projectId, int $directoryI
* boolean $params[skipUntranslatedFiles] true value can't be used with skipUntranslatedStrings=true in same request<br>
* boolean $params[exportApprovedOnly]
*/
public function buildProjectFileTranslation(int $projectId, int $fileId, array $params = [], string $ifNoneMatch = null): ?DownloadFile
{
public function buildProjectFileTranslation(
int $projectId,
int $fileId,
array $params = [],
?string $ifNoneMatch = null
): ?DownloadFile {
$path = sprintf('projects/%d/translations/builds/files/%d', $projectId, $fileId);

if ($ifNoneMatch) {
$this->setHeader('If-None-Match', $ifNoneMatch);
} else {
$this->removeHeader('If-None-Match');
}

return $this->_post($path, DownloadFileTranslation::class, $params);
Expand Down
6 changes: 4 additions & 2 deletions tests/CrowdinApiClient/Api/AbstractTestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ public function mockRequest(array $params)
if (isset($params['body'])) {
$this->assertEquals($params['body'], $options['body']);
}
if (isset($params['header'])) {
$this->assertEquals($params['header'], $options['header']);

if (isset($params['headers'])) {
$this->assertEquals($params['headers'], $options['headers']);
}

return $params['response'] ?? '';
}));
}
Expand Down
6 changes: 4 additions & 2 deletions tests/CrowdinApiClient/Api/Enterprise/AbstractTestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public function mockRequest(array $params)
if (isset($params['body'])) {
$this->assertEquals($params['body'], $options['body']);
}
if (isset($params['header'])) {
$this->assertEquals($params['header'], $options['header']);

if (isset($params['headers'])) {
$this->assertEquals($params['headers'], $options['headers']);
}

return $params['response'] ?? '';
}));
}
Expand Down
4 changes: 0 additions & 4 deletions tests/CrowdinApiClient/Api/Enterprise/TranslationApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
use CrowdinApiClient\Model\TranslationProjectDirectory;
use CrowdinApiClient\ModelCollection;

/**
* Class TranslationApiTest
* @package Crowdin\Tests\Api
*/
class TranslationApiTest extends AbstractTestApi
{
public function testApplyPreTranslation()
Expand Down
21 changes: 11 additions & 10 deletions tests/CrowdinApiClient/Api/StorageApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use CrowdinApiClient\Model\Storage;
use CrowdinApiClient\ModelCollection;
use CrowdinApiClient\Utility\Mimetypes;
use SplFileInfo;

class StorageApiTest extends AbstractTestApi
{
Expand Down Expand Up @@ -68,21 +68,22 @@ public function testDelete()
$this->crowdin->storage->delete(1);
}

public function testCreate()
public function testCreate(): void
{
$fileObject = new \SplFileInfo(__FILE__);
$fileObject = new SplFileInfo(__FILE__);

$this->mockRequest([
'method' => 'post',
'uri' => 'https://api.crowdin.com/api/v2/storages',
'response' => '{
"data": {
"id": 1
}
}',
'response' => json_encode([
'data' => [
'id' => 1
]
]),
'headers' => [
'Content-Type' => Mimetypes::getInstance()->fromFilename($fileObject->getFilename()),
'Crowdin-API-FileName' => $fileObject->getFilename(),
'Content-Type' => 'application/octet-stream',
'Crowdin-API-FileName' => 'StorageApiTest.php',
'Authorization' => 'Bearer access_token',
],
]);

Expand Down
88 changes: 56 additions & 32 deletions tests/CrowdinApiClient/Api/TranslationApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@
use CrowdinApiClient\Model\TranslationProjectBuild;
use CrowdinApiClient\ModelCollection;

/**
* Class TranslationApiTest
* @package Crowdin\Tests\Api
*/
class TranslationApiTest extends AbstractTestApi
{
public function testApplyPreTranslation()
{
$params = [
'languageIds' => ['uk'],
'fileIds' => [0]
'fileIds' => [0],
];

$this->mockRequest([
Expand Down Expand Up @@ -50,7 +46,7 @@ public function testApplyPreTranslation()
"startedAt": "2019-11-13T08:17:22Z",
"finishedAt": "2019-11-13T08:17:22Z"
}
}'
}',
]);

$preTranslation = $this->crowdin->translation->applyPreTranslation(2, $params);
Expand All @@ -60,7 +56,9 @@ public function testApplyPreTranslation()

public function testGetPreTranslation()
{
$this->mockRequestGet('/projects/2/pre-translations/9e7de270-4f83-41cb-b606-2f90631f26e2', '{
$this->mockRequestGet(
'/projects/2/pre-translations/9e7de270-4f83-41cb-b606-2f90631f26e2',
'{
"data": {
"identifier": "9e7de270-4f83-41cb-b606-2f90631f26e2",
"status": "created",
Expand All @@ -86,36 +84,49 @@ public function testGetPreTranslation()
"startedAt": "2019-11-13T08:17:22Z",
"finishedAt": "2019-11-13T08:17:22Z"
}
}');
}'
);

$preTranslation = $this->crowdin->translation->getPreTranslation(2, '9e7de270-4f83-41cb-b606-2f90631f26e2');

$this->assertInstanceOf(PreTranslation::class, $preTranslation);
$this->assertEquals('9e7de270-4f83-41cb-b606-2f90631f26e2', $preTranslation->getIdentifier());
}

public function testBuildProjectFileTranslation()
public function testBuildProjectFileTranslation(): void
{
$this->mockRequest([
'uri' => 'https://api.crowdin.com/api/v2/projects/1/translations/builds/files/2',
'method' => 'post',
'response' => '{
"data": {
"url": "https://foo.com/file",
"expireIn": "2019-09-20T10:31:21+00:00"
}
}'
'headers' => [
'content-type' => 'application/json',
'if-none-match' => 'bfc13a64729c4290ef5b2c2730249c88ca92d82d',
'Authorization' => 'Bearer access_token',
],
'response' => json_encode([
'data' => [
'url' => 'https://foo.com/file',
'expireIn' => '2019-09-20T10:31:21+00:00',
],
]),
]);

$file = $this->crowdin->translation->buildProjectFileTranslation(1, 2, ['targetLanguageId' => 'uk']);
$file = $this->crowdin->translation->buildProjectFileTranslation(
1,
2,
['targetLanguageId' => 'uk'],
'bfc13a64729c4290ef5b2c2730249c88ca92d82d'
);

$this->assertInstanceOf(DownloadFile::class, $file);
$this->assertEquals('https://foo.com/file', $file->getUrl());
}

public function testListProjectBuilds()
{
$this->mockRequestGet('/projects/2/translations/builds', '{
$this->mockRequestGet(
'/projects/2/translations/builds',
'{
"data": [
{
"data": {
Expand All @@ -140,7 +151,8 @@ public function testListProjectBuilds()
"limit": 0
}
]
}');
}'
);

$translationProjectBuilds = $this->crowdin->translation->getProjectBuilds(2);
$this->assertInstanceOf(ModelCollection::class, $translationProjectBuilds);
Expand Down Expand Up @@ -173,9 +185,9 @@ public function testBuildProject()
'options' => [
'body' => [
'branchId' => 2,
'targetLanguageIds' => ['uk']
]
]
'targetLanguageIds' => ['uk'],
],
],
]);

$params = [
Expand All @@ -197,7 +209,9 @@ public function testBuildProject()

public function testGetProjectBuildStatus()
{
$this->mockRequestGet('/projects/2/translations/builds/2', '{
$this->mockRequestGet(
'/projects/2/translations/builds/2',
'{
"data": {
"id": 2,
"projectId": 2,
Expand All @@ -212,7 +226,8 @@ public function testGetProjectBuildStatus()
"currentFileId": 1
}
}
}');
}'
);

$projectBuild = $this->crowdin->translation->getProjectBuildStatus(2, 2);

Expand All @@ -222,16 +237,22 @@ public function testGetProjectBuildStatus()

public function testDownloadProjectBuild()
{
$this->mockRequestGet('/projects/2/translations/builds/2/download', '{
$this->mockRequestGet(
'/projects/2/translations/builds/2/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"
}
}');
}'
);

$file = $this->crowdin->translation->downloadProjectBuild(2, 2);
$this->assertInstanceOf(DownloadFile::class, $file);
$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', $file->getUrl());
$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',
$file->getUrl()
);
}

public function testDeleteProjectBuild()
Expand Down Expand Up @@ -266,8 +287,8 @@ public function testUploadTranslations()
'body' => [
'storageId' => 13,
'fileId' => 2,
]
]
],
],
]);

$data = $this->crowdin->translation->uploadTranslations(8, 'uk', $params);
Expand All @@ -278,15 +299,15 @@ public function testUploadTranslations()
'projectId' => 8,
'storageId' => 34,
'languageId' => 'uk',
'fileId' => 56
'fileId' => 56,
], $data);
}

public function testExportProjectTranslation()
{
$params = [
'targetLanguageId' => 'en',
'fileIds' => [7, 14, 15]
'fileIds' => [7, 14, 15],
];

$this->mockRequest(
Expand All @@ -298,12 +319,15 @@ public function testExportProjectTranslation()
"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"
}
}'
}',
]
);

$file = $this->crowdin->translation->exportProjectTranslation(2, $params);
$this->assertInstanceOf(DownloadFile::class, $file);
$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', $file->getUrl());
$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',
$file->getUrl()
);
}
}

0 comments on commit fd3c939

Please sign in to comment.