Skip to content

Commit

Permalink
Merge pull request #47 from akeneo/fix-api-key-header
Browse files Browse the repository at this point in the history
Pass api key in headers instead of url params
  • Loading branch information
amigaire authored Mar 13, 2024
2 parents 924eb2e + 27f12e4 commit 6ab99a8
Show file tree
Hide file tree
Showing 26 changed files with 140 additions and 97 deletions.
7 changes: 5 additions & 2 deletions spec/Akeneo/Crowdin/Api/AddDirectorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ public function it_adds_a_directory(HttpClientInterface $http, ResponseInterface
$response->getContent()->willReturn($content);
$http->request(
'POST',
'project/sylius/add-directory?key=1234',
['body' => ['name' => 'directory-to-create']]
'project/sylius/add-directory',
[
'headers' => ['Authorization' => 'Bearer 1234'],
'body' => ['name' => 'directory-to-create']
]
)->willReturn($response);

$this->execute()->shouldBe($content);
Expand Down
11 changes: 8 additions & 3 deletions spec/Akeneo/Crowdin/Api/AddFileSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ public function it_should_not_add_with_no_file(HttpClientInterface $http, Respon
$content = '<xml></xml>';
$response->getContent()->willReturn($content);

$http->request('POST', 'project/sylius/add-file?key=1234')->willReturn($response);
$http->request(
'POST',
'project/sylius/add-file',
['headers' => ['Authorization' => 'Bearer 1234']]
)->willReturn($response);
$this->shouldThrow('\InvalidArgumentException')->duringExecute();
}

Expand All @@ -61,10 +65,11 @@ public function it_adds_a_file(FileReader $fileReader, HttpClientInterface $http
$fileReader->readTranslation(Argument::any())->willReturn($fakeResource);
$http->request(
'POST',
'project/sylius/add-file?key=1234',
'project/sylius/add-file',
[
'headers' => [
'Content-Type' => 'multipart/form-data'
'Content-Type' => 'multipart/form-data',
'Authorization' => 'Bearer 1234'
],
'body' => [
'files[path/to/crowdin.yml]' => $fakeResource,
Expand Down
10 changes: 7 additions & 3 deletions spec/Akeneo/Crowdin/Api/ChangeDirectorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ public function it_should_set_name(
ResponseInterface $response
) {
$this->setName('myname');
$path = 'project/sylius/change-directory?key=1234';
$data = ['body' => ['name' => 'myname']];
$path = 'project/sylius/change-directory';
$data = [
'headers' => ['Authorization' => 'Bearer 1234'],
'body' => ['name' => 'myname']
];
$http->request('POST', $path, $data)->willReturn($response);
$response->getContent(Argument::any())->willReturn('content');

Expand All @@ -50,8 +53,9 @@ public function it_should_set_data(
$this->setExportPattern('myExportPattern');
$this->setTitle('myTitle');
$this->setNewName('myNewName');
$path = 'project/sylius/change-directory?key=1234';
$path = 'project/sylius/change-directory';
$data = [
'headers' => ['Authorization' => 'Bearer 1234'],
'body' => [
'name' => 'myName',
'branch' => 'myBranch',
Expand Down
9 changes: 6 additions & 3 deletions spec/Akeneo/Crowdin/Api/DeleteDirectorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function it_should_not_delete_with_no_directory(HttpClientInterface $http
$content = '<xml></xml>';
$response->getContent()->willReturn($content);

$http->request('POST', 'project/sylius/delete-directory?key=1234')->willReturn($response);
$http->request('POST', 'project/sylius/delete-directory', ['headers' => ['Authorization' => 'Bearer 1234']])->willReturn($response);
$this->shouldThrow()->duringExecute();
}

Expand All @@ -38,8 +38,11 @@ public function it_deletes_a_directory(HttpClientInterface $http, ResponseInterf
$response->getContent()->willReturn($content);
$http->request(
'POST',
'project/sylius/delete-directory?key=1234',
['body' => ['name' => 'directory-to-delete']]
'project/sylius/delete-directory',
[
'headers' => ['Authorization' => 'Bearer 1234'],
'body' => ['name' => 'directory-to-delete']
]
)->willReturn($response);

$this->execute()->shouldBe($content);
Expand Down
9 changes: 6 additions & 3 deletions spec/Akeneo/Crowdin/Api/DeleteFileSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function it_should_not_delete_with_no_file(HttpClientInterface $http, Res
$content = '<xml></xml>';
$response->getContent()->willReturn($content);

$http->request('POST', 'project/sylius/delete-file?key=1234')->willReturn($response);
$http->request('POST', 'project/sylius/delete-file', ['headers' => ['Authorization' => 'Bearer 1234']])->willReturn($response);
$this->shouldThrow()->duringExecute();
}

Expand All @@ -41,8 +41,11 @@ public function it_deletes_a_file(HttpClientInterface $http, ResponseInterface $
$response->getContent()->willReturn($content);
$http->request(
'POST',
'project/sylius/delete-file?key=1234',
['body' => ['file' => 'path/to/my/file']]
'project/sylius/delete-file',
[
'headers' => ['Authorization' => 'Bearer 1234'],
'body' => ['file' => 'path/to/my/file']
]
)->willReturn($response);

$this->execute()->shouldBe($content);
Expand Down
4 changes: 2 additions & 2 deletions spec/Akeneo/Crowdin/Api/DownloadSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function it_downloads_all_translations(HttpClientInterface $http, Respons
{
$this->setCopyDestination('/tmp');
$this->setPackage('all.zip');
$http->request('GET', 'project/akeneo/download/all.zip?key=1234')
$http->request('GET', 'project/akeneo/download/all.zip', ['headers' => ['Authorization' => 'Bearer 1234']])
->willReturn($response);
$response->getContent()->willReturn('translations content');
$this->execute()->shouldBe('translations content');
Expand All @@ -54,7 +54,7 @@ public function it_downloads_french_translations(HttpClientInterface $http, Resp
{
$this->setCopyDestination('/tmp');
$this->setPackage('fr.zip');
$http->request('GET', 'project/akeneo/download/fr.zip?key=1234')
$http->request('GET', 'project/akeneo/download/fr.zip', ['headers' => ['Authorization' => 'Bearer 1234']])
->willReturn($response);
$response->getContent()->willReturn('translations content');
$this->execute()->shouldBe('translations content');
Expand Down
4 changes: 2 additions & 2 deletions spec/Akeneo/Crowdin/Api/ExportSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public function it_builds_last_translations(HttpClientInterface $http, ResponseI
{
$content = '<?xml version="1.0" encoding="ISO-8859-1"?><success status="built"></success>';
$response->getContent()->willReturn($content);
$http->request('GET', 'project/akeneo/export?key=1234')->willReturn($response);
$http->request('GET', 'project/akeneo/export', ['headers' => ['Authorization' => 'Bearer 1234']])->willReturn($response);
$this->execute()->shouldBe($content);
}

public function it_skips_build_if_less_than_half_an_hour(HttpClientInterface $http, ResponseInterface $response)
{
$content = '<?xml version="1.0" encoding="ISO-8859-1"?><success status="skipped"></success>';
$response->getContent()->willReturn($content);
$http->request('GET', 'project/akeneo/export?key=1234')->willReturn($response);
$http->request('GET', 'project/akeneo/export', ['headers' => ['Authorization' => 'Bearer 1234']])->willReturn($response);
$this->execute()->shouldBe($content);
}
}
2 changes: 1 addition & 1 deletion spec/Akeneo/Crowdin/Api/InfoSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function let(Client $client, HttpClientInterface $http, ResponseInterface
$client->getHttpClient()->willReturn($http);
$client->getProjectIdentifier()->willReturn('akeneo');
$client->getProjectApiKey()->willReturn('1234');
$http->request('GET', 'project/akeneo/info?key=1234')->willReturn($response);
$http->request('GET', 'project/akeneo/info', ['headers' => ['Authorization' => 'Bearer 1234']])->willReturn($response);
$response->getContent()->willReturn('<xml></xml>');
$this->beConstructedWith($client);
}
Expand Down
7 changes: 5 additions & 2 deletions spec/Akeneo/Crowdin/Api/LanguageStatusSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ public function it_gets_project_language_status(
$this->setLanguage('fr')->shouldBe($this);
$http->request(
'POST',
'project/akeneo/language-status?key=1234',
['body' => ['language' => 'fr']]
'project/akeneo/language-status',
[
'headers' => ['Authorization' => 'Bearer 1234'],
'body' => ['language' => 'fr']
]
)->willReturn($response);
$response->getContent()->willReturn('<xml></xml>');
$this->execute()->shouldBe('<xml></xml>');
Expand Down
2 changes: 1 addition & 1 deletion spec/Akeneo/Crowdin/Api/StatusSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function let(Client $client, HttpClientInterface $http, ResponseInterface
$client->getHttpClient()->willReturn($http);
$client->getProjectIdentifier()->willReturn('akeneo');
$client->getProjectApiKey()->willReturn('1234');
$http->request('GET', 'project/akeneo/status?key=1234')->willReturn($response);
$http->request('GET', 'project/akeneo/status', ['headers' => ['Authorization' => 'Bearer 1234']])->willReturn($response);
$response->getContent()->willReturn('<xml></xml>');
$this->beConstructedWith($client);
}
Expand Down
3 changes: 2 additions & 1 deletion spec/Akeneo/Crowdin/Api/SupportedLanguagesSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class SupportedLanguagesSpec extends ObjectBehavior
public function let(Client $client, HttpClientInterface $http, ResponseInterface $response)
{
$client->getHttpClient()->willReturn($http);
$http->request('GET', 'supported-languages')->willReturn($response);
$client->getProjectApiKey()->willReturn('1234');
$http->request('GET', 'supported-languages', ['headers' => ['Authorization' => 'Bearer 1234']])->willReturn($response);
$response->getContent()->willReturn('<xml></xml>');
$this->beConstructedWith($client);
}
Expand Down
11 changes: 9 additions & 2 deletions spec/Akeneo/Crowdin/Api/UpdateFileSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public function it_should_not_allow_update_with_no_file(HttpClientInterface $htt
{
$content = '<xml></xml>';
$response->getContent(true)->willReturn($content);
$http->request('POST', 'project/akeneo/update-file?key=1234')->willReturn($response);
$http->request(
'POST',
'project/akeneo/update-file',
['headers' => ['Authorization' => 'Bearer 1234']]
)->willReturn($response);
$this->shouldThrow()->duringExecute();
}

Expand All @@ -60,11 +64,13 @@ public function it_updates_some_translation_files(
$response->getContent()->willReturn($content);
$fakeResource = '[fake resource]';
$fileReader->readTranslation(Argument::any())->willReturn($fakeResource);

$http->request(
'POST',
'project/akeneo/update-file?key=1234',
'project/akeneo/update-file',
[
'headers' => [
'Authorization' => 'Bearer 1234',
'Content-Type' => 'multipart/form-data'
],
'body' => [
Expand All @@ -88,6 +94,7 @@ public function it_sends_additionnal_parameters(
Argument::any(),
[
'headers' => [
'Authorization' => 'Bearer 1234',
'Content-Type' => 'multipart/form-data'
],
'body' => [
Expand Down
15 changes: 12 additions & 3 deletions spec/Akeneo/Crowdin/Api/UploadTranslationSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public function it_should_not_allow_upload_with_no_translation(
$this->setLocale('fr');
$content = '<xml></xml>';
$response->getContent()->willReturn($content);
$http->request('POST', 'project/sylius/upload-translation?key=1234')->willReturn($response);
$http->request(
'POST',
'project/sylius/upload-translation',
['headers' => ['Authorization' => 'Bearer 1234']]
)->willReturn($response);

$this->shouldThrow('\InvalidArgumentException')->duringExecute();
}
Expand All @@ -73,7 +77,11 @@ public function it_should_not_allow_upload_with_no_locale(HttpClientInterface $h
$this->addTranslation('spec/fixtures/messages.en.yml', 'crowdin/path/file.yml');
$content = '<xml></xml>';
$response->getContent()->willReturn($content);
$http->request('POST', 'project/sylius/upload-translation?key=1234')->willReturn($response);
$http->request(
'POST',
'project/sylius/upload-translation',
['headers' => ['Authorization' => 'Bearer 1234']]
)->willReturn($response);

$this->shouldThrow()->duringExecute();
}
Expand All @@ -91,9 +99,10 @@ public function it_uploads_some_translations(
$fileReader->readTranslation(Argument::any())->willReturn($fakeResource);
$http->request(
'POST',
'project/sylius/upload-translation?key=1234',
'project/sylius/upload-translation',
[
'headers' => [
'Authorization' => 'Bearer 1234',
'Content-Type' => 'multipart/form-data'
],
'body' => [
Expand Down
14 changes: 8 additions & 6 deletions src/Akeneo/Crowdin/Api/AddDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ public function execute()
throw new InvalidArgumentException('There is no directory to create.');
}

$this->addUrlParameter('key', $this->client->getProjectApiKey());

$path = sprintf(
"project/%s/add-directory?%s",
$this->client->getProjectIdentifier(),
$this->getUrlQueryString()
"project/%s/add-directory",
$this->client->getProjectIdentifier()
);

$parameters = ['name' => $this->directory];
Expand All @@ -41,7 +38,12 @@ public function execute()
$parameters['branch'] = $this->branch;
}

$data = ['body' => $parameters];
$data = [
'headers' => [
'Authorization' => 'Bearer ' . $this->client->getProjectApiKey()
],
'body' => $parameters
];
$response = $this->client->getHttpClient()->request('POST', $path, $data);

return $response->getContent();
Expand Down
10 changes: 4 additions & 6 deletions src/Akeneo/Crowdin/Api/AddFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ public function execute(): string
throw new InvalidArgumentException('There is no files to add.');
}

$this->addUrlParameter('key', $this->client->getProjectApiKey());

$path = sprintf(
"project/%s/add-file?%s",
$this->client->getProjectIdentifier(),
$this->getUrlQueryString()
"project/%s/add-file",
$this->client->getProjectIdentifier()
);

$data = $this->parameters;
Expand All @@ -61,7 +58,8 @@ public function execute(): string

$data = [
'headers' => [
'Content-Type' => 'multipart/form-data'
'Content-Type' => 'multipart/form-data',
'Authorization' => 'Bearer 1234'
],
'body' => $data
];
Expand Down
12 changes: 6 additions & 6 deletions src/Akeneo/Crowdin/Api/ChangeDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ public function execute(): string
throw new InvalidArgumentException('Argument name is required.');
}

$this->addUrlParameter('key', $this->client->getProjectApiKey());

$path = sprintf(
"project/%s/change-directory?%s",
$this->client->getProjectIdentifier(),
$this->getUrlQueryString()
"project/%s/change-directory",
$this->client->getProjectIdentifier()
);

$data = ['name' => $this->name];
Expand All @@ -51,7 +48,10 @@ public function execute(): string
$data['branch'] = $this->branch;
}

$data = ['body' => $data];
$data = [
'headers' => ['Authorization' => 'Bearer ' . $this->client->getProjectApiKey()],
'body' => $data
];
$response = $this->client->getHttpClient()->request('POST', $path, $data);

return $response->getContent();
Expand Down
12 changes: 6 additions & 6 deletions src/Akeneo/Crowdin/Api/DeleteDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ public function execute(): string
throw new InvalidArgumentException('There is no directory to delete.');
}

$this->addUrlParameter('key', $this->client->getProjectApiKey());

$path = sprintf(
"project/%s/delete-directory?%s",
$this->client->getProjectIdentifier(),
$this->getUrlQueryString()
"project/%s/delete-directory",
$this->client->getProjectIdentifier()
);

$parameters = ['name' => $this->directory];

$data = ['body' => $parameters];
$data = [
'headers' => ['Authorization' => 'Bearer ' . $this->client->getProjectApiKey()],
'body' => $parameters
];
$response = $this->client->getHttpClient()->request('POST', $path, $data);

return $response->getContent();
Expand Down
12 changes: 6 additions & 6 deletions src/Akeneo/Crowdin/Api/DeleteFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ public function execute(): string
throw new InvalidArgumentException('There is no file to delete.');
}

$this->addUrlParameter('key', $this->client->getProjectApiKey());

$path = sprintf(
"project/%s/delete-file?%s",
$this->client->getProjectIdentifier(),
$this->getUrlQueryString()
"project/%s/delete-file",
$this->client->getProjectIdentifier()
);

$parameters = ['file' => $this->file];

$data = ['body' => $parameters];
$data = [
'headers' => ['Authorization' => 'Bearer ' . $this->client->getProjectApiKey()],
'body' => $parameters
];
$response = $this->client->getHttpClient()->request('POST', $path, $data);

return $response->getContent();
Expand Down
Loading

0 comments on commit 6ab99a8

Please sign in to comment.