Skip to content

Commit

Permalink
Merge pull request #15 from lokalise/SPHP-5_webhooks
Browse files Browse the repository at this point in the history
SPHP-5 Webhook endpoints
  • Loading branch information
beinarovic authored Jun 3, 2020
2 parents 24983a2 + 38bee2c commit 15b9547
Show file tree
Hide file tree
Showing 8 changed files with 445 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Api/Endpoints/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
158 changes: 158 additions & 0 deletions Api/Endpoints/Webhooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace Lokalise\Endpoints;

use \Lokalise\LokaliseApiResponse;
use \Lokalise\Exceptions\LokaliseApiException;
use \Lokalise\Exceptions\LokaliseResponseException;

/**
* Class Webhooks
* @package Lokalise\Endpoints
* @link https://app.lokalise.com/api2docs/curl/#resource-webhooks
*/
class Webhooks extends Endpoint implements EndpointInterface
{

/**
* @link https://app.lokalise.com/api2docs/curl/#transition-list-all-webhooks-get
*
* @param string $projectId
* @param array $queryParams
*
* @return LokaliseApiResponse
*
* @throws LokaliseApiException
* @throws LokaliseResponseException
*/
public function list($projectId, $queryParams = [])
{
return $this->request(
'GET',
"projects/$projectId/webhooks",
$queryParams
);
}

/**
* @link https://app.lokalise.com/api2docs/curl/#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://app.lokalise.com/api2docs/curl/#transition-create-a-webhook-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://app.lokalise.com/api2docs/curl/#transition-retrieve-a-webhook-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://app.lokalise.com/api2docs/curl/#transition-update-a-webhook-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://app.lokalise.com/api2docs/curl/#transition-delete-a-webhook-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://app.lokalise.com/api2docs/curl/#transition-regenerate-a-webhook-secret-patch
*
* @param string $projectId
* @param int $webhookId
*
* @return LokaliseApiResponse
*
* @throws LokaliseApiException
* @throws LokaliseResponseException
*/
public function regenerateSecret($projectId, $webhookId)
{
return $this->request(
'PATCH',
"projects/$projectId/webhooks/$webhookId/secret/regenerate"
);
}
}
13 changes: 9 additions & 4 deletions Api/LokaliseApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
use Lokalise\Endpoints\TeamUserGroups;
use \Lokalise\Endpoints\TeamUsers;
use \Lokalise\Endpoints\Translations;
use Lokalise\Endpoints\Webhooks;

class LokaliseApiClient
{
const VERSION = '3.0.0';
const VERSION = '3.1.0';

const ENDPOINT = 'https://api.lokalise.com/api2/';

Expand Down Expand Up @@ -81,6 +82,9 @@ class LokaliseApiClient
/** @var CustomTranslationStatuses */
public $customTranslationStatuses;

/** @var Webhooks */
public $webhooks;

/**
* LokaliseApiClient constructor.
*
Expand All @@ -90,9 +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->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);
Expand All @@ -103,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);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
103 changes: 103 additions & 0 deletions Docs/webhooks.md
Original file line number Diff line number Diff line change
@@ -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->regenerateSecret($projectId, $webhookId);
```

<br/><br/><br/>
<div align="right">
<b><a href="/README.md#request">⇚ Back</a></b>
</div>
<br/>
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ $client = new \Lokalise\LokaliseApiClient($apiToken);

[Orders](Docs/orders.md)

[Webhooks](Docs/webhooks.md)

## Response

```php
Expand Down
Loading

0 comments on commit 15b9547

Please sign in to comment.