|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Blocktrail\SDK\Connection; |
|
4
|
|
|
|
|
5
|
|
|
use Blocktrail\SDK\Blocktrail; |
|
6
|
|
|
use GuzzleHttp\Client as Guzzle; |
|
7
|
|
|
use GuzzleHttp\Handler\CurlHandler; |
|
8
|
|
|
use GuzzleHttp\HandlerStack; |
|
9
|
|
|
use HttpSignatures\Context; |
|
10
|
|
|
use HttpSignatures\GuzzleHttpSignatures; |
|
11
|
|
|
|
|
12
|
|
|
class RestClient extends BaseRestClient |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $options = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $curlOptions = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Guzzle |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $guzzle; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* GuzzleRestClient constructor. |
|
31
|
|
|
* @param $apiEndpoint |
|
32
|
|
|
* @param $apiVersion |
|
33
|
|
|
* @param $apiKey |
|
34
|
|
|
* @param $apiSecret |
|
35
|
|
|
*/ |
|
36
|
118 |
|
public function __construct($apiEndpoint, $apiVersion, $apiKey, $apiSecret) { |
|
37
|
118 |
|
parent::__construct($apiEndpoint, $apiVersion, $apiKey, $apiSecret); |
|
38
|
118 |
|
$this->guzzle = $this->createGuzzleClient(); |
|
39
|
118 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $options |
|
43
|
|
|
* @param array $curlOptions |
|
44
|
|
|
* @return Guzzle |
|
45
|
|
|
*/ |
|
46
|
118 |
|
protected function createGuzzleClient(array $options = [], array $curlOptions = []) { |
|
47
|
118 |
|
$options = $options + $this->options; |
|
48
|
118 |
|
$curlOptions = $curlOptions + $this->curlOptions; |
|
49
|
|
|
|
|
50
|
118 |
|
$context = new Context([ |
|
51
|
118 |
|
'keys' => [$this->apiKey => $this->apiSecret], |
|
52
|
118 |
|
'algorithm' => 'hmac-sha256', |
|
53
|
|
|
'headers' => ['(request-target)', 'Content-MD5', 'Date'], |
|
54
|
|
|
]); |
|
55
|
|
|
|
|
56
|
118 |
|
$curlHandler = new CurlHandler($curlOptions); |
|
57
|
118 |
|
$handler = HandlerStack::create($curlHandler); |
|
58
|
118 |
|
$handler->push(GuzzleHttpSignatures::middlewareFromContext($context)); |
|
59
|
|
|
|
|
60
|
118 |
|
return new Guzzle($options + array( |
|
61
|
118 |
|
'handler' => $handler, |
|
62
|
118 |
|
'base_uri' => $this->apiEndpoint, |
|
63
|
|
|
'headers' => array( |
|
64
|
|
|
'User-Agent' => Blocktrail::SDK_USER_AGENT . '/' . Blocktrail::SDK_VERSION, |
|
65
|
|
|
), |
|
66
|
|
|
'http_errors' => false, |
|
67
|
118 |
|
'connect_timeout' => 3, |
|
68
|
118 |
|
'timeout' => 20.0, // tmp until we have a good matrix of all the requests and their expect min/max time |
|
69
|
|
|
'verify' => true, |
|
70
|
118 |
|
'proxy' => '', |
|
71
|
|
|
'debug' => false, |
|
72
|
|
|
'config' => array(), |
|
73
|
118 |
|
'auth' => '', |
|
74
|
|
|
)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return Guzzle |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getGuzzleClient() { |
|
81
|
|
|
return $this->guzzle; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* enable CURL debugging output |
|
86
|
|
|
* |
|
87
|
|
|
* @param bool $debug |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setCurlDebugging($debug = true) { |
|
90
|
|
|
$this->options['debug'] = $debug; |
|
91
|
|
|
|
|
92
|
|
|
$this->guzzle = $this->createGuzzleClient(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* set cURL default option on Guzzle client |
|
98
|
|
|
* @param string $key |
|
99
|
|
|
* @param bool $value |
|
100
|
|
|
*/ |
|
101
|
|
|
public function setCurlDefaultOption($key, $value) { |
|
102
|
|
|
$this->curlOptions[$key] = $value; |
|
103
|
|
|
|
|
104
|
|
|
$this->guzzle = $this->createGuzzleClient(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* set the proxy config for Guzzle |
|
109
|
|
|
* |
|
110
|
|
|
* @param $proxy |
|
111
|
|
|
*/ |
|
112
|
|
|
public function setProxy($proxy) { |
|
113
|
|
|
$this->options['proxy'] = $proxy; |
|
114
|
|
|
|
|
115
|
|
|
$this->guzzle = $this->createGuzzleClient(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* generic request executor |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $method GET, POST, PUT, DELETE |
|
122
|
|
|
* @param string $endpointUrl |
|
123
|
|
|
* @param array $queryString |
|
124
|
|
|
* @param array|string $body |
|
125
|
|
|
* @param string $auth http-signatures to enable http-signature signing |
|
126
|
|
|
* @param string $contentMD5Mode body or url |
|
127
|
|
|
* @param float $timeout timeout in seconds |
|
128
|
|
|
* @return Response |
|
129
|
|
|
*/ |
|
130
|
33 |
|
public function request($method, $endpointUrl, $queryString = null, $body = null, $auth = null, $contentMD5Mode = null, $timeout = null) { |
|
131
|
33 |
|
$request = $this->buildRequest($method, $endpointUrl, $queryString, $body, $auth, $contentMD5Mode, $timeout); |
|
132
|
33 |
|
$response = $this->guzzle->send($request, ['auth' => $auth, 'timeout' => $timeout]); |
|
133
|
|
|
|
|
134
|
33 |
|
return $this->responseHandler($response); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|