class Webhook in Fastly 8.3
Class Webhook.
@package Drupal\fastly\Services
Hierarchy
- class \Drupal\fastly\Services\Webhook
Expanded class hierarchy of Webhook
7 files declare their use of Webhook
- Api.php in src/
Api.php - FastlySettingsForm.php in src/
Form/ FastlySettingsForm.php - ImageOptimizerForm.php in src/
Form/ ImageOptimizerForm.php - PurgeOptionsForm.php in src/
Form/ PurgeOptionsForm.php - StaleContentOptionsForm.php in src/
Form/ StaleContentOptionsForm.php
1 string reference to 'Webhook'
1 service uses Webhook
File
- src/
Services/ Webhook.php, line 14
Namespace
Drupal\fastly\ServicesView source
class Webhook {
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $config;
/**
* The Fastly logger channel.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* The HTTP client.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* Connect timeout.
*
* @var string
*/
protected $webhookConnectTimeout;
/**
* Webhook constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config.
* @param \GuzzleHttp\ClientInterface $httpClient
* The HTTP client.
* @param \Psr\Log\LoggerInterface $logger
* The Fastly logger channel.
* @param string $webhookConnectTimeout
* The timeout for webhook connections.
*/
public function __construct(ConfigFactoryInterface $configFactory, ClientInterface $httpClient, LoggerInterface $logger, $webhookConnectTimeout) {
$this->config = $configFactory
->get('fastly.settings');
$this->httpClient = $httpClient;
$this->logger = $logger;
$this->webhookConnectTimeout = $webhookConnectTimeout;
}
/**
* Sends request to WebHookURL.
*
* @param string $message
* Webhook message, pass through t() or SafeMarkup::format first.
* @param string $type
* Webhook type.
*
* @return mixed
* FALSE if webhook notification are disabled or an unsupported type.
*/
public function sendWebHook($message, $type) {
if (!$this->config
->get('webhook_enabled') || !in_array($type, $this->config
->get('webhook_notifications'))) {
return FALSE;
}
$text = $message;
$headers = [
'Content-type: application/json',
];
$body = [
"text" => $text,
"username" => "fastly-drupal-bot",
"icon_emoji" => ":airplane:",
];
$option = [
"headers" => $headers,
"connect_timeout" => $this->webhookConnectTimeout,
"json" => $body,
];
// @TODO: handle exceptions.
$this->httpClient
->request("POST", $this->config
->get('webhook_url'), $option);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Webhook:: |
protected | property | The config factory. | |
Webhook:: |
protected | property | The HTTP client. | |
Webhook:: |
protected | property | The Fastly logger channel. | |
Webhook:: |
protected | property | Connect timeout. | |
Webhook:: |
public | function | Sends request to WebHookURL. | |
Webhook:: |
public | function | Webhook constructor. |