api-php-client icon indicating copy to clipboard operation
api-php-client copied to clipboard

Authentication requests are always executed

Open Toflar opened this issue 7 years ago • 7 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?

Toflar avatar Nov 23 '18 15:11 Toflar

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.

FabienSalles avatar Nov 29 '18 09:11 FabienSalles

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.

ahocquard avatar Jan 17 '19 10:01 ahocquard

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.

Toflar avatar Jan 17 '19 10:01 Toflar

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.

ahocquard avatar Feb 27 '19 13:02 ahocquard

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

Toflar avatar Jun 23 '20 12:06 Toflar

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?

ahocquard avatar Jun 23 '20 13:06 ahocquard

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

Toflar avatar Jun 23 '20 13:06 Toflar