2023-11-12 22:55:24 +01:00
|
|
|
<?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;
|
|
|
|
|
|
|
|
protected $apiKey = null;
|
|
|
|
protected $baseUrl = null;
|
|
|
|
|
2024-01-06 18:55:09 +01:00
|
|
|
// (Minimal) Etherpad version @TODO settings / get from instance?
|
|
|
|
const API_VERSION = '1.2.1';
|
2023-11-12 22:55:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2023-12-16 02:34:11 +01:00
|
|
|
$this->baseUrl = $config->get('url') ? rtrim($config->get('url'), '/') : null;
|
2023-11-12 22:55:24 +01:00
|
|
|
$this->apiKey = $config->get('key');
|
|
|
|
}
|
|
|
|
|
2024-01-23 22:04:47 +01:00
|
|
|
/* Preveri veljavnost tokena-a s klicom na etherpadov api. */
|
2023-11-12 22:55:24 +01:00
|
|
|
public function checkToken() {
|
2023-12-16 02:34:11 +01:00
|
|
|
if ($this->baseUrl) {
|
|
|
|
return $this->request('checkToken');
|
|
|
|
}
|
|
|
|
return false;
|
2023-11-12 22:55:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-01-23 22:37:16 +01:00
|
|
|
* Poizvedi na etherpadov API. Doda baseURL in verzijo pred zeljen url, doda
|
|
|
|
* API ključ in uredi parametre. Vendo je POST!
|
2023-11-12 22:55:24 +01:00
|
|
|
*/
|
2023-12-16 01:53:05 +01:00
|
|
|
public function request($url, $opts = []) {
|
2023-12-15 17:08:37 +01:00
|
|
|
$uri = "{$this->baseUrl}/" . self::API_VERSION . '/' . explode('?', $url)[0];
|
|
|
|
if (!isset($opts['form_params'])) {
|
|
|
|
$opts['form_params'] = [];
|
2023-11-12 22:55:24 +01:00
|
|
|
}
|
2023-12-15 17:08:37 +01:00
|
|
|
$opts['form_params']['apikey'] = $this->apiKey;
|
|
|
|
$opts['verify'] = false;
|
2023-11-12 22:55:24 +01:00
|
|
|
|
2023-12-15 17:08:37 +01:00
|
|
|
return $this->httpClient->post($uri, $opts);
|
2023-11-12 22:55:24 +01:00
|
|
|
}
|
|
|
|
}
|