Skip to content

Commit

Permalink
Merge pull request #264 from akeneo/GRF-862
Browse files Browse the repository at this point in the history
GRF-862 : Add api/rest/v1/category-media-files/%s/download route to the client
  • Loading branch information
jeremyBeaucousinAkeneo authored Jun 12, 2023
2 parents 58565f9 + 6529574 commit 4e38b9a
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spec/AkeneoPimClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,6 +51,7 @@ function let(
Authentication $authentication,
ProductApiInterface $productApi,
CategoryApiInterface $categoryApi,
DownloadableResourceInterface $categoryMediaFileApi,
AttributeApiInterface $attributeApi,
AttributeOptionApiInterface $attributeOptionApi,
AttributeGroupApiInterface $attributeGroupApi,
Expand Down Expand Up @@ -90,6 +92,7 @@ function let(
$authentication,
$productApi,
$categoryApi,
$categoryMediaFileApi,
$attributeApi,
$attributeOptionApi,
$attributeGroupApi,
Expand Down Expand Up @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions spec/Api/CategoryMediaFileApiSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace spec\Akeneo\Pim\ApiClient\Api;

use Akeneo\Pim\ApiClient\Api\CategoryMediaFileApi;
use Akeneo\Pim\ApiClient\Api\Operation\DownloadableResourceInterface;
use Akeneo\Pim\ApiClient\Client\ResourceClientInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class CategoryMediaFileApiSpec extends ObjectBehavior
{
function let(
ResourceClientInterface $resourceClient,
) {
$this->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);
}
}
11 changes: 11 additions & 0 deletions src/AkeneoPimClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -124,6 +127,14 @@ public function getCategoryApi(): CategoryApiInterface
return $this->categoryApi;
}

/**
* {@inheritdoc}
*/
public function getCategoryMediaFileApi(): DownloadableResourceInterface
{
return $this->categoryMediaFileApi;
}

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 2 additions & 0 deletions src/AkeneoPimClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions src/AkeneoPimClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,6 +58,8 @@ public function getProductApi(): ProductApiInterface;

public function getCategoryApi(): CategoryApiInterface;

public function getCategoryMediaFileApi(): DownloadableResourceInterface;

public function getAttributeApi(): AttributeApiInterface;

public function getAttributeOptionApi(): AttributeOptionApiInterface;
Expand Down
31 changes: 31 additions & 0 deletions src/Api/CategoryMediaFileApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Akeneo\Pim\ApiClient\Api;

use Akeneo\Pim\ApiClient\Api\Operation\DownloadableResourceInterface;
use Akeneo\Pim\ApiClient\Client\ResourceClientInterface;
use Psr\Http\Message\ResponseInterface;

/**
* API implementation to manage enriched categories media files.
*
* @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
{
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]);
}
}
32 changes: 32 additions & 0 deletions tests/Api/DownloadCategoryMediaFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Akeneo\Pim\ApiClient\tests\Api;

use Akeneo\Pim\ApiClient\Api\CategoryMediaFileApi;
use donatj\MockWebServer\RequestInfo;
use donatj\MockWebServer\Response;
use donatj\MockWebServer\ResponseStack;
use PHPUnit\Framework\Assert;
use Psr\Http\Message\ResponseInterface;

class DownloadCategoryMediaFileTest extends ApiTestCase
{
public function test_download_media_file()
{
$expectedMediaFilePath = realpath(__DIR__ . '/../fixtures/akeneo.png');

$this->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());
}
}

0 comments on commit 4e38b9a

Please sign in to comment.