mapado / rest-client-sdk-bundle

Symfony Bundle for https://github.com/mapado/rest-client-sdk
MIT License
5 stars 1 forks source link

RequestAwareRestClient, request stack, and command-line #9

Closed jmfeurprier closed 7 years ago

jmfeurprier commented 7 years ago

Hi,

we are using the SDK Bundle in a Symfony 3 application.

Everything works fine when used in a HTTP context (Controller => Manager => EntityRepo => SDK).

When we use the SDK in a CLI context (Command => Manager => ...), things go bad as there is no request/request stack available:

[Symfony\Component\Debug\Exception\ContextErrorException]  
  Notice: Trying to get property of non-object
Exception trace:
 () at /var/www/bo/vendor/mapado/rest-client-sdk-bundle/src/RequestAwareRestClient.php:43
 Mapado\RestClientSdkBundle\RequestAwareRestClient->mergeDefaultParameters() at /var/www/bo/vendor/mapado/rest-client-sdk/src/RestClient.php:203
 Mapado\RestClientSdk\RestClient->executeRequest() at /var/www/bo/vendor/mapado/rest-client-sdk/src/RestClient.php:104
 Mapado\RestClientSdk\RestClient->get() at /var/www/bo/vendor/mapado/rest-client-sdk/src/EntityRepository.php:73
 Mapado\RestClientSdk\EntityRepository->find() at /var/www/bo/src/AppBundle/Manager/VideoManager.php:49
 AppBundle\Manager\VideoManager->getVideo() at /var/www/bo/src/AppBundle/Command/VideoUploadCommand.php:31
 AppBundle\Command\VideoUploadCommand->execute() at /var/www/bo/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:265
 Symfony\Component\Console\Command\Command->run() at /var/www/bo/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:852
 Symfony\Component\Console\Application->doRunCommand() at /var/www/bo/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:189
 Symfony\Component\Console\Application->doRun() at /var/www/bo/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:80
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /var/www/bo/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:120
 Symfony\Component\Console\Application->run() at /var/www/bo/bin/console:28

Is there a way to configure the bundle to work with commands?

jmfeurprier commented 7 years ago

I guess adding an empty() check on the return value of $this->requestStack->getMasterRequest() in src/RequestAwareRestClient.php , method mergeDefaultParameters(), should fix the issue.

Actual code:

    /**
     * {@inheritdoc}
     */
    protected function mergeDefaultParameters(array $parameters)
    {
        $parameters = parent::mergeDefaultParameters($parameters);

        $language = $this->requestStack->getMasterRequest()->headers->get('Accept-Language');

        $parameters['headers'] = isset($parameters['headers']) ? $parameters['headers'] : [];
        $parameters['headers'] = array_merge($parameters['headers'], ['Accept-Language' => $language]);

        return $parameters;
    }
jdeniau commented 7 years ago

LGTM, can you make a PR of it ?

jmfeurprier commented 7 years ago

Sure, will do !

jmfeurprier commented 7 years ago

PR #10 is ready.

jdeniau commented 7 years ago

Fixed by #10