Skip to content

Commit dd9ac0d

Browse files
committed
Add resource client method to upsert resources using a JSON string
1 parent d711bab commit dd9ac0d

File tree

3 files changed

+108
-3
lines changed

3 files changed

+108
-3
lines changed

spec/Client/ResourceClientSpec.php

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function it_upserts_a_resource(
197197
->shouldReturn(201);
198198
}
199199

200-
function it_upserts_a_list_of_resources_from_an_array(
200+
function it_upserts_a_list_of_streamed_resources_from_an_array(
201201
$httpClient,
202202
$uriGenerator,
203203
$responseFactory,
@@ -244,7 +244,7 @@ function it_upserts_a_list_of_resources_from_an_array(
244244
->shouldReturn($listResponse);
245245
}
246246

247-
function it_upserts_a_list_of_resources_from_an_stream(
247+
function it_upserts_a_list_of_streamed_resources_from_an_stream(
248248
$httpClient,
249249
$uriGenerator,
250250
$responseFactory,
@@ -275,6 +275,71 @@ function it_upserts_a_list_of_resources_from_an_stream(
275275
->shouldReturn($listResponse);
276276
}
277277

278+
function it_upserts_a_list_of_json_resources(
279+
HttpClient $httpClient,
280+
UriGeneratorInterface $uriGenerator,
281+
ResponseInterface $response,
282+
StreamInterface $responseBody
283+
) {
284+
$uri = 'http://akeneo.com/api/rest/v1/reference-entities/designer/records';
285+
286+
$uriGenerator
287+
->generate('api/rest/v1/reference-entities/%s/records', ['designer'])
288+
->willReturn($uri);
289+
290+
$body = <<<JSON
291+
[{"code":"designer_1"},{"code":"designer_2"},{"code":"designer_3"}]
292+
JSON;
293+
294+
$httpClient
295+
->sendRequest('PATCH', $uri, ['Content-Type' => 'application/json'], $body)
296+
->willReturn($response);
297+
298+
$response
299+
->getBody()
300+
->willReturn($responseBody);
301+
302+
$upsertResponse = <<<JSON
303+
[
304+
{
305+
"code": "designer_1",
306+
"status_code": 204
307+
},
308+
{
309+
"code": "designer_2",
310+
"status_code": 204
311+
},
312+
{
313+
"code": "designer_3",
314+
"status_code": 201
315+
}
316+
]
317+
JSON;
318+
319+
$responseBody
320+
->getContents()
321+
->willReturn($upsertResponse);
322+
323+
$this->upsertJsonResourceList('api/rest/v1/reference-entities/%s/records', ['designer'], [
324+
['code' => 'designer_1'],
325+
['code' => 'designer_2'],
326+
['code' => 'designer_3'],
327+
])->shouldReturn([
328+
[
329+
'code' => 'designer_1',
330+
'status_code' =>204
331+
],
332+
[
333+
'code' => 'designer_2',
334+
'status_code' =>204
335+
],
336+
[
337+
'code' => 'designer_3',
338+
'status_code' =>201
339+
]
340+
]);
341+
}
342+
278343
function it_throws_an_exception_if_limit_is_defined_in_additional_parameters_to_get_resources()
279344
{
280345
$this

src/Client/ResourceClient.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Akeneo\Pim\ApiClient\Client;
46

57
use Akeneo\Pim\ApiClient\Exception\InvalidArgumentException;
8+
use Akeneo\Pim\ApiClient\Exception\RuntimeException;
69
use Akeneo\Pim\ApiClient\Routing\UriGeneratorInterface;
710
use Akeneo\Pim\ApiClient\Stream\MultipartStreamBuilderFactory;
811
use Akeneo\Pim\ApiClient\Stream\UpsertResourceListResponseFactory;
@@ -182,6 +185,27 @@ public function upsertStreamResourceList($uri, array $uriParameters = [], $resou
182185
return $this->upsertListResponseFactory->create($response->getBody());
183186
}
184187

188+
/**
189+
* {@inheritdoc}
190+
*/
191+
public function upsertJsonResourceList(string $uri, array $uriParameters = [], array $resources = []): array
192+
{
193+
$uri = $this->uriGenerator->generate($uri, $uriParameters);
194+
$response = $this->httpClient->sendRequest(
195+
'PATCH',
196+
$uri,
197+
['Content-Type' => 'application/json'],
198+
json_encode($resources)
199+
);
200+
201+
$response = json_decode($response->getBody()->getContents(), true);
202+
if (!is_array($response)) {
203+
throw new RuntimeException('The server response is not a valid JSON');
204+
}
205+
206+
return $response;
207+
}
208+
185209
/**
186210
* {@inheritdoc}
187211
*/

src/Client/ResourceClientInterface.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Akeneo\Pim\ApiClient\Client;
46

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

9294
/**
93-
* Updates or creates several resources using a stream.
95+
* Updates or creates several resources using a stream for the request and the response.
9496
*
9597
* @param string $uri URI of the resource
9698
* @param array $uriParameters URI parameters of the resource
@@ -104,6 +106,20 @@ public function upsertResource($uri, array $uriParameters = [], array $body = []
104106
*/
105107
public function upsertStreamResourceList($uri, array $uriParameters = [], $resources = []);
106108

109+
/**
110+
* Updates or creates several resources using a single JSON string for the request and the response.
111+
*
112+
* @param string $uri URI of the resource
113+
* @param array $uriParameters URI parameters of the resource
114+
* @param array $resources array of resources to create or update.
115+
*
116+
* @throws HttpException If the request failed.
117+
* @throws InvalidArgumentException If the resources or any part thereof are invalid.
118+
*
119+
* @return array
120+
*/
121+
public function upsertJsonResourceList(string $uri, array $uriParameters = [], array $resources = []): array;
122+
107123
/**
108124
* Deletes a resource.
109125
*

0 commit comments

Comments
 (0)