8000 [Translation] remove credentials from PoEditorProvider by nicolas-grekas · Pull Request #41159 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] remove credentials from PoEditorProvider #41159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
8000 Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
],
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']),
Expand Down Expand Up @@ -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),
],
]);
Expand All @@ -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),
],
Expand All @@ -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),
],
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
];
}
Expand Down
0