Skip to content

Commit

Permalink
feat: Add the Screenshot Labels management APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Elliot Bruneel committed Oct 3, 2023
1 parent ec0823f commit 9e15887
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/CrowdinApiClient/Api/LabelApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace CrowdinApiClient\Api;

use CrowdinApiClient\Http\ResponseDecorator\ResponseModelListDecorator;
use CrowdinApiClient\Model\Label;
use CrowdinApiClient\Model\Screenshot;
use CrowdinApiClient\ModelCollection;

/**
Expand Down Expand Up @@ -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);
}
}
119 changes: 119 additions & 0 deletions tests/CrowdinApiClient/Api/LabelApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CrowdinApiClient\Tests\Api;

use CrowdinApiClient\Model\Label;
use CrowdinApiClient\Model\Screenshot;
use CrowdinApiClient\ModelCollection;

class LabelApiTest extends AbstractTestApi
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 9e15887

Please sign in to comment.