manifest/web/modules/custom/etherpad_api/src/Client.php

67 lines
1.3 KiB
PHP

<?php
namespace Drupal\etherpad_api;
use Drupal\Core\Config\ImmutableConfig;
use GuzzleHttp\ClientInterface;
/**
* Service description.
*/
class Client {
/**
* The HTTP client.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* The config.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
protected $apiKey = null;
protected $baseUrl = null;
// (Minimal) Etherpad version
const API_VERSION = '1.2';
/**
* Constructs a Client object.
*
* @param \GuzzleHttp\ClientInterface $httpClient
* The HTTP client.
* @param \Drupal\Core\Config\ImmutableConfig $config
* The config.
*/
public function __construct(ClientInterface $httpClient, ImmutableConfig $config) {
$this->httpClient = $httpClient;
$this->config = $config;
$this->baseUrl = rtrim($config->get('url'), '/');
$this->apiKey = $config->get('key');
}
public function checkToken() {
return $this->request('get', '/checkToken');
}
/**
* Method description.
*/
public function request($method, $url) {
$uri = "{$this->baseUrl}/" . self::API_VERSION . $url;
if (str_contains($uri, '?')) {
$uri .= "&apikey={$this->apiKey}";
} else {
$uri .= "?apikey={$this->apiKey}";
}
return $this->httpClient->request($method, $uri);
}
}