[Translation] remove credentials from PoEditorProvider

This commit is contained in:
Nicolas Grekas 2021-05-10 16:22:16 +02:00
parent 8ef49cf3b3
commit 4373d7b945
5 changed files with 60 additions and 29 deletions

View File

@ -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' => [

View File

@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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,
],
]);
}
}

View File

@ -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),
],
]);

View File

@ -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

View File

@ -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',
];
}