From 50f7e6aa03a56be2f90f27497261f86173c30bbc Mon Sep 17 00:00:00 2001 From: Andrej Griniuk Date: Mon, 1 Jun 2020 14:06:34 +1000 Subject: [PATCH 1/6] add webhooks endpoint --- Api/Endpoints/Webhooks.php | 181 +++++++++++++++++++++++++++++++++++++ Api/LokaliseApiClient.php | 5 + 2 files changed, 186 insertions(+) create mode 100644 Api/Endpoints/Webhooks.php diff --git a/Api/Endpoints/Webhooks.php b/Api/Endpoints/Webhooks.php new file mode 100644 index 0000000..85d3e9f --- /dev/null +++ b/Api/Endpoints/Webhooks.php @@ -0,0 +1,181 @@ +request( + 'GET', + "projects/$projectId/webhooks", + $queryParams + ); + } + + /** + * @link https://lokalise.co/api2docs/php/#transition-list-all-webhooks-get + * + * @param string $projectId + * @param array $queryParams + * + * @return LokaliseApiResponse + * + * @throws LokaliseApiException + * @throws LokaliseResponseException + */ + public function fetchAll($projectId, $queryParams = []) + { + return $this->requestAll( + 'GET', + "projects/$projectId/webhooks", + $queryParams, + [], + 'webhooks' + ); + } + + /** + * @link https://lokalise.co/api2docs/php/#transition-create-webhooks-post + * + * @param string $projectId + * @param array $body + * + * @return LokaliseApiResponse + * + * @throws LokaliseApiException + * @throws LokaliseResponseException + */ + public function create($projectId, $body) + { + return $this->request( + 'POST', + "projects/$projectId/webhooks", + [], + $body + ); + } + + /** + * @link https://lokalise.co/api2docs/php/#transition-retrieve-a-webhooks-get + * + * @param string $projectId + * @param int $webhookId + * + * @return LokaliseApiResponse + * + * @throws LokaliseApiException + * @throws LokaliseResponseException + */ + public function retrieve($projectId, $webhookId) + { + return $this->request( + 'GET', + "projects/$projectId/webhooks/$webhookId" + ); + } + + /** + * @link https://lokalise.co/api2docs/php/#transition-update-a-webhooks-put + * + * @param string $projectId + * @param int $webhookId + * @param array $body + * + * @return LokaliseApiResponse + * + * @throws LokaliseApiException + * @throws LokaliseResponseException + */ + public function update($projectId, $webhookId, $body) + { + return $this->request( + 'PUT', + "projects/$projectId/webhooks/$webhookId", + [], + $body + ); + } + + /** + * @link https://lokalise.co/api2docs/php/#transition-bulk-update-put + * + * @param string $projectId + * @param array $body + * + * @return LokaliseApiResponse + * + * @throws LokaliseApiException + * @throws LokaliseResponseException + */ + public function bulkUpdate($projectId, $body) + { + return $this->request( + 'PUT', + "projects/$projectId/webhooks", + [], + $body + ); + } + + /** + * @link https://lokalise.co/api2docs/php/#transition-delete-a-webhooks-delete + * + * @param string $projectId + * @param int $webhookId + * + * @return LokaliseApiResponse + * + * @throws LokaliseApiException + * @throws LokaliseResponseException + */ + public function delete($projectId, $webhookId) + { + return $this->request( + 'DELETE', + "projects/$projectId/webhooks/$webhookId" + ); + } + + /** + * @link https://lokalise.co/api2docs/php/#transition-delete-multiple-webhooks-delete + * + * @param string $projectId + * @param array $body + * + * @return LokaliseApiResponse + * + * @throws LokaliseApiException + * @throws LokaliseResponseException + */ + public function bulkDelete($projectId, $body) + { + return $this->request( + 'DELETE', + "projects/$projectId/webhooks", + [], + $body + ); + } +} diff --git a/Api/LokaliseApiClient.php b/Api/LokaliseApiClient.php index f5cf327..a903088 100644 --- a/Api/LokaliseApiClient.php +++ b/Api/LokaliseApiClient.php @@ -20,6 +20,7 @@ use Lokalise\Endpoints\TeamUserGroups; use \Lokalise\Endpoints\TeamUsers; use \Lokalise\Endpoints\Translations; +use Lokalise\Endpoints\Webhooks; class LokaliseApiClient { @@ -81,6 +82,9 @@ class LokaliseApiClient /** @var CustomTranslationStatuses */ public $customTranslationStatuses; + /** @var Webhooks */ + public $webhooks; + /** * LokaliseApiClient constructor. * @@ -92,6 +96,7 @@ public function __construct($apiToken) $this->contributors = new Contributors(self::ENDPOINT, $apiToken); $this->files = new Files(self::ENDPOINT, $apiToken); $this->keys = new Keys(self::ENDPOINT, $apiToken); + $this->webhooks = new Webhooks(self::ENDPOINT, $apiToken); $this->languages = new Languages(self::ENDPOINT, $apiToken); $this->projects = new Projects(self::ENDPOINT, $apiToken); $this->queuedProcesses = new QueuedProcesses(self::ENDPOINT, $apiToken); From b0a90c2f0c0f98e40ca64b39547ae206b6b2b150 Mon Sep 17 00:00:00 2001 From: beinarovic Date: Wed, 3 Jun 2020 11:34:04 +0300 Subject: [PATCH 2/6] SPHP-5 Updated missing Webhook endpoints & phpdocs --- Api/Endpoints/Endpoint.php | 2 +- Api/Endpoints/Webhooks.php | 47 ++++++++++---------------------------- 2 files changed, 13 insertions(+), 36 deletions(-) diff --git a/Api/Endpoints/Endpoint.php b/Api/Endpoints/Endpoint.php index dcf8624..017a14e 100644 --- a/Api/Endpoints/Endpoint.php +++ b/Api/Endpoints/Endpoint.php @@ -44,7 +44,7 @@ public function setClient(Client $client) } /** - * @param string $requestType GET|POST|PUT|DELETE + * @param string $requestType GET|POST|PUT|PATCH|DELETE * @param string $uri * @param array $queryParams * @param array $body diff --git a/Api/Endpoints/Webhooks.php b/Api/Endpoints/Webhooks.php index 85d3e9f..62635eb 100644 --- a/Api/Endpoints/Webhooks.php +++ b/Api/Endpoints/Webhooks.php @@ -9,13 +9,13 @@ /** * Class Webhooks * @package Lokalise\Endpoints - * @link https://lokalise.co/api2docs/php/#resource-webhooks + * @link https://app.lokalise.com/api2docs/curl/#resource-webhooks */ class Webhooks extends Endpoint implements EndpointInterface { /** - * @link https://lokalise.co/api2docs/php/#transition-list-all-webhooks-get + * @link https://app.lokalise.com/api2docs/curl/#transition-list-all-webhooks-get * * @param string $projectId * @param array $queryParams @@ -35,7 +35,7 @@ public function list($projectId, $queryParams = []) } /** - * @link https://lokalise.co/api2docs/php/#transition-list-all-webhooks-get + * @link https://app.lokalise.com/api2docs/curl/#transition-list-all-webhooks-get * * @param string $projectId * @param array $queryParams @@ -57,7 +57,7 @@ public function fetchAll($projectId, $queryParams = []) } /** - * @link https://lokalise.co/api2docs/php/#transition-create-webhooks-post + * @link https://app.lokalise.com/api2docs/curl/#transition-create-a-webhook-post * * @param string $projectId * @param array $body @@ -78,7 +78,7 @@ public function create($projectId, $body) } /** - * @link https://lokalise.co/api2docs/php/#transition-retrieve-a-webhooks-get + * @link https://app.lokalise.com/api2docs/curl/#transition-retrieve-a-webhook-get * * @param string $projectId * @param int $webhookId @@ -97,7 +97,7 @@ public function retrieve($projectId, $webhookId) } /** - * @link https://lokalise.co/api2docs/php/#transition-update-a-webhooks-put + * @link https://app.lokalise.com/api2docs/curl/#transition-update-a-webhook-put * * @param string $projectId * @param int $webhookId @@ -119,28 +119,7 @@ public function update($projectId, $webhookId, $body) } /** - * @link https://lokalise.co/api2docs/php/#transition-bulk-update-put - * - * @param string $projectId - * @param array $body - * - * @return LokaliseApiResponse - * - * @throws LokaliseApiException - * @throws LokaliseResponseException - */ - public function bulkUpdate($projectId, $body) - { - return $this->request( - 'PUT', - "projects/$projectId/webhooks", - [], - $body - ); - } - - /** - * @link https://lokalise.co/api2docs/php/#transition-delete-a-webhooks-delete + * @link https://app.lokalise.com/api2docs/curl/#transition-delete-a-webhook-delete * * @param string $projectId * @param int $webhookId @@ -159,23 +138,21 @@ public function delete($projectId, $webhookId) } /** - * @link https://lokalise.co/api2docs/php/#transition-delete-multiple-webhooks-delete + * @link https://app.lokalise.com/api2docs/curl/#transition-regenerate-a-webhook-secret-patch * * @param string $projectId - * @param array $body + * @param int $webhookId * * @return LokaliseApiResponse * * @throws LokaliseApiException * @throws LokaliseResponseException */ - public function bulkDelete($projectId, $body) + public function regenerate($projectId, $webhookId) { return $this->request( - 'DELETE', - "projects/$projectId/webhooks", - [], - $body + 'PATCH', + "projects/$projectId/webhooks/$webhookId/secret/regenerate" ); } } From 0cd141f006817a7910b8a61ae03c9ac281ec7e83 Mon Sep 17 00:00:00 2001 From: beinarovic Date: Wed, 3 Jun 2020 11:34:39 +0300 Subject: [PATCH 3/6] SPHP-5 Reordered endpoint list --- Api/LokaliseApiClient.php | 8 ++++---- composer.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Api/LokaliseApiClient.php b/Api/LokaliseApiClient.php index a903088..225cb1a 100644 --- a/Api/LokaliseApiClient.php +++ b/Api/LokaliseApiClient.php @@ -94,10 +94,12 @@ public function __construct($apiToken) { $this->comments = new Comments(self::ENDPOINT, $apiToken); $this->contributors = new Contributors(self::ENDPOINT, $apiToken); + $this->customTranslationStatuses = new CustomTranslationStatuses(self::ENDPOINT, $apiToken); $this->files = new Files(self::ENDPOINT, $apiToken); $this->keys = new Keys(self::ENDPOINT, $apiToken); - $this->webhooks = new Webhooks(self::ENDPOINT, $apiToken); $this->languages = new Languages(self::ENDPOINT, $apiToken); + $this->orders = new Orders(self::ENDPOINT, $apiToken); + $this->paymentCards = new PaymentCards(self::ENDPOINT, $apiToken); $this->projects = new Projects(self::ENDPOINT, $apiToken); $this->queuedProcesses = new QueuedProcesses(self::ENDPOINT, $apiToken); $this->screenshots = new Screenshots(self::ENDPOINT, $apiToken); @@ -108,9 +110,7 @@ public function __construct($apiToken) $this->translations = new Translations(self::ENDPOINT, $apiToken); $this->teamUserGroups = new TeamUserGroups(self::ENDPOINT, $apiToken); $this->translationProviders = new TranslationProviders(self::ENDPOINT, $apiToken); - $this->paymentCards = new PaymentCards(self::ENDPOINT, $apiToken); - $this->orders = new Orders(self::ENDPOINT, $apiToken); - $this->customTranslationStatuses = new CustomTranslationStatuses(self::ENDPOINT, $apiToken); + $this->webhooks = new Webhooks(self::ENDPOINT, $apiToken); } /** diff --git a/composer.json b/composer.json index 9d27a20..d6afe21 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "lokalise/php-lokalise-api", "type": "library", "description": "Lokalise API client", - "homepage": "https://lokalise.co/api2docs/php/", + "homepage": "https://app.lokalise.com/api2docs/curl/", "keywords": ["REST", "API", "Lokalise"], "license": "MIT", "authors": [ From 851337e288755c934c80ac2cdc9b7ee5e1567158 Mon Sep 17 00:00:00 2001 From: beinarovic Date: Wed, 3 Jun 2020 11:47:14 +0300 Subject: [PATCH 4/6] SPHP-5 Covered webhooks with tests --- Tests/Endpoints/WebhooksTest.php | 168 +++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 Tests/Endpoints/WebhooksTest.php diff --git a/Tests/Endpoints/WebhooksTest.php b/Tests/Endpoints/WebhooksTest.php new file mode 100644 index 0000000..89e40e6 --- /dev/null +++ b/Tests/Endpoints/WebhooksTest.php @@ -0,0 +1,168 @@ +mockedWebhooks = $this + ->getMockBuilder(Webhooks::class) + ->setConstructorArgs([null, '{Test_Api_Token}']) + ->setMethods(['request', 'requestAll']) + ->getMock(); + + $this->mockedWebhooks->method('request')->willReturnCallback( + function ($requestType, $uri, $queryParams = [], $body = []) { + return [ + 'requestType' => $requestType, + 'uri' => $uri, + 'queryParams' => $queryParams, + 'body' => $body, + ]; + } + ); + + $this->mockedWebhooks->method('requestAll')->willReturnCallback( + function ($requestType, $uri, $queryParams = [], $body = [], $bodyResponseKey = '') { + return [ + 'requestType' => $requestType, + 'uri' => $uri, + 'queryParams' => $queryParams, + 'body' => $body, + 'bodyResponseKey' => $bodyResponseKey, + ]; + } + ); + } + + protected function tearDown() + { + $this->mockedWebhooks = null; + } + + public function testEndpointClass() + { + $this->assertInstanceOf('\Lokalise\Endpoints\Endpoint', $this->mockedWebhooks); + $this->assertInstanceOf('\Lokalise\Endpoints\EndpointInterface', $this->mockedWebhooks); + } + + public function testList() + { + $projectId = '{Project_Id}'; + $getParameters = ['params' => ['any']]; + + $this->assertEquals( + [ + 'requestType' => 'GET', + 'uri' => "projects/$projectId/webhooks", + 'queryParams' => $getParameters, + 'body' => [], + ], + $this->mockedWebhooks->list($projectId, $getParameters) + ); + } + + public function testFetchAll() + { + $projectId = '{Project_Id}'; + + $this->assertEquals( + [ + 'requestType' => 'GET', + 'uri' => "projects/$projectId/webhooks", + 'queryParams' => [], + 'body' => [], + 'bodyResponseKey' => 'webhooks', + ], + $this->mockedWebhooks->fetchAll($projectId) + ); + } + + public function testCreate() + { + $projectId = '{Project_Id}'; + $body = ['params' => ['any']]; + + $this->assertEquals( + [ + 'requestType' => 'POST', + 'uri' => "projects/$projectId/webhooks", + 'queryParams' => [], + 'body' => $body, + ], + $this->mockedWebhooks->create($projectId, $body) + ); + } + + public function testRetrieve() + { + $projectId = '{Project_Id}'; + $webhookId = '{Webhook_Id}'; + + $this->assertEquals( + [ + 'requestType' => 'GET', + 'uri' => "projects/$projectId/webhooks/$webhookId", + 'queryParams' => [], + 'body' => [], + ], + $this->mockedWebhooks->retrieve($projectId, $webhookId) + ); + } + + public function testUpdate() + { + $projectId = '{Project_Id}'; + $webhookId = '{Webhook_Id}'; + $body = ['params' => ['any']]; + + $this->assertEquals( + [ + 'requestType' => 'PUT', + 'uri' => "projects/$projectId/webhooks/$webhookId", + 'queryParams' => [], + 'body' => $body, + ], + $this->mockedWebhooks->update($projectId, $webhookId, $body) + ); + } + + public function testDelete() + { + $projectId = '{Project_Id}'; + $webhookId = '{Webhook_Id}'; + + $this->assertEquals( + [ + 'requestType' => 'DELETE', + 'uri' => "projects/$projectId/webhooks/$webhookId", + 'queryParams' => [], + 'body' => [], + ], + $this->mockedWebhooks->delete($projectId, $webhookId) + ); + } + + public function testRegenerate() + { + $projectId = '{Project_Id}'; + $webhookId = '{Webhook_Id}'; + + $this->assertEquals( + [ + 'requestType' => 'PATCH', + 'uri' => "projects/$projectId/webhooks/$webhookId/secret/regenerate", + 'queryParams' => [], + 'body' => [], + ], + $this->mockedWebhooks->regenerate($projectId, $webhookId) + ); + } +} From e515a8b4c5a48d11e30d12fb00767fa7ca4f6e9f Mon Sep 17 00:00:00 2001 From: beinarovic Date: Wed, 3 Jun 2020 12:21:17 +0300 Subject: [PATCH 5/6] SPHP-5 Updated docs & version --- Api/LokaliseApiClient.php | 2 +- CHANGELOG.md | 3 ++ Docs/webhooks.md | 103 ++++++++++++++++++++++++++++++++++++++ README.md | 2 + 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 Docs/webhooks.md diff --git a/Api/LokaliseApiClient.php b/Api/LokaliseApiClient.php index 225cb1a..1fa1ca0 100644 --- a/Api/LokaliseApiClient.php +++ b/Api/LokaliseApiClient.php @@ -24,7 +24,7 @@ class LokaliseApiClient { - const VERSION = '3.0.0'; + const VERSION = '3.1.0'; const ENDPOINT = 'https://api.lokalise.com/api2/'; diff --git a/CHANGELOG.md b/CHANGELOG.md index fa5e504..f6a300c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 3.1.0 (Jun 3, 2020) +- Added [webhook](Docs/webhooks.md) API. + ## 3.0.0 (May 25, 2020) - Added [queued process](Docs/queuedProcesses.md) API. - File upload now uses queued processes. diff --git a/Docs/webhooks.md b/Docs/webhooks.md new file mode 100644 index 0000000..6e0de90 --- /dev/null +++ b/Docs/webhooks.md @@ -0,0 +1,103 @@ +# Webhooks API + +### List all webhooks +https://app.lokalise.com/api2docs/curl/#transition-list-all-webhooks-get + +```php +$response = $client->webhooks->list( + $projectId, + [ + 'limit' => 20, + 'page' => 1, + ] +); +``` + +```php +$response = $client->webhooks->fetchAll( + $projectId +); +``` + +### Create a webhook +https://app.lokalise.com/api2docs/curl/#transition-create-a-webhook-post + +```php +$response = $client->webhooks->create( + $projectId, + [ + 'url' => 'https://my.domain.com/webhook', + 'events' => [ + 'project.translation.proofread', + 'project.translation.updated', + ], + 'event_lang_map' => [ + [ + 'event' => 'project.translation.updated', + 'lang_iso_codes' => [ + 'en_GB', + ], + ], + ], + ] +); +``` + +### Retrieve a webhook +https://app.lokalise.com/api2docs/curl/#transition-retrieve-a-webhook-get + +```php +$response = $client->webhooks->retrieve($projectId, $webhookId); +``` + +### Update a webhook +https://app.lokalise.com/api2docs/curl/#transition-update-a-webhook-put + +```php +$response = $client->webhooks->update( + $projectId, + $webhookId, + [ + 'events' => [ + 'project.translation.proofread', + 'project.translation.updated', + 'project.imported', + ], + 'event_lang_map' => [ + [ + 'event' => 'project.translation.proofread', + 'lang_iso_codes' => [ + 'en_GB', + ], + ], + [ + 'event' => 'project.translation.updated', + 'lang_iso_codes' => [ + 'en_GB' + ], + ], + ], + ] +); +``` + + +### Delete a webhook +https://app.lokalise.com/api2docs/curl/#transition-delete-a-webhook-delete + +```php +$response = $client->webhooks->delete($projectId, $webhookId); +``` + +### Regenerate a webhook secret +https://app.lokalise.com/api2docs/curl/#transition-regenerate-a-webhook-secret-patch + +```php +$response = $client->webhooks->regenerate($projectId, $webhookId); +``` + +


+ +
diff --git a/README.md b/README.md index caf9614..f05e052 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,8 @@ $client = new \Lokalise\LokaliseApiClient($apiToken); [Orders](Docs/orders.md) +[Webhooks](Docs/webhooks.md) + ## Response ```php From 38bee2c51d20954a32384cd7b71a4761b7622040 Mon Sep 17 00:00:00 2001 From: beinarovic Date: Wed, 3 Jun 2020 12:34:03 +0300 Subject: [PATCH 6/6] SPHP-5 Renames webhook regenerate method to regenerateSecret --- Api/Endpoints/Webhooks.php | 2 +- Docs/webhooks.md | 2 +- Tests/Endpoints/WebhooksTest.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Api/Endpoints/Webhooks.php b/Api/Endpoints/Webhooks.php index 62635eb..b973e74 100644 --- a/Api/Endpoints/Webhooks.php +++ b/Api/Endpoints/Webhooks.php @@ -148,7 +148,7 @@ public function delete($projectId, $webhookId) * @throws LokaliseApiException * @throws LokaliseResponseException */ - public function regenerate($projectId, $webhookId) + public function regenerateSecret($projectId, $webhookId) { return $this->request( 'PATCH', diff --git a/Docs/webhooks.md b/Docs/webhooks.md index 6e0de90..f3815dd 100644 --- a/Docs/webhooks.md +++ b/Docs/webhooks.md @@ -93,7 +93,7 @@ $response = $client->webhooks->delete($projectId, $webhookId); https://app.lokalise.com/api2docs/curl/#transition-regenerate-a-webhook-secret-patch ```php -$response = $client->webhooks->regenerate($projectId, $webhookId); +$response = $client->webhooks->regenerateSecret($projectId, $webhookId); ```


diff --git a/Tests/Endpoints/WebhooksTest.php b/Tests/Endpoints/WebhooksTest.php index 89e40e6..68abe57 100644 --- a/Tests/Endpoints/WebhooksTest.php +++ b/Tests/Endpoints/WebhooksTest.php @@ -150,7 +150,7 @@ public function testDelete() ); } - public function testRegenerate() + public function testRegenerateSecret() { $projectId = '{Project_Id}'; $webhookId = '{Webhook_Id}'; @@ -162,7 +162,7 @@ public function testRegenerate() 'queryParams' => [], 'body' => [], ], - $this->mockedWebhooks->regenerate($projectId, $webhookId) + $this->mockedWebhooks->regenerateSecret($projectId, $webhookId) ); } }