Skip to content
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
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,65 @@ try {
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
```

### Managing Channels

All channel methods return an array with the decoded JSON response from the API. On failure, a `RuntimeException` is thrown.

#### List channels

```php
$channels = $client->listChannels();
// $channels['data'] — array of channel objects
```

#### Create a channel

```php
$channel = $client->createChannel(
name: 'My Channel',
description: 'Optional description', // optional
);
// $channel['data'] — created channel object
```

#### Get a channel

```php
$channel = $client->getChannel('PUSHINATOR_CHANNEL_ID');
// $channel['data'] — channel object
```

#### Update a channel

```php
$channel = $client->updateChannel(
channelId: 'PUSHINATOR_CHANNEL_ID',
name: 'Updated Name',
description: 'Updated description', // optional
);
// $channel['data'] — updated channel object
```

#### Delete a channel

```php
$channel = $client->deleteChannel('PUSHINATOR_CHANNEL_ID');
// $channel['data'] — deleted channel object
```

#### Channel object structure

```json
{
"data": {
"id": "channel-id",
"name": "My Channel",
"description": "Optional description",
"acknowledgment_enabled": false,
"created_at": "2024-01-01T00:00:00.000000Z",
"updated_at": "2024-01-01T00:00:00.000000Z"
}
}
```
110 changes: 110 additions & 0 deletions src/PushinatorClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,114 @@ public function sendNotification(string $channelId, string $notification): void
);
}
}

/**
* List all channels.
*
* @return array
*/
public function listChannels(): array
{
return $this->request('GET', '/api/v2/channels');
}

/**
* Create a new channel.
*
* @param string $name
* @param string|null $description
* @param bool|null $acknowledgmentEnabled
* @return array
*/
public function createChannel(string $name, ?string $description = null, ?bool $acknowledgmentEnabled = null): array
{
$body = ['name' => $name];

if ($description !== null) {
$body['description'] = $description;
}

return $this->request('POST', '/api/v2/channels', $body);
}

/**
* Get a channel by ID.
*
* @param string $channelId
* @return array
*/
public function getChannel(string $channelId): array
{
return $this->request('GET', "/api/v2/channels/{$channelId}");
}

/**
* Update a channel by ID.
*
* @param string $channelId
* @param string $name
* @param string|null $description
* @param bool|null $acknowledgmentEnabled
* @return array
*/
public function updateChannel(string $channelId, string $name, ?string $description = null, ?bool $acknowledgmentEnabled = null): array
{
$body = ['name' => $name];

if ($description !== null) {
$body['description'] = $description;
}

return $this->request('PUT', "/api/v2/channels/{$channelId}", $body);
}

/**
* Delete a channel by ID.
*
* @param string $channelId
* @return array
*/
public function deleteChannel(string $channelId): array
{
return $this->request('DELETE', "/api/v2/channels/{$channelId}");
}

/**
* @param string $method
* @param string $path
* @param array $body
* @return array
*/
private function request(string $method, string $path, array $body = []): array
{
try {
$options = [
'headers' => [
'Authorization' => "Bearer {$this->apiToken}",
'Content-Type' => 'application/json',
'User-Agent' => self::$userAgent,
],
];

if (!empty($body)) {
$options['json'] = $body;
}

$response = $this->client->request($method, self::$baseUrl . $path, $options);

if ($response->getStatusCode() !== 200) {
throw new \RuntimeException(
"Request failed. Status: {$response->getStatusCode()}, Body: {$response->getBody()}"
);
}

return json_decode($response->getBody()->getContents(), true);
} catch (GuzzleException $e) {
throw new \RuntimeException(
"Request failed: {$e->getMessage()}",
$e->getCode(),
$e
);
}
}
}
Loading
Loading