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

Authentication requests are always executed

Toflar opened this issue · comments

Hey everybody,

We're using the library to request data using Guzzle.
The issue we're having is that the Akeneo SDK is always executing authentication requests even though a request is actually already in the cache.
Here's what we're using:

$stack = HandlerStack::create();
$stack->push(new CacheMiddleware(new GreedyCacheStrategy(....)));

$httpClient = Client::createWithConfig(array_merge(['handler' => $stack], $config));

$builder = new AkeneoPimClientBuilder($baseUri);
$builder->setHttpClient($httpClient);
$api = $builder->buildAuthenticatedByPassword(...);

So in other words, because Akeneo does not send any http caching headers we do use the GreedyCacheStrategy to force greedy caching. We then set this client using the setHttpClient() method. However, the ->buildAuthenticatedByPassword() wraps this client and always executes an authentication request even though the inner client actually has a middleware that knows the response already from the cache. Thus we're executing a number of nonsense authentication requests all the time.

Is there any recommendation on how to implement caching on the SDK itself?

Hello,

I also would like to have recommendation for this.

By my side, I decorated each API class that I wanted to cache (FamilyApi, AttributeApi...) and overrided the AkeneoPimClientBuilder class to use them.

It is a little dirty but this works form me and you can have differents caching strategies by API.

Hello and sorry for the late reply,

In order to better understand it:

  • Is the client doing one authentication request per request? Not expected.
  • Is the client doing one authentication request at first HTTP call and then it's authenticated? Expected.

None of the questions are related to this issue, sorry.
The problem is not that multiple authentication requests are executed. The problem is the fact that it is executed at all.

The AuthenticatedHttpClient wraps the real HttpClient to execute an authentication request during the first request (https://github.com/akeneo/api-php-client/blob/master/src/Client/AuthenticatedHttpClient.php#L52).

This means that if the instance of HttpClient is configured to use caching, the wrapping AuthenticatedHttpClient doesn't know so it always executes an authentication request, even if the real HttpClient instance afterwards doesn't execute any request because it takes it from the cache.

Hello, I better understand your problem.

Your authentication response is returned from the cache itself, which will not work.
I think that's the correct behavior is to prevent it directly from the authentication response in the API (server side).

I will talk with my teammates and keep you in touch about this problem.

Any updates on this? This is still a major performance bottleneck...

Hello,

I understand you issue, but I'm wondering how a single authentication request leads to performance bottleneck? In which context do you use the HTTP client? CLI or HTTP request? Can't you set up a token this way: https://api.akeneo.com/php-client/authentication.html#example?

How does it matter how long that request takes? It's a completely useless request if I already have the data in cache.