Class Client
The client for communicating with Composable Commerce
- Commercetools\Core\AbstractHttpClient
- Commercetools\Core\Client implements Psr\Log\LoggerAwareInterface
Description:
Instantiation
$config = Config::fromArray( ['client_id' => '<client_id>', 'client_secret' => '<client_secret>', 'project' => '<project>'] ); $client = Client::ofConfig($config);
Execution
Synchronous
There are two main approaches for retrieving result objects
Client centric:
$response = $client->execute(ProductProjectionSearchRequest::of()); $products = $response->toObject();
Request centric:
$request = ProductProjectionSearchRequest::of(); $response = $request->executeWithClient($client); $products = $request->mapFromResponse($response);
By using the request centric approach the IDE is capable of resolving the correct classes and give a maximum of support with available methods.
Asynchronous
The asynchronous execution will return a promise to fulfill the request.
$response = $client->executeAsync(ProductProjectionSearchRequest::of());
Batch
By filling the batch queue and starting the execution all requests will be executed in parallel.
$client->addBatchRequest(ProductProjectionSearchRequest::of()) ->addBatchRequest(CartByIdGetRequest::ofId($cartId)); $responses = $client->executeBatch();
Instantiation options
Using a logger
The client uses the PSR-3 logger interface for logging requests and deprecation notices. To enable logging provide a PSR-3 compliant logger (e.g. Monolog).
$logger = new \Monolog\Logger('name'); $logger->pushHandler(new StreamHandler('./requests.log')); $client = Client::ofConfigAndLogger($config, $logger);
Using a cache adapter
The client will automatically request an OAuth token and store the token in the provided cache.
It's also possible to use a different cache adapter. The SDK provides a Doctrine, a Redis and an APCu cache adapter. By default the SDK tries to instantiate the APCu or a PSR-6 filesystem cache adapter if there is no cache given. E.g. Redis:
$redis = new \Redis(); $redis->connect('localhost'); $client = Client::ofConfigAndCache($config, $redis);
Using cache and logger
$client = Client::ofConfigCacheAndLogger($config, $cache, $logger);
Using a custom cache adapter
class <CacheClass>Adapter implements \Commercetools\Core\Cache\CacheAdapterInterface { protected $cache; public function __construct(<CacheClass> $cache) { $this->cache = $cache; } } $client->getAdapterFactory()->registerCallback(function ($cache) { if ($cache instanceof <CacheClass>) { return new <CacheClass>Adapter($cache); } return null; });
Located at Core/Client.php
Methods summary
public
|
#
__construct( array|
|
public
Commercetools\Core\Client\OAuth\Manager
|
|
public
|
|
public
|
|
public
|
#
execute(
Executes an API request synchronously |
public
|
#
executeAsync(
Executes an API request asynchronously |
public
|
|
public
|
#
addBatchRequest(
Adds a request to the batch execution queue |
public static
|
|
public static
|
#
ofConfigAndCache(
Instantiates a client with the given config and cache adapter |
public static
|
#
ofConfigAndLogger(
Instantiates a client with the given config and a PSR-3 compliant logger |
public static
|
#
ofConfigCacheAndLogger(
Instantiates a client with the given config, a cache adapter and a PSR-3 compliant logger |
Methods inherited from Commercetools\Core\AbstractHttpClient
Constants summary
string |
DEPRECATION_HEADER
|
#
'X-DEPRECATION-NOTICE'
|