From c35d9a84875431dc212543aa3bd3a44794c42ca5 Mon Sep 17 00:00:00 2001 From: jeremy-beaucousin Date: Thu, 8 Jun 2023 19:36:37 +0200 Subject: [PATCH 1/2] GRF-862 : Add api/rest/v1/category-media-files/%s/download route to the client --- spec/AkeneoPimClientSpec.php | 8 +++++ spec/Api/CategoryMediaFileApiSpec.php | 35 +++++++++++++++++++++ src/AkeneoPimClient.php | 11 +++++++ src/AkeneoPimClientBuilder.php | 2 ++ src/AkeneoPimClientInterface.php | 3 ++ src/Api/CategoryMediaFileApi.php | 32 +++++++++++++++++++ tests/Api/DownloadCategoryMediaFileTest.php | 32 +++++++++++++++++++ 7 files changed, 123 insertions(+) create mode 100644 spec/Api/CategoryMediaFileApiSpec.php create mode 100644 src/Api/CategoryMediaFileApi.php create mode 100644 tests/Api/DownloadCategoryMediaFileTest.php diff --git a/spec/AkeneoPimClientSpec.php b/spec/AkeneoPimClientSpec.php index 49435c7..80dc47e 100644 --- a/spec/AkeneoPimClientSpec.php +++ b/spec/AkeneoPimClientSpec.php @@ -29,6 +29,7 @@ use Akeneo\Pim\ApiClient\Api\MeasureFamilyApiInterface; use Akeneo\Pim\ApiClient\Api\MeasurementFamilyApiInterface; use Akeneo\Pim\ApiClient\Api\MediaFileApiInterface; +use Akeneo\Pim\ApiClient\Api\Operation\DownloadableResourceInterface; use Akeneo\Pim\ApiClient\Api\ProductApiInterface; use Akeneo\Pim\ApiClient\Api\ProductDraftApiInterface; use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApiInterface; @@ -50,6 +51,7 @@ function let( Authentication $authentication, ProductApiInterface $productApi, CategoryApiInterface $categoryApi, + DownloadableResourceInterface $categoryMediaFileApi, AttributeApiInterface $attributeApi, AttributeOptionApiInterface $attributeOptionApi, AttributeGroupApiInterface $attributeGroupApi, @@ -90,6 +92,7 @@ function let( $authentication, $productApi, $categoryApi, + $categoryMediaFileApi, $attributeApi, $attributeOptionApi, $attributeGroupApi, @@ -158,6 +161,11 @@ function it_gets_category_api($categoryApi) $this->getCategoryApi()->shouldReturn($categoryApi); } + function it_gets_category_media_file_api($categoryMediaFileApi) + { + $this->getCategoryMediaFileApi()->shouldReturn($categoryMediaFileApi); + } + function it_gets_attribute_api($attributeApi) { $this->getAttributeApi()->shouldReturn($attributeApi); diff --git a/spec/Api/CategoryMediaFileApiSpec.php b/spec/Api/CategoryMediaFileApiSpec.php new file mode 100644 index 0000000..3c3a3e4 --- /dev/null +++ b/spec/Api/CategoryMediaFileApiSpec.php @@ -0,0 +1,35 @@ +beConstructedWith($resourceClient); + } + + function it_is_initializable() + { + $this->shouldHaveType(CategoryMediaFileApi::class); + $this->shouldImplement(DownloadableResourceInterface::class); + } + + function it_downloads_a_media_file($resourceClient, ResponseInterface $response, StreamInterface $streamBody) + { + $resourceClient + ->getStreamedResource(CategoryMediaFileApi::MEDIA_FILE_DOWNLOAD_URI, ['42.jpg']) + ->willReturn($response); + + $this->download('42.jpg')->shouldReturn($response); + } +} diff --git a/src/AkeneoPimClient.php b/src/AkeneoPimClient.php index 9ee84f1..c9caa22 100644 --- a/src/AkeneoPimClient.php +++ b/src/AkeneoPimClient.php @@ -19,6 +19,7 @@ use Akeneo\Pim\ApiClient\Api\AttributeGroupApiInterface; use Akeneo\Pim\ApiClient\Api\AttributeOptionApiInterface; use Akeneo\Pim\ApiClient\Api\CategoryApiInterface; +use Akeneo\Pim\ApiClient\Api\CategoryMediaFileApi; use Akeneo\Pim\ApiClient\Api\ChannelApiInterface; use Akeneo\Pim\ApiClient\Api\CurrencyApiInterface; use Akeneo\Pim\ApiClient\Api\FamilyApiInterface; @@ -27,6 +28,7 @@ use Akeneo\Pim\ApiClient\Api\MeasureFamilyApiInterface; use Akeneo\Pim\ApiClient\Api\MeasurementFamilyApiInterface; use Akeneo\Pim\ApiClient\Api\MediaFileApiInterface; +use Akeneo\Pim\ApiClient\Api\Operation\DownloadableResourceInterface; use Akeneo\Pim\ApiClient\Api\ProductApiInterface; use Akeneo\Pim\ApiClient\Api\ProductDraftApiInterface; use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApiInterface; @@ -54,6 +56,7 @@ public function __construct( protected Authentication $authentication, protected ProductApiInterface $productApi, protected CategoryApiInterface $categoryApi, + protected DownloadableResourceInterface $categoryMediaFileApi, protected AttributeApiInterface $attributeApi, protected AttributeOptionApiInterface $attributeOptionApi, protected AttributeGroupApiInterface $attributeGroupApi, @@ -124,6 +127,14 @@ public function getCategoryApi(): CategoryApiInterface return $this->categoryApi; } + /** + * {@inheritdoc} + */ + public function getCategoryMediaFileApi(): DownloadableResourceInterface + { + return $this->categoryMediaFileApi; + } + /** * {@inheritdoc} */ diff --git a/src/AkeneoPimClientBuilder.php b/src/AkeneoPimClientBuilder.php index 65c5879..febc4d6 100644 --- a/src/AkeneoPimClientBuilder.php +++ b/src/AkeneoPimClientBuilder.php @@ -20,6 +20,7 @@ use Akeneo\Pim\ApiClient\Api\AttributeOptionApi; use Akeneo\Pim\ApiClient\Api\AuthenticationApi; use Akeneo\Pim\ApiClient\Api\CategoryApi; +use Akeneo\Pim\ApiClient\Api\CategoryMediaFileApi; use Akeneo\Pim\ApiClient\Api\ChannelApi; use Akeneo\Pim\ApiClient\Api\CurrencyApi; use Akeneo\Pim\ApiClient\Api\FamilyApi; @@ -213,6 +214,7 @@ protected function buildAuthenticatedClient(Authentication $authentication): Ake $authentication, new ProductApi($resourceClient, $pageFactory, $cursorFactory), new CategoryApi($resourceClientWithCache, $pageFactory, $cursorFactory), + new CategoryMediaFileApi($resourceClientWithCache), new AttributeApi($resourceClientWithCache, $pageFactory, $cursorFactory), new AttributeOptionApi($resourceClientWithCache, $pageFactory, $cursorFactory), new AttributeGroupApi($resourceClientWithCache, $pageFactory, $cursorFactory), diff --git a/src/AkeneoPimClientInterface.php b/src/AkeneoPimClientInterface.php index 3c3ea2a..3350fbf 100644 --- a/src/AkeneoPimClientInterface.php +++ b/src/AkeneoPimClientInterface.php @@ -27,6 +27,7 @@ use Akeneo\Pim\ApiClient\Api\MeasureFamilyApiInterface; use Akeneo\Pim\ApiClient\Api\MeasurementFamilyApiInterface; use Akeneo\Pim\ApiClient\Api\MediaFileApiInterface; +use Akeneo\Pim\ApiClient\Api\Operation\DownloadableResourceInterface; use Akeneo\Pim\ApiClient\Api\ProductApiInterface; use Akeneo\Pim\ApiClient\Api\ProductDraftApiInterface; use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApiInterface; @@ -57,6 +58,8 @@ public function getProductApi(): ProductApiInterface; public function getCategoryApi(): CategoryApiInterface; + public function getCategoryMediaFileApi(): DownloadableResourceInterface; + public function getAttributeApi(): AttributeApiInterface; public function getAttributeOptionApi(): AttributeOptionApiInterface; diff --git a/src/Api/CategoryMediaFileApi.php b/src/Api/CategoryMediaFileApi.php new file mode 100644 index 0000000..8d9024f --- /dev/null +++ b/src/Api/CategoryMediaFileApi.php @@ -0,0 +1,32 @@ + + * @copyright 2017 Akeneo SAS (http://www.akeneo.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ +class CategoryMediaFileApi implements DownloadableResourceInterface +{ + public const MEDIA_FILE_DOWNLOAD_URI = 'api/rest/v1/category-media-files/%s/download'; + + public function __construct( + protected ResourceClientInterface $resourceClient, + ) { + } + + /** + * {@inheritdoc} + */ + public function download(string $code): ResponseInterface + { + return $this->resourceClient->getStreamedResource(static::MEDIA_FILE_DOWNLOAD_URI, [$code]); + } +} diff --git a/tests/Api/DownloadCategoryMediaFileTest.php b/tests/Api/DownloadCategoryMediaFileTest.php new file mode 100644 index 0000000..ccb3e07 --- /dev/null +++ b/tests/Api/DownloadCategoryMediaFileTest.php @@ -0,0 +1,32 @@ +server->setResponseOfPath( + '/' . sprintf(CategoryMediaFileApi::MEDIA_FILE_DOWNLOAD_URI, '/f/b/0/6/fb068ccc9e3c5609d73c28d852812ba5faeeab28_akeneo.png'), + new ResponseStack( + new Response(file_get_contents($expectedMediaFilePath), [], 201) + ) + ); + + $api = $this->createClientByPassword()->getCategoryMediaFileApi(); + $mediaFile = $api->download('/f/b/0/6/fb068ccc9e3c5609d73c28d852812ba5faeeab28_akeneo.png'); + + Assert::assertSame('GET', $this->server->getLastRequest()->jsonSerialize()[RequestInfo::JSON_KEY_METHOD]); + Assert::assertInstanceOf(ResponseInterface::class, $mediaFile); + Assert::assertSame(file_get_contents($expectedMediaFilePath), $mediaFile->getBody()->getContents()); + } +} From 652957423cf623b38cdea40661bd58a2640462e5 Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 12 Jun 2023 14:25:32 +0200 Subject: [PATCH 2/2] GRF-862: fix phpdoc --- src/Api/CategoryMediaFileApi.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Api/CategoryMediaFileApi.php b/src/Api/CategoryMediaFileApi.php index 8d9024f..ea291b2 100644 --- a/src/Api/CategoryMediaFileApi.php +++ b/src/Api/CategoryMediaFileApi.php @@ -7,10 +7,9 @@ use Psr\Http\Message\ResponseInterface; /** - * API implementation to manage the media files for the products. + * API implementation to manage enriched categories media files. * - * @author Laurent Petard - * @copyright 2017 Akeneo SAS (http://www.akeneo.com) + * @copyright 2023 Akeneo SAS (http://www.akeneo.com) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ class CategoryMediaFileApi implements DownloadableResourceInterface