Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/Api/AssociationTypeApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function it_upserts_an_association_type($resourceClient)
function it_upserts_a_list_of_association_types($resourceClient, UpsertResourceListResponse $response)
{
$resourceClient
->upsertResourceList(
->upsertStreamResourceList(
AssociationTypeApi::ASSOCIATION_TYPES_URI,
[],
[
Expand Down
2 changes: 1 addition & 1 deletion spec/Api/AttributeApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function it_upserts_an_attribute($resourceClient)
function it_upserts_a_list_of_attributes($resourceClient, UpsertResourceListResponse $response)
{
$resourceClient
->upsertResourceList(
->upsertStreamResourceList(
AttributeApi::ATTRIBUTES_URI,
[],
[
Expand Down
2 changes: 1 addition & 1 deletion spec/Api/AttributeGroupApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function it_upserts_an_attribute_group($resourceClient)
function it_upserts_a_list_of_attribute_groups($resourceClient, UpsertResourceListResponse $response)
{
$resourceClient
->upsertResourceList(
->upsertStreamResourceList(
AttributeGroupApi::ATTRIBUTE_GROUPS_URI,
[],
[
Expand Down
2 changes: 1 addition & 1 deletion spec/Api/AttributeOptionApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function it_upserts_an_attribute_option($resourceClient)

function it_upserts_a_list_of_attribute_options($resourceClient, UpsertResourceListResponse $response)
{
$resourceClient->upsertResourceList(AttributeOptionApi::ATTRIBUTE_OPTIONS_URI, ['foo'], [
$resourceClient->upsertStreamResourceList(AttributeOptionApi::ATTRIBUTE_OPTIONS_URI, ['foo'], [
['code' => 'bar', 'attribute' => 'foo', 'sort_order' => 42],
['code' => 'fighters', 'attribute' => 'foo', 'sort_order' => 43]
])->willReturn($response);
Expand Down
2 changes: 1 addition & 1 deletion spec/Api/CategoryApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function it_upserts_a_category($resourceClient)
function it_upserts_a_list_of_categories($resourceClient, UpsertResourceListResponse $response)
{
$resourceClient
->upsertResourceList(
->upsertStreamResourceList(
CategoryApi::CATEGORIES_URI,
[],
[
Expand Down
2 changes: 1 addition & 1 deletion spec/Api/ChannelApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function it_upserts_a_channel($resourceClient)
function it_upserts_a_list_of_channel($resourceClient, UpsertResourceListResponse $response)
{
$resourceClient
->upsertResourceList(
->upsertStreamResourceList(
ChannelApi::CHANNELS_URI,
[],
[
Expand Down
2 changes: 1 addition & 1 deletion spec/Api/FamilyApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function it_upserts_a_family($resourceClient)
function it_upserts_a_list_of_families($resourceClient, UpsertResourceListResponse $response)
{
$resourceClient
->upsertResourceList(
->upsertStreamResourceList(
FamilyApi::FAMILIES_URI,
[],
[
Expand Down
2 changes: 1 addition & 1 deletion spec/Api/FamilyVariantApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function it_upserts_a_list_of_family_variants($resourceClient, UpsertResourceLis
];

$resourceClient
->upsertResourceList(FamilyVariantApi::FAMILY_VARIANTS_URI, ['boots'], $data)
->upsertStreamResourceList(FamilyVariantApi::FAMILY_VARIANTS_URI, ['boots'], $data)
->willReturn($response);

$this->upsertList('boots', $data)->shouldReturn($response);
Expand Down
2 changes: 1 addition & 1 deletion spec/Api/ProductApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function it_deletes_a_product($resourceClient)
function it_upserts_a_list_of_products($resourceClient, UpsertResourceListResponse $response)
{
$resourceClient
->upsertResourceList(
->upsertStreamResourceList(
ProductApi::PRODUCTS_URI,
[],
[
Expand Down
2 changes: 1 addition & 1 deletion spec/Api/ProductModelApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function it_upserts_a_list_of_product_models($resourceClient, UpsertResourceList
]
];

$resourceClient->upsertResourceList(ProductModelApi::PRODUCT_MODELS_URI, [], $data)->willReturn($response);
$resourceClient->upsertStreamResourceList(ProductModelApi::PRODUCT_MODELS_URI, [], $data)->willReturn($response);

$this->upsertList($data)->shouldReturn($response);
}
Expand Down
75 changes: 70 additions & 5 deletions spec/Client/ResourceClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ function it_upserts_a_resource(
->shouldReturn(201);
}

function it_upserts_a_list_of_resources_from_an_array(
function it_upserts_a_list_of_streamed_resources_from_an_array(
$httpClient,
$uriGenerator,
$responseFactory,
Expand Down Expand Up @@ -231,7 +231,7 @@ function it_upserts_a_list_of_resources_from_an_array(
$responseFactory->create($responseBodyStream)->willReturn($listResponse);

$this
->upsertResourceList(
->upsertStreamResourceList(
'api/rest/v1/categories',
[],
[
Expand All @@ -244,7 +244,7 @@ function it_upserts_a_list_of_resources_from_an_array(
->shouldReturn($listResponse);
}

function it_upserts_a_list_of_resources_from_an_stream(
function it_upserts_a_list_of_streamed_resources_from_an_stream(
$httpClient,
$uriGenerator,
$responseFactory,
Expand All @@ -271,10 +271,75 @@ function it_upserts_a_list_of_resources_from_an_stream(
$responseFactory->create($responseBodyStream)->willReturn($listResponse);

$this
->upsertResourceList('api/rest/v1/categories', [], $resourcesStream)
->upsertStreamResourceList('api/rest/v1/categories', [], $resourcesStream)
->shouldReturn($listResponse);
}

function it_upserts_a_list_of_json_resources(
HttpClient $httpClient,
UriGeneratorInterface $uriGenerator,
ResponseInterface $response,
StreamInterface $responseBody
) {
$uri = 'http://akeneo.com/api/rest/v1/reference-entities/designer/records';

$uriGenerator
->generate('api/rest/v1/reference-entities/%s/records', ['designer'])
->willReturn($uri);

$body = <<<JSON
[{"code":"designer_1"},{"code":"designer_2"},{"code":"designer_3"}]
JSON;

$httpClient
->sendRequest('PATCH', $uri, ['Content-Type' => 'application/json'], $body)
->willReturn($response);

$response
->getBody()
->willReturn($responseBody);

$upsertResponse = <<<JSON
[
{
"code": "designer_1",
"status_code": 204
},
{
"code": "designer_2",
"status_code": 204
},
{
"code": "designer_3",
"status_code": 201
}
]
JSON;

$responseBody
->getContents()
->willReturn($upsertResponse);

$this->upsertJsonResourceList('api/rest/v1/reference-entities/%s/records', ['designer'], [
['code' => 'designer_1'],
['code' => 'designer_2'],
['code' => 'designer_3'],
])->shouldReturn([
[
'code' => 'designer_1',
'status_code' =>204
],
[
'code' => 'designer_2',
'status_code' =>204
],
[
'code' => 'designer_3',
'status_code' =>201
]
]);
}

function it_throws_an_exception_if_limit_is_defined_in_additional_parameters_to_get_resources()
{
$this
Expand All @@ -293,7 +358,7 @@ function it_throws_an_exception_if_resources_is_not_an_array_and_not_a_stream_wh
{
$this
->shouldthrow(new InvalidArgumentException('The parameter "resources" must be an array or an instance of StreamInterface.'))
->during('upsertResourceList', ['api/rest/v1/categories', [], 'foo']);
->during('upsertStreamResourceList', ['api/rest/v1/categories', [], 'foo']);
}

function it_creates_a_multipart_resource(
Expand Down
2 changes: 1 addition & 1 deletion src/Api/AssociationTypeApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ public function upsert($code, array $data = [])
*/
public function upsertList($associationTypes)
{
return $this->resourceClient->upsertResourceList(static::ASSOCIATION_TYPES_URI, [], $associationTypes);
return $this->resourceClient->upsertStreamResourceList(static::ASSOCIATION_TYPES_URI, [], $associationTypes);
}
}
2 changes: 1 addition & 1 deletion src/Api/AttributeApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ public function upsert($code, array $data = [])
*/
public function upsertList($attributes)
{
return $this->resourceClient->upsertResourceList(static::ATTRIBUTES_URI, [], $attributes);
return $this->resourceClient->upsertStreamResourceList(static::ATTRIBUTES_URI, [], $attributes);
}
}
2 changes: 1 addition & 1 deletion src/Api/AttributeGroupApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ public function upsert($code, array $data = [])
*/
public function upsertList($attributeGroups)
{
return $this->resourceClient->upsertResourceList(static::ATTRIBUTE_GROUPS_URI, [], $attributeGroups);
return $this->resourceClient->upsertStreamResourceList(static::ATTRIBUTE_GROUPS_URI, [], $attributeGroups);
}
}
2 changes: 1 addition & 1 deletion src/Api/AttributeOptionApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ public function upsert($attributeCode, $attributeOptionCode, array $data = [])
*/
public function upsertList($attributeCode, $attributeOptions)
{
return $this->resourceClient->upsertResourceList(static::ATTRIBUTE_OPTIONS_URI, [$attributeCode], $attributeOptions);
return $this->resourceClient->upsertStreamResourceList(static::ATTRIBUTE_OPTIONS_URI, [$attributeCode], $attributeOptions);
}
}
2 changes: 1 addition & 1 deletion src/Api/CategoryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ public function upsert($code, array $data = [])
*/
public function upsertList($categories)
{
return $this->resourceClient->upsertResourceList(static::CATEGORIES_URI, [], $categories);
return $this->resourceClient->upsertStreamResourceList(static::CATEGORIES_URI, [], $categories);
}
}
2 changes: 1 addition & 1 deletion src/Api/ChannelApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ public function upsert($code, array $data = [])
*/
public function upsertList($channels)
{
return $this->resourceClient->upsertResourceList(static::CHANNELS_URI, [], $channels);
return $this->resourceClient->upsertStreamResourceList(static::CHANNELS_URI, [], $channels);
}
}
2 changes: 1 addition & 1 deletion src/Api/FamilyApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ public function upsert($code, array $data = [])
*/
public function upsertList($families)
{
return $this->resourceClient->upsertResourceList(static::FAMILIES_URI, [], $families);
return $this->resourceClient->upsertStreamResourceList(static::FAMILIES_URI, [], $families);
}
}
2 changes: 1 addition & 1 deletion src/Api/FamilyVariantApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,6 @@ public function all($familyCode, $pageSize = 10, array $queryParameters = [])
*/
public function upsertList($familyCode, $familyVariants)
{
return $this->resourceClient->upsertResourceList(static::FAMILY_VARIANTS_URI, [$familyCode], $familyVariants);
return $this->resourceClient->upsertStreamResourceList(static::FAMILY_VARIANTS_URI, [$familyCode], $familyVariants);
}
}
2 changes: 1 addition & 1 deletion src/Api/ProductApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ public function delete($code)
*/
public function upsertList($products)
{
return $this->resourceClient->upsertResourceList(static::PRODUCTS_URI, [], $products);
return $this->resourceClient->upsertStreamResourceList(static::PRODUCTS_URI, [], $products);
}
}
2 changes: 1 addition & 1 deletion src/Api/ProductModelApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ public function all($pageSize = 10, array $queryParameters = [])
*/
public function upsertList($productModels)
{
return $this->resourceClient->upsertResourceList(static::PRODUCT_MODELS_URI, [], $productModels);
return $this->resourceClient->upsertStreamResourceList(static::PRODUCT_MODELS_URI, [], $productModels);
}
}
26 changes: 25 additions & 1 deletion src/Client/ResourceClient.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

declare(strict_types=1);

namespace Akeneo\Pim\ApiClient\Client;

use Akeneo\Pim\ApiClient\Exception\InvalidArgumentException;
use Akeneo\Pim\ApiClient\Exception\RuntimeException;
use Akeneo\Pim\ApiClient\Routing\UriGeneratorInterface;
use Akeneo\Pim\ApiClient\Stream\MultipartStreamBuilderFactory;
use Akeneo\Pim\ApiClient\Stream\UpsertResourceListResponseFactory;
Expand Down Expand Up @@ -150,7 +153,7 @@ public function upsertResource($uri, array $uriParameters = [], array $body = []
/**
* {@inheritdoc}
*/
public function upsertResourceList($uri, array $uriParameters = [], $resources = [])
public function upsertStreamResourceList($uri, array $uriParameters = [], $resources = [])
{
if (!is_array($resources) && !$resources instanceof StreamInterface) {
throw new InvalidArgumentException('The parameter "resources" must be an array or an instance of StreamInterface.');
Expand Down Expand Up @@ -182,6 +185,27 @@ public function upsertResourceList($uri, array $uriParameters = [], $resources =
return $this->upsertListResponseFactory->create($response->getBody());
}

/**
* {@inheritdoc}
*/
public function upsertJsonResourceList(string $uri, array $uriParameters = [], array $resources = []): array
{
$uri = $this->uriGenerator->generate($uri, $uriParameters);
$response = $this->httpClient->sendRequest(
'PATCH',
$uri,
['Content-Type' => 'application/json'],
json_encode($resources)
);

$response = json_decode($response->getBody()->getContents(), true);
if (!is_array($response)) {
throw new RuntimeException('The server response is not a valid JSON');
}

return $response;
}

/**
* {@inheritdoc}
*/
Expand Down
20 changes: 18 additions & 2 deletions src/Client/ResourceClientInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Akeneo\Pim\ApiClient\Client;

use Akeneo\Pim\ApiClient\Exception\HttpException;
Expand Down Expand Up @@ -90,7 +92,7 @@ public function createMultipartResource($uri, array $uriParameters = [], array $
public function upsertResource($uri, array $uriParameters = [], array $body = []);

/**
* Updates or creates several resources.
* Updates or creates several resources using a stream for the request and the response.
*
* @param string $uri URI of the resource
* @param array $uriParameters URI parameters of the resource
Expand All @@ -102,7 +104,21 @@ public function upsertResource($uri, array $uriParameters = [], array $body = []
*
* @return \Traversable returns an iterable object, each entry corresponding to the response of the upserted resource
*/
public function upsertResourceList($uri, array $uriParameters = [], $resources = []);
public function upsertStreamResourceList($uri, array $uriParameters = [], $resources = []);

/**
* Updates or creates several resources using a single JSON string for the request and the response.
*
* @param string $uri URI of the resource
* @param array $uriParameters URI parameters of the resource
* @param array $resources array of resources to create or update.
*
* @throws HttpException If the request failed.
* @throws InvalidArgumentException If the resources or any part thereof are invalid.
*
* @return array
*/
public function upsertJsonResourceList(string $uri, array $uriParameters = [], array $resources = []): array;

/**
* Deletes a resource.
Expand Down