Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| ApiClientConnector | |
0.00% |
0 / 16 |
|
0.00% |
0 / 7 |
110 | |
0.00% |
0 / 1 |
| send | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| request | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| sendAsync | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| requestAsync | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getConfig | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getClient | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getClientOptions | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\api; |
| 23 | |
| 24 | use GuzzleHttp\Promise\PromiseInterface; |
| 25 | use oat\oatbox\Configurable; |
| 26 | use Psr\Http\Message\RequestInterface; |
| 27 | use Psr\Http\Message\ResponseInterface; |
| 28 | use Psr\Http\Message\UriInterface; |
| 29 | use GuzzleHttp\Exception\GuzzleException; |
| 30 | use GuzzleHttp\Client; |
| 31 | use GuzzleHttp\ClientInterface; |
| 32 | |
| 33 | /** |
| 34 | * Class ApiClientConnector |
| 35 | * |
| 36 | * Class to handle a http connection. |
| 37 | * |
| 38 | */ |
| 39 | class ApiClientConnector extends Configurable implements ClientInterface |
| 40 | { |
| 41 | public const OPTION_BASE_URI = 'base_uri'; |
| 42 | |
| 43 | /** |
| 44 | * Send an HTTP request. |
| 45 | * |
| 46 | * @param RequestInterface $request Request to send |
| 47 | * @param array $options Request options to apply to the given |
| 48 | * request and to the transfer. |
| 49 | * |
| 50 | * @return ResponseInterface |
| 51 | * @throws GuzzleException |
| 52 | */ |
| 53 | public function send(RequestInterface $request, array $options = []): ResponseInterface |
| 54 | { |
| 55 | return $this->getClient()->send($request, $options); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Create and send an HTTP request. |
| 60 | * |
| 61 | * Use an absolute path to override the base path of the client, or a |
| 62 | * relative path to append to the base path of the client. The URL can |
| 63 | * contain the query string as well. |
| 64 | * |
| 65 | * @param string $method HTTP method. |
| 66 | * @param string|UriInterface $uri URI object or string. |
| 67 | * @param array $options Request options to apply. |
| 68 | * |
| 69 | * @return ResponseInterface |
| 70 | * @throws GuzzleException |
| 71 | */ |
| 72 | public function request($method, $uri, array $options = []): ResponseInterface |
| 73 | { |
| 74 | return $this->getClient()->request($method, $uri, $options); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Asynchronously send an HTTP request. |
| 79 | * |
| 80 | * @param RequestInterface $request Request to send |
| 81 | * @param array $options Request options to apply to the given |
| 82 | * request and to the transfer. |
| 83 | * |
| 84 | * @return PromiseInterface |
| 85 | */ |
| 86 | public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface |
| 87 | { |
| 88 | return $this->getClient()->sendAsync($request, $options); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Create and send an asynchronous HTTP request. |
| 93 | * |
| 94 | * Use an absolute path to override the base path of the client, or a |
| 95 | * relative path to append to the base path of the client. The URL can |
| 96 | * contain the query string as well. Use an array to provide a URL |
| 97 | * template and additional variables to use in the URL template expansion. |
| 98 | * |
| 99 | * @param string $method HTTP method |
| 100 | * @param string|UriInterface $uri URI object or string. |
| 101 | * @param array $options Request options to apply. |
| 102 | * |
| 103 | * @return PromiseInterface |
| 104 | */ |
| 105 | public function requestAsync($method, $uri, array $options = []): PromiseInterface |
| 106 | { |
| 107 | return $this->getClient()->requestAsync($method, $uri, $options); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get a client configuration option. |
| 112 | * |
| 113 | * These options include default request options of the client, a "handler" |
| 114 | * (if utilized by the concrete client), and a "base_uri" if utilized by |
| 115 | * the concrete client. |
| 116 | * |
| 117 | * @param string|null $option The config option to retrieve. |
| 118 | * |
| 119 | * @return mixed |
| 120 | */ |
| 121 | public function getConfig($option = null) |
| 122 | { |
| 123 | return $this->getClient()->getConfig($option); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Return the Http Client e.q. Guzzle client |
| 128 | * |
| 129 | * @return Client |
| 130 | */ |
| 131 | protected function getClient() |
| 132 | { |
| 133 | return new Client($this->getClientOptions()); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get the options as client options |
| 138 | * |
| 139 | * @return array |
| 140 | */ |
| 141 | protected function getClientOptions() |
| 142 | { |
| 143 | $options = $this->getOptions(); |
| 144 | if (!isset($options['headers']) || !isset($options['headers']['Content-Type'])) { |
| 145 | $options['headers']['Content-Type'] = ['application/json']; |
| 146 | } else { |
| 147 | if (!in_array('application/json', $options['headers']['Content-Type'])) { |
| 148 | $options['headers']['Content-Type'][] = ['application/json']; |
| 149 | } |
| 150 | } |
| 151 | return $options; |
| 152 | } |
| 153 | } |