Skip to content

Commit

Permalink
Switch to laravel's http client instead of guzzle client in poeditor …
Browse files Browse the repository at this point in the history
…class
  • Loading branch information
gdebrauwer committed Jan 19, 2024
1 parent 526358b commit 40ff800
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 129 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
],
"require": {
"php": "^8.1|^8.2|^8.3",
"guzzlehttp/guzzle": "^6.5|^7.0",
"guzzlehttp/guzzle": "^7.0",
"illuminate/filesystem": "^9.0|^10.0",
"illuminate/http": "^9.0|^10.0",
"illuminate/support": "^9.0|^10.0",
"symfony/var-exporter": "^5.0|^6.0"
},
Expand Down
92 changes: 22 additions & 70 deletions src/Poeditor/Poeditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace NextApps\PoeditorSync\Poeditor;

use GuzzleHttp\Client;
use Illuminate\Support\Facades\Http;
use InvalidArgumentException;

class Poeditor
{
public function __construct(
public Client $client,
public string $apiKey,
public string $projectId
) {
Expand All @@ -18,77 +17,30 @@ public function __construct(

public function download(string $language) : array
{
$projectResponse = $this->client
->post(
'https://api.poeditor.com/v2/projects/export',
[
'form_params' => [
'api_token' => $this->apiKey,
'id' => $this->projectId,
'language' => $language,
'type' => 'key_value_json',
],
]
)
->getBody()
->getContents();

$exportUrl = json_decode($projectResponse, true)['result']['url'];

$exportResponse = $this->client
->get($exportUrl)
->getBody()
->getContents();

return json_decode($exportResponse, true);
$exportUrl = Http::asForm()->post('https://api.poeditor.com/v2/projects/export', [
'api_token' => $this->apiKey,
'id' => $this->projectId,
'language' => $language,
'type' => 'key_value_json',
])->json('result.url');

return Http::get($exportUrl)->json();
}

public function upload(string $language, array $translations, bool $overwrite = false) : UploadResponse
{
$filename = stream_get_meta_data($file = tmpfile())['uri'] . '.json';

file_put_contents($filename, json_encode($translations));

$response = $this->client
->post(
'https://api.poeditor.com/v2/projects/upload',
[
'multipart' => [
[
'name' => 'api_token',
'contents' => $this->apiKey,
],
[
'name' => 'id',
'contents' => $this->projectId,
],
[
'name' => 'language',
'contents' => $language,
],
[
'name' => 'updating',
'contents' => 'terms_translations',
],
[
'name' => 'file',
'contents' => fopen($filename, 'r+'),
'filename' => 'translations.json',
],
[
'name' => 'overwrite',
'contents' => (int) $overwrite,
],
[
'name' => 'fuzzy_trigger',
'contents' => 1,
],
],
]
)
->getBody()
->getContents();

return new UploadResponse(json_decode($response, true));
$response = Http::asMultipart()
->attach('file', json_encode($translations), 'translations.json')
->post('https://api.poeditor.com/v2/projects/upload', [
'api_token' => $this->apiKey,
'id' => $this->projectId,
'language' => $language,
'updating' => 'terms_translations',
'overwrite' => (int) $overwrite,
'fuzzy_trigger' => 1,
])
->json();

return new UploadResponse($response);
}
}
2 changes: 0 additions & 2 deletions src/PoeditorSyncServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace NextApps\PoeditorSync;

use GuzzleHttp\Client;
use Illuminate\Support\ServiceProvider;
use NextApps\PoeditorSync\Commands\DownloadCommand;
use NextApps\PoeditorSync\Commands\UploadCommand;
Expand Down Expand Up @@ -30,7 +29,6 @@ public function register() : void

$this->app->bind(Poeditor::class, function () {
return new Poeditor(
app(Client::class),
config('poeditor-sync.api_key'),
config('poeditor-sync.project_id')
);
Expand Down
1 change: 0 additions & 1 deletion src/Translations/TranslationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace NextApps\PoeditorSync\Translations;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Symfony\Component\VarExporter\VarExporter;
Expand Down
116 changes: 61 additions & 55 deletions tests/PoeditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,48 @@

namespace NextApps\PoeditorSync\Tests;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use InvalidArgumentException;
use NextApps\PoeditorSync\Poeditor\Poeditor;
use NextApps\PoeditorSync\Poeditor\UploadResponse;

class PoeditorTest extends TestCase
{
public MockHandler $requestMockHandler;

public array $requestHistoryContainer = [];

protected function setUp() : void
{
parent::setUp();

$stack = HandlerStack::create($this->requestMockHandler = new MockHandler());
$stack->push(Middleware::history($this->requestHistoryContainer));

app()->instance(Client::class, new Client(['handler' => $stack]));
}

/** @test */
public function it_requests_export_url_and_downloads_it_content()
{
$this->requestMockHandler->append(
new Response(200, [], json_encode([
Http::fake([
'https://api.poeditor.com/v2/projects/export' => Http::response([
'response' => [
'status' => 'success',
'code' => 200,
'message' => 'OK',
],
'result' => [
'url' => $url = $this->faker->url(),
'url' => $exportUrl = $this->faker->url(),
],
])),
new Response(200, [], json_encode([
]),
$exportUrl => Http::response([
'key' => 'value',
])),
);
]),
]);

$translations = app(Poeditor::class)->download($locale = $this->faker->locale());

$this->assertEquals(['key' => 'value'], $translations);

$getExportUrlRequest = $this->requestHistoryContainer[0]['request'];

$this->assertEquals('POST', $getExportUrlRequest->getMethod());
$this->assertEquals('api.poeditor.com', $getExportUrlRequest->getUri()->getHost());
$this->assertEquals('/v2/projects/export', $getExportUrlRequest->getUri()->getPath());

$getExportUrlRequestBody = [];
parse_str($getExportUrlRequest->getBody()->getContents(), $getExportUrlRequestBody);

$this->assertEquals(config('poeditor-sync.api_key'), $getExportUrlRequestBody['api_token']);
$this->assertEquals(config('poeditor-sync.project_id'), $getExportUrlRequestBody['id']);
$this->assertEquals($locale, $getExportUrlRequestBody['language']);
$this->assertEquals('key_value_json', $getExportUrlRequestBody['type']);

$downloadExportRequest = $this->requestHistoryContainer[1]['request'];

$this->assertEquals('GET', $downloadExportRequest->getMethod());
$this->assertEquals(parse_url($url, PHP_URL_HOST), $downloadExportRequest->getUri()->getHost());
$this->assertEquals(parse_url($url, PHP_URL_PATH), $downloadExportRequest->getUri()->getPath());
Http::assertSent(function (Request $request) use ($locale) {
return $request->url() === 'https://api.poeditor.com/v2/projects/export'
&& $request->method() === 'POST'
&& $request->isForm()
&& $request->data() === [
'api_token' => config('poeditor-sync.api_key'),
'id' => config('poeditor-sync.project_id'),
'language' => $locale,
'type' => 'key_value_json',
];
});
}

/** @test */
Expand All @@ -94,8 +69,8 @@ public function it_throws_an_error_if_project_id_is_empty()
/** @test */
public function it_uploads_translations()
{
$this->requestMockHandler->append(
new Response(200, [], json_encode([
Http::fake([
'https://api.poeditor.com/v2/projects/upload' => Http::response([
'response' => [
'status' => 'success',
'code' => 200,
Expand All @@ -113,17 +88,48 @@ public function it_uploads_translations()
'updated' => $this->faker->randomNumber(),
],
],
])),
);
]),
]);

$response = app(Poeditor::class)->upload($this->faker->locale(), ['key' => 'value']);
$response = app(Poeditor::class)->upload($locale = $this->faker->locale(), $translations = ['key' => 'value']);

$this->assertInstanceOf(UploadResponse::class, $response);

$request = $this->requestHistoryContainer[0]['request'];

$this->assertEquals('POST', $request->getMethod());
$this->assertEquals('api.poeditor.com', $request->getUri()->getHost());
$this->assertEquals('/v2/projects/upload', $request->getUri()->getPath());
Http::assertSent(function (Request $request) use ($locale, $translations) {
return $request->url() === 'https://api.poeditor.com/v2/projects/upload'
&& $request->method() === 'POST'
&& $request->isMultipart()
&& $request->data() === [
[
'name' => 'api_token',
'contents' => config('poeditor-sync.api_key'),
],
[
'name' => 'id',
'contents' => config('poeditor-sync.project_id'),
],
[
'name' => 'language',
'contents' => $locale,
],
[
'name' => 'updating',
'contents' => 'terms_translations',
],
[
'name' => 'overwrite',
'contents' => 0,
],
[
'name' => 'fuzzy_trigger',
'contents' => 1,
],
[
'name' => 'file',
'contents' => json_encode($translations),
'filename' => 'translations.json',
],
];
});
}
}

0 comments on commit 40ff800

Please sign in to comment.