gliterd /
backblaze-b2
| 1 | <?php |
||
| 2 | |||
| 3 | namespace BackblazeB2\Http; |
||
| 4 | |||
| 5 | use BackblazeB2\ErrorHandler; |
||
| 6 | use BackblazeB2\Exceptions\B2Exception; |
||
| 7 | use GuzzleHttp\Client as GuzzleClient; |
||
| 8 | use GuzzleHttp\Exception\GuzzleException; |
||
| 9 | use Psr\Http\Message\ResponseInterface; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Client wrapper around Guzzle. |
||
| 13 | */ |
||
| 14 | class Client extends GuzzleClient |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Sends a response to the B2 API, automatically handling decoding JSON and errors. |
||
| 18 | * |
||
| 19 | * @param string $method |
||
| 20 | * @param null $uri |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 21 | * @param array $options |
||
| 22 | * @param bool $asJson |
||
| 23 | * |
||
| 24 | * @throws GuzzleException If the request fails. |
||
| 25 | * @throws B2Exception If the B2 server replies with an error. |
||
| 26 | * |
||
| 27 | * @return mixed|ResponseInterface|string |
||
| 28 | */ |
||
| 29 | 28 | public function guzzleRequest($method, $uri = null, array $options = [], $asJson = true) |
|
| 30 | { |
||
| 31 | 28 | $response = parent::request($method, $uri, $options); |
|
| 32 | |||
| 33 | 28 | if ($response->getStatusCode() !== 200) { |
|
| 34 | 7 | ErrorHandler::handleErrorResponse($response); |
|
| 35 | } |
||
| 36 | |||
| 37 | 28 | if ($asJson) { |
|
| 38 | 28 | return json_decode($response->getBody(), true); |
|
| 39 | } |
||
| 40 | |||
| 41 | 4 | return $response->getBody()->getContents(); |
|
| 42 | } |
||
| 43 | } |
||
| 44 |