PHP Client interacting with cde container API.
To install this package run composer require productsupcom/container-api-client in your project root dir.
Code is mostly generated from openapi specs for container api, but Productsup\CDE\ContainerApi\ContainerApi class is
an abstraction that is holding all the operations you might need with container api. The class is dependent on 2
services:
Productsup\CDE\ContainerApi\BaseClient\Runtime\Client\ClientMonolog\Formatter\FormatterInterface
Once you instantiate it you can use it to communicate with container api.
Productsup\CDE\ContainerApi\BaseClient\Runtime\Client\Client
which is an autogenerated client for container api. You can instantiate it like this:
$httpClient = new \GuzzleHttp\Client([
'base_uri' => 'http://cde-container-api', //client container runs in a container alongside your connector container
]);
$client = Client::create($httpClient);NOTE: We use guzzle as http client, but you can use any of the clients
implementing Psr\Http\Client\ClientInterface
This argument is nullable, so if you don't want to use custom log formatting you can leave it null and it will
use Monolog\Formatter\NormalizerFormatter. If you wish to format the logs sent to container api you can inject any
implementation of the Monolog\Formatter\FormatterInterface
<?php
declare(strict_types=1);
use Productsup\CDE\Connector\Application\Feed\OutputFeedForImport;
class ImportService
{
public function __construct(
private OutputFeedForImport $feedForImport,
) {
}
public function importToProductsupPlatform(): void
{
$items = [
['id' => 1, 'name' => 'Item 1', 'size' => 'm'],
['id' => 2, 'name' => 'Item 2', 'description' => 'New fancy item'],
];
foreach ($items as $item) {
$this->feedForImport->appendBufferedToOutputFeed($item);
}
$this->feedForImport->end();
}
}$containerApi = new \Productsup\CDE\ContainerApi\ContainerApi($client);
foreach ($containerApi->yieldFromInputFile() as $line) {
// do something with new item
$containerApi->info('A new item exported.');
}You can log message on different levels. Please check SpecificLogTrait.php
DEBUG level is for internal purpose and is not visible on Platform.
$containerApi = new \Productsup\CDE\ContainerApi\ContainerApi($client);
$containerApi->info('Info message');
$containerApi->error('Error message');<?php
declare(strict_types=1);
use Productsup\CDE\ContainerApi\ContainerApi;
use Productsup\CDE\Connector\Application\Feed\OutputFeedForImport;
class ImportService
{
public function __construct(
private ContainerApi $containerApi,
private OutputFeedForImport $feedForImport,
) {
}
public function importToProductsupPlatform(): void
{
$items = [
['id' => 1, 'name' => 'Item 1', 'size' => 'm'],
[],
];
foreach ($items as $item) {
try {
$this->feedForImport->appendBufferedToOutputFeed($item);
} catch (Throwable $throwable) {
$this->containerApi->error('Item is empty.');
continue;
}
}
$this->feedForImport->end();
}
}ApiFailureException - Container API responded with HTTP status code = 500. To get real exception, you have to use $exception->getPrevious().
FileNotFoundException - the file from which we are reading the data does not exist
- Get JSON from https://cde.productsup.com/docs/api/#/ContainerApi/show_container_api_version_docs
- Update
/config/container-api.jsonfile with latest open-api documentation - Run
docker compose run php-cli php vendor/bin/jane-openapi generate --config-file=config/client_config.phpUpdate Productsup\CDE\ContainerApi\ContainerApi if there are changes in filters or endpoints.