Bokbasen /
php-api-client
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bokbasen\ApiClient; |
||
| 4 | |||
| 5 | use Get; |
||
|
0 ignored issues
–
show
|
|||
| 6 | use Http\Discovery\HttpClientDiscovery; |
||
| 7 | use Http\Discovery\MessageFactoryDiscovery; |
||
| 8 | use Http\Client\HttpClient; |
||
| 9 | use Http\Message\MessageFactory; |
||
| 10 | use Psr\Http\Message\ResponseInterface; |
||
| 11 | use Bokbasen\Auth\Login; |
||
| 12 | use Psr\Http\Message\StreamInterface; |
||
| 13 | use Psr\Http\Message\UriInterface; |
||
| 14 | use Psr\Log\LoggerInterface; |
||
| 15 | use Bokbasen\ApiClient\Exceptions\BokbasenApiClientException; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
| 22 | * IN NO EVENT SHALL THE COPYRIGHT |
||
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 30 | */ |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Generic HTTP client for use against Bokbasen APIs. |
||
| 34 | * |
||
| 35 | * @license https://opensource.org/licenses/MIT |
||
| 36 | */ |
||
| 37 | class Client |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var LoggerInterface |
||
| 41 | */ |
||
| 42 | protected $logger; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var Login |
||
| 46 | */ |
||
| 47 | protected $login; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Caller |
||
| 51 | */ |
||
| 52 | protected $caller; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $baseUrl; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param Login $login |
||
| 61 | * @param string $baseUrl |
||
| 62 | */ |
||
| 63 | 4 | public function __construct(Login $login, string $baseUrl) |
|
| 64 | { |
||
| 65 | 4 | $this->login = $login; |
|
| 66 | 4 | $this->baseUrl = $baseUrl; |
|
| 67 | 4 | } |
|
| 68 | |||
| 69 | 2 | protected function getCaller(): Caller |
|
| 70 | { |
||
| 71 | 2 | if (!$this->caller) { |
|
| 72 | 2 | $this->caller = new Caller(); |
|
| 73 | } |
||
| 74 | |||
| 75 | 2 | return $this->caller; |
|
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @throws BokbasenApiClientException |
||
| 80 | */ |
||
| 81 | 2 | protected function call(string $method, string $path, $body = null, ?array $headers = [], bool $authenticate = true): ResponseInterface |
|
| 82 | { |
||
| 83 | 2 | $headers = $authenticate ? $this->addAuthenticationHeaders($headers) : $headers; |
|
| 84 | 2 | $url = $this->prependBaseUrl($path); |
|
| 85 | |||
| 86 | 2 | if ($this->logger) { |
|
| 87 | $this->logger->debug(sprintf('Executing HTTP %s request to %s with data %s.', $method, $url, $body)); |
||
| 88 | } |
||
| 89 | |||
| 90 | 2 | return $this->getCaller()->request($method, $url, $headers, $body); |
|
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Execute POST request |
||
| 95 | * |
||
| 96 | * @param string $path |
||
| 97 | * @param resource|string|StreamInterface|null $body |
||
| 98 | * @param array $headers |
||
| 99 | * @param bool $authenticate |
||
| 100 | * |
||
| 101 | * @return ResponseInterface |
||
| 102 | * |
||
| 103 | * @throws BokbasenApiClientException |
||
| 104 | */ |
||
| 105 | public function post(string $path, $body, ?array $headers = [], bool $authenticate = true): ResponseInterface |
||
| 106 | { |
||
| 107 | return $this->call( |
||
| 108 | HttpRequestOptions::HTTP_METHOD_POST, |
||
| 109 | $path, |
||
| 110 | $body, |
||
| 111 | $headers, |
||
| 112 | $authenticate |
||
| 113 | ); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Execute PUT request |
||
| 118 | * |
||
| 119 | * @param string $path |
||
| 120 | * @param resource|string|StreamInterface|null $body |
||
| 121 | * @param array $headers |
||
| 122 | * @param bool $authenticate |
||
| 123 | * |
||
| 124 | * @return ResponseInterface |
||
| 125 | * |
||
| 126 | * @throws BokbasenApiClientException |
||
| 127 | */ |
||
| 128 | public function put(string $path, $body, ?array $headers = [], bool $authenticate = true): ResponseInterface |
||
| 129 | { |
||
| 130 | return $this->call( |
||
| 131 | HttpRequestOptions::HTTP_METHOD_PUT, |
||
| 132 | $path, |
||
| 133 | $body, |
||
| 134 | $headers, |
||
| 135 | $authenticate |
||
| 136 | ); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Execute GET request |
||
| 141 | * |
||
| 142 | * @param string $path |
||
| 143 | * @param resource|string|StreamInterface|null $body |
||
| 144 | * @param array $headers |
||
| 145 | * @param bool $authenticate |
||
| 146 | * |
||
| 147 | * @return ResponseInterface |
||
| 148 | * |
||
| 149 | * @throws BokbasenApiClientException |
||
| 150 | */ |
||
| 151 | 2 | public function get(string $path, array $headers = [], $authenticate = true): ResponseInterface |
|
| 152 | { |
||
| 153 | 2 | return $this->call( |
|
| 154 | 2 | HttpRequestOptions::HTTP_METHOD_GET, |
|
| 155 | 2 | $path, |
|
| 156 | 2 | null, |
|
| 157 | 2 | $headers, |
|
| 158 | 2 | $authenticate |
|
| 159 | ); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Execute PATCH request |
||
| 164 | * |
||
| 165 | * @param string $path |
||
| 166 | * @param resource|string|StreamInterface|null $body |
||
| 167 | * @param array $headers |
||
| 168 | * @param bool $authenticate |
||
| 169 | * |
||
| 170 | * @return ResponseInterface |
||
| 171 | * |
||
| 172 | * @throws BokbasenApiClientException |
||
| 173 | */ |
||
| 174 | public function patch(string $path, $body, ?array $headers = [], bool $authenticate = true): ResponseInterface |
||
| 175 | { |
||
| 176 | return $this->call( |
||
| 177 | HttpRequestOptions::HTTP_METHOD_PATCH, |
||
| 178 | $path, |
||
| 179 | $body, |
||
| 180 | $headers, |
||
| 181 | $authenticate |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Special endpoint for posting json, sets correct content type header and encodes data as json |
||
| 187 | * |
||
| 188 | * @param string $path |
||
| 189 | * @param array|\stdClass $body |
||
| 190 | * |
||
| 191 | * @return ResponseInterface |
||
| 192 | * |
||
| 193 | * @throws BokbasenApiClientException |
||
| 194 | */ |
||
| 195 | 2 | public function postJson(string $path, $body): ResponseInterface |
|
| 196 | { |
||
| 197 | 2 | if (!is_array($body) && !$body instanceof \stdClass) { |
|
| 198 | 2 | throw new BokbasenApiClientException('Data must be array or stdClass'); |
|
| 199 | } |
||
| 200 | |||
| 201 | $body = json_encode($body); |
||
| 202 | |||
| 203 | if ($body === false) { |
||
| 204 | throw new BokbasenApiClientException('Not able to convert data to json'); |
||
| 205 | } |
||
| 206 | |||
| 207 | return $this->post( |
||
| 208 | $path, |
||
| 209 | $body, |
||
| 210 | [ |
||
| 211 | 'Content-Type' => HttpRequestOptions::CONTENT_TYPE_JSON, |
||
| 212 | ] |
||
| 213 | ); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param LoggerInterface $logger |
||
| 218 | */ |
||
| 219 | public function setLogger(LoggerInterface $logger = null): void |
||
| 220 | { |
||
| 221 | $this->logger = $logger; |
||
| 222 | } |
||
| 223 | |||
| 224 | 2 | protected function prependBaseUrl(string $path): string |
|
| 225 | { |
||
| 226 | 2 | return sprintf('%s%s', $this->baseUrl, $path); |
|
| 227 | } |
||
| 228 | |||
| 229 | 2 | protected function addAuthenticationHeaders(array $existingHeaders = []): array |
|
| 230 | { |
||
| 231 | 2 | return array_merge($this->login->getAuthHeadersAsArray(), $existingHeaders); |
|
| 232 | } |
||
| 233 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths