LockrClient.php in Lockr 7.3
File
vendor/lockr/lockr/src/LockrClient.php
View source
<?php
namespace Lockr;
use GuzzleHttp;
use GuzzleHttp\Psr7;
use Lockr\Exception\LockrApiException;
class LockrClient {
const VERSION = '1.0.8';
private $httpClient;
private $stats;
private $hasCert = false;
public function __construct(GuzzleHttp\ClientInterface $http_client, LockrStatsInterface $stats = null) {
$this->httpClient = $http_client;
$this->stats = $stats ?: new BlackholeStats();
$this->hasCert = (bool) $http_client
->getConfig('cert');
}
public function hasCert() {
return $this->hasCert;
}
public function getHttpClient() {
return $this->httpClient;
}
public function getStats() {
return $this->stats;
}
public static function createFromSettings(SettingsInterface $settings) {
$ua = 'php/' . phpversion() . ' LockrClient/' . self::VERSION;
$base_options = [
'base_uri' => "https://{$settings->getHostname()}",
'connect_timeout' => 2.0,
'expect' => false,
'headers' => [
'accept' => [
'application/json',
],
'user-agent' => [
$ua,
],
],
'http_errors' => false,
'read_timeout' => 3.0,
'timeout' => 5.0,
];
$options = array_replace($base_options, $settings
->getOptions());
$client = new GuzzleHttp\Client($options);
return new static($client);
}
public function query(array $data) {
$resp = $this->httpClient
->request('POST', '/graphql', [
'json' => $data,
]);
$resp_data = json_decode((string) $resp
->getBody(), true);
if (!empty($resp_data['errors'])) {
throw new LockrApiException($resp_data['errors']);
}
return $resp_data['data'];
}
}