ho-nl / BolCom_RetailerApi

Package that talks to the bol.com v8 API
10 stars 10 forks source link

Multiple accounts support doesn't seem to work #25

Closed FiveX-dev closed 4 years ago

FiveX-dev commented 4 years ago

When I run the following code it returns the inventory response of the first user in the clientPool for all users...

$clientPoolArr = [];
foreach ($found_users as $key => $the_user) {
  $clientPoolArr['user_' . $the_user['id']] = new \BolCom\RetailerApi\Client(new \BolCom\RetailerApi\Client\ClientConfig($the_user['public_key'], $the_user['private_key']));
}

$clientPool = new \BolCom\RetailerApi\Infrastructure\ClientPool($clientPoolArr);
$this->MessageBus = new \BolCom\RetailerApi\Infrastructure\MessageBus($clientPool);

//Request from all clients
foreach ($clientPool->names() as $name) {

    /** @var \BolCom\RetailerApi\Model\Inventory\InventoryOfferList $inventoryOfferList */
    $inventoryOfferList = $this->MessageBus->dispatch(\BolCom\RetailerApi\Model\Inventory\Query\GetInventory::with(
        1
    ), $name);

    var_dump($inventoryOfferList);
}
internetboekhandel commented 4 years ago

We get this too. If I use a clientpool then the details of the last client are used. It looks likes the logon details are cached somwhere.

I tried before without the clientpool, if I restart the script the details of the last call are used. Somehow the connection has to be terminated or cleared.

With regards, Theo

ZenoWebdev commented 4 years ago

The reason why this issue is occurring, is because multiple clients will share the same access token path. Simply giving the clients another path in the ClientConfig constructor solved the problem for us.

internetboekhandel commented 4 years ago

I understand the idea, but how to implement this?

ZenoWebdev commented 4 years ago

As stated in the README.md file in the “Multiple account support” section, you can create a client pool using multiple clients. These clients take a ClientConfig class to be instantiated. The constructor of the ClientConfig currently takes 5 parameters.

public function __construct(string $clientId, string $clientSecret, bool $testMode = false, string $accessTokenPath = '/tmp/bol_access_token.json', bool $enabled = true)

You can change the access token path by overriding the default value. (e.g. ‘/tmp/bol_access_token_1.json’ for your first account)

internetboekhandel commented 4 years ago

I changed the following in ClientConfig.php:

public function __construct( .... $this->accessTokenPath = $accessTokenPath;

Change to: $this->accessTokenPath = $accessTokenPath.$clientId.'.json';

To make it a different token json for each clientid. add the client id. Don't think this should be a security risq

ZenoWebdev commented 4 years ago

You do not want to change the source files. Changing the access token path can be done in your own code.

$clientPool = new \BolCom\RetailerApi\Infrastructure\ClientPool([
  'account1' => new \BolCom\RetailerApi\Client(new \BolCom\RetailerApi\Client\ClientConfig('clientId1', 'clientSecret1', $testMode = false, $accessTokenPath = ‘/tmp/bol_access_token_1.json’)),
]);
internetboekhandel commented 4 years ago

This doesn't work. The filename is not changed, ti defaults to the standard.