Skip to content

productsupcom/container-api-client

Repository files navigation

container-api-client

PHP Client interacting with cde container API.

Installation

To install this package run composer require productsupcom/container-api-client in your project root dir.

Usage

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:

  1. Productsup\CDE\ContainerApi\BaseClient\Runtime\Client\Client
  2. Monolog\Formatter\FormatterInterface

Once you instantiate it you can use it to communicate with container api.

Client

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

Formatter

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

Usage example

Push product to the Platform

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

Read product from the Platform

$containerApi = new \Productsup\CDE\ContainerApi\ContainerApi($client);

foreach ($containerApi->yieldFromInputFile() as $line) {
    // do something with new item
    $containerApi->info('A new item exported.');
}

Log message to the Platform

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');

Pushing an empty product to Platform and handling exception

<?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

Development

Update Base Client

  1. Get JSON from https://cde.productsup.com/docs/api/#/ContainerApi/show_container_api_version_docs
  2. Update /config/container-api.json file with latest open-api documentation
  3. Run
docker compose run php-cli php vendor/bin/jane-openapi generate --config-file=config/client_config.php

Update Productsup\CDE\ContainerApi\ContainerApi if there are changes in filters or endpoints.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages