43 lines
1.1 KiB
Plaintext
43 lines
1.1 KiB
Plaintext
<?php
|
|
|
|
namespace Drupal\etherpad_api\Controller;
|
|
|
|
use Drupal\Core\Controller\ControllerBase;
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use GuzzleHttp\Exception\ClientException;
|
|
|
|
use Drupal\etherpad_api\Client;
|
|
|
|
/**
|
|
* Returns responses for Etherpad API routes.
|
|
*/
|
|
class EtherpadApiController extends ControllerBase {
|
|
protected $client;
|
|
|
|
public function __construct(Client $client) {
|
|
$this->client = $client;
|
|
}
|
|
|
|
public static function create(ContainerInterface $container) {
|
|
return new static($container->get('etherpad_api.client'));
|
|
}
|
|
|
|
/**
|
|
* Builds the response.
|
|
*/
|
|
public function build($components, Request $request) {
|
|
$uri = str_replace(':', '/', $components);
|
|
if ($params = $request->getQueryString()) {
|
|
$uri .= "?$params";
|
|
}
|
|
|
|
try {
|
|
return $this->client->request($request->getMethod(), $uri);
|
|
} catch (ClientException $exception) {
|
|
return new Response($exception->getMessage(), $exception->getCode());
|
|
}
|
|
}
|
|
}
|