akeneo / api-php-client

PHP client of Akeneo PIM API

Home Page:https://packagist.org/packages/akeneo/api-php-client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

seeing the response of any upsertList() call

johamm opened this issue · comments

I'm using upsertList() in a number of areas (ProductModel, Category, AttributeOptions etc) and when I run it, I'm not getting a response. How would I know what item has been created/updated successfully and which have failed? Reading the API documentation suggests you should...

For example: https://api.akeneo.com/php-client/resources.html#upsert-a-list-of-product-models

foreach ($responseLines as $line) {
        echo $line['line'];
        echo $line['identifier'];
        echo $line['status_code'];
        if (isset($line['message'])) {
            echo $line['message'];
        }
    }

When I try this, there's nothing showing. When I print_r($responseLines) there's only the following:

Akeneo\Pim\ApiClient\Stream\UpsertResourceListResponse Object
(
    [bodyStream:protected] => GuzzleHttp\Psr7\Stream Object
        (
            [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #131
            [size:GuzzleHttp\Psr7\Stream:private] => 
            [seekable:GuzzleHttp\Psr7\Stream:private] => 1
            [readable:GuzzleHttp\Psr7\Stream:private] => 1
            [writable:GuzzleHttp\Psr7\Stream:private] => 1
            [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
            [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
                (
                )
        )

    [streamReader:protected] => Akeneo\Pim\ApiClient\Stream\LineStreamReader Object
        (
        )

    [lineNumber:protected] => 1
    [line:protected] => 
)

I agree, it does not work on my side as well.

I tried the following, which updates the product values correctly. However, I am not getting an output with the suggested foreach loop:

$responseLines = $api->getProductApi()->upsertList(
    [
        [
            'identifier' => 'TCK-171',
            "values" => [
                'Mar_StatusComment' => [
                    [
                        'data' => "Test 123",
                        'locale' => 'en_US',
                        'scope' => null,
                    ],
                ],
            ],
        ],
        [
            'identifier' => 'TCK-170',
            "values" => [
                'Mar_StatusComment' => [
                    [
                        'data' => "Test 12345",
                        'locale' => 'en_US',
                        'scope' => null,
                    ],
                ],
            ],
        ]
    ]);


foreach ($responseLines as $line) {
    echo $line['line'];
    echo $line['identifier'];
    echo $line['status_code'];
    if (isset($line['message'])) {
        echo $line['message'];
    }
}

Hi @johamm @timonburkard ,
UpsertResourceListResponse is iterable. The data is stored in the bodyStream property and the object needs to be iterated in order for each line to be decoded.

@timonburkard What's the output if you do var_dump($line) inside the foreach ? I have:

array(3) {
  'line' =>
  int(1)
  'identifier' =>
  string(8) "TCK-171"
  'status_code' =>
  int(204) // 201 if created, 204 if updated
}

And example if any line produces an error

array(5) {
  'line' =>
  int(2)
  'identifier' =>
  string(7) "noexist"
  'status_code' =>
  int(422)
  'message' =>
  string(106) "The descriptions attribute does not exist in your PIM. Check the expected format on the API documentation."
  '_links' =>
  array(1) {
    'documentation' =>
    array(1) {
      'href' =>
      string(62) "http://api.akeneo.com/api-reference.html#patch_products__code_"
    }
  }
}

So it seems ok my end with client v7 to v9, on a PIM v6.

Hi @LevFlavien,

Thanks for your reply. I tried your suggestion as follows:

$responseLines = $api->getProductApi()->upsertList(
    [
        [
            'identifier' => 'TCK-171',
            "values" => [
                'Mar_StatusComment' => [
                    [
                        'data' => "Test 2022-02-24",
                        'locale' => 'en_US',
                        'scope' => null,
                    ],
                ],
            ],
        ],
        [
            'identifier' => 'TCK-170',
            "values" => [
                'Mar_StatusComment' => [
                    [
                        'data' => "Test 2022-02-24",
                        'locale' => 'en_US',
                        'scope' => null,
                    ],
                ],
            ],
        ]
    ]);


print("*** responesLines ***\n");
var_dump($responseLines);
print("\n\n");

$i = 0;
foreach ($responseLines as $line) {
    print("*** line: " . $i . " ***\n");
    var_dump($line);
    print("\n\n");
    ++$i;
}

The products are updated correctly:
image

However, I still cannot iterate properly through the response. Output on console:

*** responesLines ***
object(Akeneo\Pim\ApiClient\Stream\UpsertResourceListResponse)#66 (4) {
  ["bodyStream":protected]=>
  object(GuzzleHttp\Psr7\Stream)#68 (7) {
    ["stream":"GuzzleHttp\Psr7\Stream":private]=>
    resource(177) of type (stream)
    ["size":"GuzzleHttp\Psr7\Stream":private]=>
    NULL
    ["seekable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["readable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["writable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["uri":"GuzzleHttp\Psr7\Stream":private]=>
    string(10) "php://temp"
    ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=>
    array(0) {
    }
  }
  ["streamReader":protected]=>
  object(Akeneo\Pim\ApiClient\Stream\LineStreamReader)#67 (0) {
  }
  ["lineNumber":protected]=>
  int(1)
  ["line":protected]=>
  NULL
}


*** line: 0 ***
NULL

I also checked composer to ensure I am on the latest version:

PS C:\Users\t.burkard\git\writer-test> composer show
akeneo/api-php-client             v9.0.0  Akeneo PIM client for the API
guzzlehttp/guzzle                 7.4.2   Guzzle is a PHP HTTP client library
guzzlehttp/promises               1.5.1   Guzzle promises library
guzzlehttp/psr7                   2.2.1   PSR-7 message implementation that also provides common utility methods
http-interop/http-factory-guzzle  1.2.0   An HTTP Factory using Guzzle PSR7
php-http/discovery                1.14.1  Finds installed HTTPlug implementations and PSR-7 message factories
php-http/guzzle7-adapter          1.0.0   Guzzle 7 HTTP Adapter
php-http/httplug                  2.3.0   HTTPlug, the HTTP client abstraction for PHP
php-http/message-factory          v1.0.2  Factory interfaces for PSR-7 HTTP Message
php-http/multipart-stream-builder 1.2.0   A builder class that help you create a multipart stream
php-http/promise                  1.1.0   Promise used for asynchronous HTTP requests
psr/http-client                   1.0.1   Common interface for HTTP clients
psr/http-factory                  1.0.1   Common interfaces for PSR-7 HTTP message factories
psr/http-message                  1.0.1   Common interface for HTTP messages
ralouphie/getallheaders           3.0.3   A polyfill for getallheaders.
symfony/deprecation-contracts     v3.0.1  A generic function and convention to trigger deprecation notices
symfony/polyfill-ctype            v1.25.0 Symfony polyfill for ctype functions
symfony/yaml                      v6.0.3  Loads and dumps YAML files

My PIM version is EE v6.0.21.

Weird, it works on my side with the same PIM version.

Could you please check whether the problem is on PIM or Client side, by requesting without using the client?

For example here's a curl, complete <PIM_URL> and <YOUR_TOKEN> variables.

curl -X PATCH <PIM_URL>api/rest/v1/products \
        -H "Content-Type: application/vnd.akeneo.collection+json" \
        -H "Authorization: Bearer <YOUR_TOKEN>" \
        -d $'{"identifier": "TCK-171", "values": {"Mar_StatusComment": [{"data": "Test 2022-02-24", "locale": "en_US", "scope": null}]}}\n{"identifier": "TCK-170", "values": {"Mar_StatusComment": [{"data": "Test 2022-02-24", "locale": "en_US", "scope": null}]}}'

expected output:

{"line":1,"identifier":"TCK-171","status_code":204}
{"line":2,"identifier":"TCK-170","status_code":204}

@LevFlavien it works with curl:

image