-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathDropboxGuzzleHttpClient.php
111 lines (96 loc) · 3.19 KB
/
DropboxGuzzleHttpClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace Kunnu\Dropbox\Http\Clients;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use Kunnu\Dropbox\Http\DropboxRawResponse;
use GuzzleHttp\Exception\BadResponseException;
use Kunnu\Dropbox\Exceptions\DropboxClientException;
/**
* DropboxGuzzleHttpClient.
*/
class DropboxGuzzleHttpClient implements DropboxHttpClientInterface
{
/**
* GuzzleHttp client.
*
* @var \GuzzleHttp\Client
*/
protected $client;
/**
* Create a new DropboxGuzzleHttpClient instance.
*
* @param Client $client GuzzleHttp Client
*/
public function __construct(Client $client = null)
{
//Set the client
$this->client = $client ?: new Client();
}
/**
* Send request to the server and fetch the raw response.
*
* @param string $url URL/Endpoint to send the request to
* @param string $method Request Method
* @param string|resource|StreamInterface $body Request Body
* @param array $headers Request Headers
* @param array $options Additional Options
*
* @return \Kunnu\Dropbox\Http\DropboxRawResponse Raw response from the server
*
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
*/
public function send($url, $method, $body, $headers = [], $options = [])
{
//Create a new Request Object
$request = new Request($method, $url, $headers, $body);
try {
//Send the Request
$rawResponse = $this->client->send($request, $options);
} catch (BadResponseException $e) {
throw new DropboxClientException($e->getResponse()->getBody(), $e->getCode(), $e);
} catch (RequestException $e) {
$rawResponse = $e->getResponse();
if (! $rawResponse instanceof ResponseInterface) {
throw new DropboxClientException($e->getMessage(), $e->getCode());
}
}
//Something went wrong
if ($rawResponse->getStatusCode() >= 400) {
throw new DropboxClientException($rawResponse->getBody());
}
if (array_key_exists('sink', $options)) {
//Response Body is saved to a file
$body = '';
} else {
//Get the Response Body
$body = $this->getResponseBody($rawResponse);
}
$rawHeaders = $rawResponse->getHeaders();
$httpStatusCode = $rawResponse->getStatusCode();
//Create and return a DropboxRawResponse object
return new DropboxRawResponse($rawHeaders, $body, $httpStatusCode);
}
/**
* Get the Response Body.
*
* @param string|\Psr\Http\Message\ResponseInterface $response Response object
*
* @return string
*/
protected function getResponseBody($response)
{
//Response must be string
$body = $response;
if ($response instanceof ResponseInterface) {
//Fetch the body
$body = $response->getBody();
}
if ($body instanceof StreamInterface) {
$body = $body->getContents();
}
return (string) $body;
}
}