From 4373d7b94538d824a5c66155fed851b36046106f Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 10 May 2021 16:22:16 +0200 Subject: [PATCH] [Translation] remove credentials from PoEditorProvider --- .../Lokalise/LokaliseProviderFactory.php | 4 +- .../Bridge/PoEditor/PoEditorHttpClient.php | 44 +++++++++++++++++++ .../Bridge/PoEditor/PoEditorProvider.php | 14 +----- .../PoEditor/PoEditorProviderFactory.php | 10 ++--- .../PoEditor/Tests/PoEditorProviderTest.php | 17 +++---- 5 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorHttpClient.php diff --git a/src/Symfony/Component/Translation/Bridge/Lokalise/LokaliseProviderFactory.php b/src/Symfony/Component/Translation/Bridge/Lokalise/LokaliseProviderFactory.php index af69c01ff6..954c66ce48 100644 --- a/src/Symfony/Component/Translation/Bridge/Lokalise/LokaliseProviderFactory.php +++ b/src/Symfony/Component/Translation/Bridge/Lokalise/LokaliseProviderFactory.php @@ -50,7 +50,9 @@ final class LokaliseProviderFactory extends AbstractProviderFactory throw new UnsupportedSchemeException($dsn, 'lokalise', $this->getSupportedSchemes()); } - $endpoint = sprintf('%s%s', 'default' === $dsn->getHost() ? self::HOST : $dsn->getHost(), $dsn->getPort() ? ':'.$dsn->getPort() : ''); + $endpoint = 'default' === $dsn->getHost() ? self::HOST : $dsn->getHost(); + $endpoint .= $dsn->getPort() ? ':'.$dsn->getPort() : ''; + $client = $this->client->withOptions([ 'base_uri' => 'https://'.$endpoint.'/projects/'.$this->getUser($dsn).'/api2/', 'headers' => [ diff --git a/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorHttpClient.php b/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorHttpClient.php new file mode 100644 index 0000000000..110020815f --- /dev/null +++ b/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorHttpClient.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Bridge\PoEditor; + +use Symfony\Component\HttpClient\DecoratorTrait; +use Symfony\Component\HttpClient\ScopingHttpClient; +use Symfony\Contracts\HttpClient\HttpClientInterface; +use Symfony\Contracts\HttpClient\ResponseInterface; + +final class PoEditorHttpClient implements HttpClientInterface +{ + use DecoratorTrait; + + public function request(string $method, string $url, array $options = []): ResponseInterface + { + if (isset($options['poeditor_credentials'])) { + if ('POST' === $method) { + $options['body'] = $options['poeditor_credentials'] + $options['body']; + } + unset($options['poeditor_credentials']); + } + + return $this->client->request($method, $url, $options); + } + + public static function create(HttpClientInterface $client, string $baseUri, string $apiToken, string $projectId): HttpClientInterface + { + return ScopingHttpClient::forBaseUri(new self($client), $baseUri, [ + 'poeditor_credentials' => [ + 'api_token' => $apiToken, + 'id' => $projectId, + ], + ]); + } +} diff --git a/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProvider.php b/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProvider.php index 4bbaba5909..5249e95307 100644 --- a/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProvider.php +++ b/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProvider.php @@ -33,18 +33,14 @@ use Symfony\Contracts\HttpClient\HttpClientInterface; */ final class PoEditorProvider implements ProviderInterface { - private $apiKey; - private $projectId; private $client; private $loader; private $logger; private $defaultLocale; private $endpoint; - public function __construct(string $apiKey, string $projectId, HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint) + public function __construct(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint) { - $this->apiKey = $apiKey; - $this->projectId = $projectId; $this->client = $client; $this->loader = $loader; $this->logger = $logger; @@ -106,8 +102,6 @@ final class PoEditorProvider implements ProviderInterface foreach ($domains as $domain) { $response = $this->client->request('POST', 'projects/export', [ 'body' => [ - 'api_token' => $this->apiKey, - 'id' => $this->projectId, 'language' => $locale, 'type' => 'xlf', 'filters' => json_encode(['translated']), @@ -176,8 +170,6 @@ final class PoEditorProvider implements ProviderInterface { $response = $this->client->request('POST', 'terms/add', [ 'body' => [ - 'api_token' => $this->apiKey, - 'id' => $this->projectId, 'data' => json_encode($terms), ], ]); @@ -194,8 +186,6 @@ final class PoEditorProvider implements ProviderInterface foreach ($translationsPerLocale as $locale => $translations) { $responses = $this->client->request('POST', 'translations/add', [ 'body' => [ - 'api_token' => $this->apiKey, - 'id' => $this->projectId, 'language' => $locale, 'data' => json_encode($translations), ], @@ -213,8 +203,6 @@ final class PoEditorProvider implements ProviderInterface { $response = $this->client->request('POST', 'terms/delete', [ 'body' => [ - 'api_token' => $this->apiKey, - 'id' => $this->projectId, 'data' => json_encode($ids), ], ]); diff --git a/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProviderFactory.php b/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProviderFactory.php index 16aae3e2fe..07ea5ddfab 100644 --- a/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProviderFactory.php +++ b/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProviderFactory.php @@ -50,12 +50,12 @@ final class PoEditorProviderFactory extends AbstractProviderFactory throw new UnsupportedSchemeException($dsn, 'poeditor', $this->getSupportedSchemes()); } - $endpoint = sprintf('%s%s', 'default' === $dsn->getHost() ? self::HOST : $dsn->getHost(), $dsn->getPort() ? ':'.$dsn->getPort() : ''); - $client = $this->client->withOptions([ - 'base_uri' => 'https://'.$endpoint.'/v2/', - ]); + $endpoint = 'default' === $dsn->getHost() ? self::HOST : $dsn->getHost(); + $endpoint .= $dsn->getPort() ? ':'.$dsn->getPort() : ''; - return new PoEditorProvider($this->getPassword($dsn), $this->getUser($dsn), $client, $this->loader, $this->logger, $this->defaultLocale, $endpoint); + $client = PoEditorHttpClient::create($this->client, 'https://'.$endpoint.'/v2/', $this->getPassword($dsn), $this->getUser($dsn)); + + return new PoEditorProvider($client, $this->loader, $this->logger, $this->defaultLocale, $endpoint); } protected function getSupportedSchemes(): array diff --git a/src/Symfony/Component/Translation/Bridge/PoEditor/Tests/PoEditorProviderTest.php b/src/Symfony/Component/Translation/Bridge/PoEditor/Tests/PoEditorProviderTest.php index 5c813e4821..708586c385 100644 --- a/src/Symfony/Component/Translation/Bridge/PoEditor/Tests/PoEditorProviderTest.php +++ b/src/Symfony/Component/Translation/Bridge/PoEditor/Tests/PoEditorProviderTest.php @@ -5,6 +5,7 @@ namespace Symfony\Component\Translation\Bridge\PoEditor\Tests; use Psr\Log\LoggerInterface; use Symfony\Component\HttpClient\MockHttpClient; use Symfony\Component\HttpClient\Response\MockResponse; +use Symfony\Component\Translation\Bridge\PoEditor\PoEditorHttpClient; use Symfony\Component\Translation\Bridge\PoEditor\PoEditorProvider; use Symfony\Component\Translation\Loader\ArrayLoader; use Symfony\Component\Translation\Loader\LoaderInterface; @@ -19,29 +20,25 @@ class PoEditorProviderTest extends ProviderTestCase { public function createProvider(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint): ProviderInterface { - return new PoEditorProvider('API_KEY', 'PROJECT_ID', $client, $loader, $logger, $defaultLocale, $endpoint); + return new PoEditorProvider(PoEditorHttpClient::create($client, 'https://poeditor', 'API_KEY', 'PROJECT_ID'), $loader, $logger, $defaultLocale, $endpoint); } public function toStringProvider(): iterable { + $client = PoEditorHttpClient::create($this->getClient(), 'https://poeditor', 'API_KEY', 'PROJECT_ID'); + yield [ - $this->createProvider($this->getClient()->withOptions([ - 'base_uri' => 'api.poeditor.com', - ]), $this->getLoader(), $this->getLogger(), $this->getDefaultLocale(), 'api.poeditor.com'), + $this->createProvider($client, $this->getLoader(), $this->getLogger(), $this->getDefaultLocale(), 'api.poeditor.com'), 'poeditor://api.poeditor.com', ]; yield [ - $this->createProvider($this->getClient()->withOptions([ - 'base_uri' => 'https://example.com', - ]), $this->getLoader(), $this->getLogger(), $this->getDefaultLocale(), 'example.com'), + $this->createProvider($client, $this->getLoader(), $this->getLogger(), $this->getDefaultLocale(), 'example.com'), 'poeditor://example.com', ]; yield [ - $this->createProvider($this->getClient()->withOptions([ - 'base_uri' => 'https://example.com:99', - ]), $this->getLoader(), $this->getLogger(), $this->getDefaultLocale(), 'example.com:99'), + $this->createProvider($client, $this->getLoader(), $this->getLogger(), $this->getDefaultLocale(), 'example.com:99'), 'poeditor://example.com:99', ]; }