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

66 lines
1.4 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($url, $opts = []) {
$uri = "{$this->baseUrl}/" . self::API_VERSION . '/' . explode('?', $url)[0];
if (!isset($opts['form_params'])) {
$opts['form_params'] = [];
}
$opts['form_params']['apikey'] = $this->apiKey;
$opts['verify'] = false;
return $this->httpClient->post($uri, $opts);
}
}