154 lines
4.8 KiB
PHP
154 lines
4.8 KiB
PHP
<?php
|
|
|
|
/**
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; under version 2
|
|
* of the License (non-upgradable).
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*
|
|
* Copyright (c) 2017 (original work) Open Assessment Technologies SA;
|
|
*
|
|
*/
|
|
|
|
namespace oat\tao\model\api;
|
|
|
|
use GuzzleHttp\Promise\PromiseInterface;
|
|
use oat\oatbox\Configurable;
|
|
use Psr\Http\Message\RequestInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\UriInterface;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\ClientInterface;
|
|
|
|
/**
|
|
* Class ApiClientConnector
|
|
*
|
|
* Class to handle a http connection.
|
|
*
|
|
*/
|
|
class ApiClientConnector extends Configurable implements ClientInterface
|
|
{
|
|
const OPTION_BASE_URI = 'base_uri';
|
|
|
|
/**
|
|
* Send an HTTP request.
|
|
*
|
|
* @param RequestInterface $request Request to send
|
|
* @param array $options Request options to apply to the given
|
|
* request and to the transfer.
|
|
*
|
|
* @return ResponseInterface
|
|
* @throws GuzzleException
|
|
*/
|
|
public function send(RequestInterface $request, array $options = [])
|
|
{
|
|
return $this->getClient()->send($request, $options);
|
|
}
|
|
|
|
/**
|
|
* Create and send an HTTP request.
|
|
*
|
|
* Use an absolute path to override the base path of the client, or a
|
|
* relative path to append to the base path of the client. The URL can
|
|
* contain the query string as well.
|
|
*
|
|
* @param string $method HTTP method.
|
|
* @param string|UriInterface $uri URI object or string.
|
|
* @param array $options Request options to apply.
|
|
*
|
|
* @return ResponseInterface
|
|
* @throws GuzzleException
|
|
*/
|
|
public function request($method, $uri, array $options = [])
|
|
{
|
|
return $this->getClient()->request($method, $uri, $options);
|
|
}
|
|
|
|
/**
|
|
* Asynchronously send an HTTP request.
|
|
*
|
|
* @param RequestInterface $request Request to send
|
|
* @param array $options Request options to apply to the given
|
|
* request and to the transfer.
|
|
*
|
|
* @return PromiseInterface
|
|
*/
|
|
public function sendAsync(RequestInterface $request, array $options = [])
|
|
{
|
|
return $this->getClient()->sendAsync($request, $options);
|
|
}
|
|
|
|
/**
|
|
* Create and send an asynchronous HTTP request.
|
|
*
|
|
* Use an absolute path to override the base path of the client, or a
|
|
* relative path to append to the base path of the client. The URL can
|
|
* contain the query string as well. Use an array to provide a URL
|
|
* template and additional variables to use in the URL template expansion.
|
|
*
|
|
* @param string $method HTTP method
|
|
* @param string|UriInterface $uri URI object or string.
|
|
* @param array $options Request options to apply.
|
|
*
|
|
* @return PromiseInterface
|
|
*/
|
|
public function requestAsync($method, $uri, array $options = [])
|
|
{
|
|
return $this->getClient()->requestAsync($method, $uri, $options);
|
|
}
|
|
|
|
/**
|
|
* Get a client configuration option.
|
|
*
|
|
* These options include default request options of the client, a "handler"
|
|
* (if utilized by the concrete client), and a "base_uri" if utilized by
|
|
* the concrete client.
|
|
*
|
|
* @param string|null $option The config option to retrieve.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function getConfig($option = null)
|
|
{
|
|
return $this->getClient()->getConfig($option);
|
|
}
|
|
|
|
/**
|
|
* Return the Http Client e.q. Guzzle client
|
|
*
|
|
* @return Client
|
|
*/
|
|
protected function getClient()
|
|
{
|
|
return new Client($this->getClientOptions());
|
|
}
|
|
|
|
/**
|
|
* Get the options as client options
|
|
*
|
|
* @return array
|
|
*/
|
|
protected function getClientOptions()
|
|
{
|
|
$options = $this->getOptions();
|
|
if (!isset($options['headers']) || !isset($options['headers']['Content-Type'])) {
|
|
$options['headers']['Content-Type'] = ['application/json'];
|
|
} else {
|
|
if (!in_array('application/json', $options['headers']['Content-Type'])) {
|
|
$options['headers']['Content-Type'][] = ['application/json'];
|
|
}
|
|
}
|
|
return $options;
|
|
}
|
|
}
|