protected static function HttpClientTrait::httpClient in Markdown 8.2
Retrieves an HTTP client.
Parameters
string $name: A Drupal extension machine name.
string $type: The type of Drupal extension, e.g. module or theme.
Return value
\GuzzleHttp\Client An HTTP client.
File
- src/
Traits/ HttpClientTrait.php, line 32
Class
- HttpClientTrait
- Trait to assist with creating an HTTP client using module info as user-agent.
Namespace
Drupal\markdown\TraitsCode
protected static function httpClient($name = 'markdown', $type = 'module') {
if (!static::$httpClient) {
// @todo Replace with "extension.list.$type" service.
$info = system_get_info($type, $name);
$extension = isset($info['name']) ? $info['name'] : $name;
if ($info && !empty($info['version'])) {
$extension .= '/' . $info['version'];
}
$extension .= " (+https://www.drupal.org/project/{$name})";
/** @var \Drupal\Core\Http\ClientFactory $httpClientFactory */
$httpClientFactory = \Drupal::service('http_client_factory');
static::$httpClient = $httpClientFactory
->fromOptions([
'headers' => [
'User-Agent' => $extension . ' Drupal/' . \Drupal::VERSION . ' (+https://www.drupal.org/) ' . \GuzzleHttp\default_user_agent(),
],
]);
}
return static::$httpClient;
}