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 af69c01ff6891..954c66ce48507 100644 --- a/src/Symfony/Component/Translation/Bridge/Lokalise/LokaliseProviderFactory.php +++ b/src/Symfony/Component/Translation/Bridge/Lokalise/LokaliseProviderFactory.php @@ -50,7 +50,9 @@ public function create(Dsn $dsn): ProviderInterface 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 0000000000000..110020815f297 --- /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 4bbaba5909426..5249e95307d8e 100644 --- a/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProvider.php +++ b/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProvider.php @@ -33,18 +33,14 @@ */ 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 @@ public function read(array $domains, array $locales): TranslatorBag 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 @@ private function addTerms(array $terms): void { $response = $this->client->request('POST', 'terms/add', [ 'body' => [ - 'api_token' => $this->apiKey, - 'id' => $this->projectId, 'data' => json_encode($terms), ], ]); @@ -194,8 +186,6 @@ private function addTranslations(array $translationsPerLocale): void 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 @@ private function deleteTerms(array $ids): void { $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 16aae3e2fe175..07ea5ddfab3ed 100644 --- a/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProviderFactory.php +++ b/src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProviderFactory.php @@ -50,12 +50,12 @@ public function create(Dsn $dsn): ProviderInterface 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 5c813e482181d..708586c385a85 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 @@ 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', ]; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy