From 9e158878ea9cac5e450e1c1d4721de2bd5e7a98f Mon Sep 17 00:00:00 2001 From: Elliot Bruneel Date: Sun, 1 Oct 2023 18:26:14 +0200 Subject: [PATCH] feat: Add the Screenshot Labels management APIs --- src/CrowdinApiClient/Api/LabelApi.php | 49 ++++++++ tests/CrowdinApiClient/Api/LabelApiTest.php | 119 ++++++++++++++++++++ 2 files changed, 168 insertions(+) diff --git a/src/CrowdinApiClient/Api/LabelApi.php b/src/CrowdinApiClient/Api/LabelApi.php index 1f6713e1..a799438d 100644 --- a/src/CrowdinApiClient/Api/LabelApi.php +++ b/src/CrowdinApiClient/Api/LabelApi.php @@ -2,7 +2,9 @@ namespace CrowdinApiClient\Api; +use CrowdinApiClient\Http\ResponseDecorator\ResponseModelListDecorator; use CrowdinApiClient\Model\Label; +use CrowdinApiClient\Model\Screenshot; use CrowdinApiClient\ModelCollection; /** @@ -88,4 +90,51 @@ public function delete(int $projectId, int $labelId) $path = sprintf('projects/%d/labels/%d', $projectId, $labelId); return $this->_delete($path); } + + /** + * Assign Label to Screenshots + * @link https://developer.crowdin.com/api/v2/#operation/api.projects.labels.screenshots.post API Documentation + * @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.labels.screenshots.post API Documentation Enterprise + * + * @param int $projectId + * @param int $labelId + * @param array $data + * string[] $data[screenshotIds] required + * @return ModelCollection + */ + public function assignScreenshots(int $projectId, int $labelId, array $data): ModelCollection + { + $path = sprintf('projects/%d/labels/%d/screenshots', $projectId, $labelId); + + $options = [ + 'body' => json_encode($data), + 'headers' => ['Content-Type' => 'application/json'] + ]; + + return $this->client->apiRequest('post', $path, new ResponseModelListDecorator(Screenshot::class), $options); + } + + /** + * Unassign Label from Screenshots + * + * @link https://developer.crowdin.com/api/v2/#operation/api.projects.labels.screenshots.deleteMany API Documentation + * @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.labels.screenshots.deleteMany API Documentation Enterprise + * + * @param int $projectId + * @param int $labelId + * @param array $data + * string[] $data[screenshotIds] required + * @return ModelCollection + */ + public function unassignScreenshots(int $projectId, int $labelId, array $data): ModelCollection + { + $path = sprintf('projects/%d/labels/%d/screenshots', $projectId, $labelId); + + $options = [ + 'body' => json_encode($data), + 'headers' => ['Content-Type' => 'application/json'] + ]; + + return $this->client->apiRequest('delete', $path, new ResponseModelListDecorator(Screenshot::class), $options); + } } diff --git a/tests/CrowdinApiClient/Api/LabelApiTest.php b/tests/CrowdinApiClient/Api/LabelApiTest.php index ca45b20c..d251f412 100644 --- a/tests/CrowdinApiClient/Api/LabelApiTest.php +++ b/tests/CrowdinApiClient/Api/LabelApiTest.php @@ -3,6 +3,7 @@ namespace CrowdinApiClient\Tests\Api; use CrowdinApiClient\Model\Label; +use CrowdinApiClient\Model\Screenshot; use CrowdinApiClient\ModelCollection; class LabelApiTest extends AbstractTestApi @@ -99,4 +100,122 @@ public function testDelete() $this->mockRequestDelete('/projects/2/labels/34'); $this->crowdin->label->delete(2, 34); } + + public function testAssignScreenshots() + { + $params = [ + 'screenshotIds' => ['2'], + ]; + + $this->mockRequest([ + 'path' => '/projects/2/labels/34/screenshots', + 'method' => 'post', + 'body' => json_encode($params), + 'response' => '{ + "data": [ + { + "data": { + "id": 2, + "userId": 6, + "url": "https://production-enterprise-screenshots.downloads.crowdin.com/992000002/6/2/middle.jpg?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIGJKLQV66ZXPMMEA%2F20190923%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190923T093016Z&X-Amz-SignedHeaders=host&X-Amz-Expires=120&X-Amz-Signature=8df06f57594f7d1804b7c037629f6916224415e9b935c4f6619fbe002fb25e73", + "name": "translate_with_siri.jpg", + "size": { + "width": 267, + "height": 176 + }, + "tagsCount": 0, + "tags": [ + { + "id": 98, + "screenshotId": 2, + "stringId": 2822, + "position": { + "x": 474, + "y": 147, + "width": 490, + "height": 99 + }, + "createdAt": "2019-09-23T09:35:31+00:00" + } + ], + "labels": [ + 1 + ], + "createdAt": "2019-09-23T09:29:19+00:00", + "updatedAt": "2019-09-23T09:29:19+00:00" + } + } + ], + "pagination": { + "offset": 0, + "limit": 25 + } + }' + ]); + + $screenshots = $this->crowdin->label->assignScreenshots(2, 34, $params); + $this->assertInstanceOf(ModelCollection::class, $screenshots); + $this->assertCount(1, $screenshots); + $this->assertInstanceOf(Screenshot::class, $screenshots[0]); + $this->assertEquals(2, $screenshots[0]->getId()); + } + + public function testUnassignScreenshots() + { + $params = [ + 'screenshotIds' => ['2'], + ]; + + $this->mockRequest([ + 'path' => '/projects/2/labels/34/screenshots', + 'method' => 'delete', + 'body' => json_encode($params), + 'response' => '{ + "data": [ + { + "data": { + "id": 2, + "userId": 6, + "url": "https://production-enterprise-screenshots.downloads.crowdin.com/992000002/6/2/middle.jpg?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIGJKLQV66ZXPMMEA%2F20190923%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190923T093016Z&X-Amz-SignedHeaders=host&X-Amz-Expires=120&X-Amz-Signature=8df06f57594f7d1804b7c037629f6916224415e9b935c4f6619fbe002fb25e73", + "name": "translate_with_siri.jpg", + "size": { + "width": 267, + "height": 176 + }, + "tagsCount": 0, + "tags": [ + { + "id": 98, + "screenshotId": 2, + "stringId": 2822, + "position": { + "x": 474, + "y": 147, + "width": 490, + "height": 99 + }, + "createdAt": "2019-09-23T09:35:31+00:00" + } + ], + "labels": [ + 1 + ], + "createdAt": "2019-09-23T09:29:19+00:00", + "updatedAt": "2019-09-23T09:29:19+00:00" + } + } + ], + "pagination": { + "offset": 0, + "limit": 25 + } + }' + ]); + + $screenshots = $this->crowdin->label->unassignScreenshots(2, 34, $params); + $this->assertInstanceOf(ModelCollection::class, $screenshots); + $this->assertCount(1, $screenshots); + $this->assertInstanceOf(Screenshot::class, $screenshots[0]); + $this->assertEquals(2, $screenshots[0]->getId()); + } }