Passed
Pull Request — master (#129)
by thomas
26:40 queued 15:56
created

RestClient   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 63.41%

Importance

Changes 0
Metric Value
wmc 9
eloc 41
dl 0
loc 136
c 0
b 0
f 0
ccs 26
cts 41
cp 0.6341
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setProxy() 0 4 1
A __construct() 0 3 1
A setThrottle() 0 2 1
A request() 0 8 2
A setCurlDefaultOption() 0 4 1
A createGuzzleClient() 0 28 1
A setCurlDebugging() 0 4 1
A getGuzzleClient() 0 2 1
1
<?php
2
3
namespace Blocktrail\SDK\Connection;
4
5
use Blocktrail\SDK\Blocktrail;
6
use Blocktrail\SDK\Throttler;
7
use GuzzleHttp\Client as Guzzle;
8
use GuzzleHttp\Handler\CurlHandler;
9
use GuzzleHttp\HandlerStack;
10
use HttpSignatures\Context;
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
     * @var Throttler
31
     */
32
    protected $throttler;
33
34
    /**
35
     * GuzzleRestClient constructor.
36
     * @param $apiEndpoint
37
     * @param $apiVersion
38
     * @param $apiKey
39
     * @param $apiSecret
40
     * @param Throttler|null $throttler
41
     */
42 119
    public function __construct($apiEndpoint, $apiVersion, $apiKey, $apiSecret) {
43 119
        parent::__construct($apiEndpoint, $apiVersion, $apiKey, $apiSecret);
44 119
        $this->guzzle = $this->createGuzzleClient();
45 119
    }
46 119
47
    public function setThrottle($throttle) {
48
        $this->throttler = Throttler::getInstance($this->apiEndpoint, $throttle);
49
    }
50
51 119
    /**
52 119
     * @param array $options
53
     * @param array $curlOptions
54
     * @return Guzzle
55
     */
56
    protected function createGuzzleClient(array $options = [], array $curlOptions = []) {
57
        $options = $options + $this->options;
58
        $curlOptions = $curlOptions + $this->curlOptions;
59 119
60 119
        $context = new Context([
0 ignored issues
show
Unused Code introduced by
The assignment to $context is dead and can be removed.
Loading history...
61 119
            'keys' => [$this->apiKey => $this->apiSecret],
62
            'algorithm' => 'hmac-sha256',
63 119
            'headers' => ['(request-target)', 'Content-MD5', 'Date'],
64 119
        ]);
65 119
66
        $curlHandler = new CurlHandler($curlOptions);
67
        $handler = HandlerStack::create($curlHandler);
68
//        $handler->push(GuzzleHttpSignatures::middlewareFromContext($context));
69 119
70 119
        return new Guzzle($options + array(
71
            'handler' => $handler,
72
            'base_uri' => $this->apiEndpoint,
73 119
            'headers' => array(
74 119
                'User-Agent' => Blocktrail::SDK_USER_AGENT . '/' . Blocktrail::SDK_VERSION,
75 119
            ),
76
            'http_errors' => false,
77
            'connect_timeout' => 3,
78
            'timeout' => 20.0, // tmp until we have a good matrix of all the requests and their expect min/max time
79
            'verify' => true,
80 119
            'proxy' => '',
81 119
            'debug' => false,
82
            'config' => array(),
83 119
            'auth' => '',
84
        ));
85
    }
86 119
87
    /**
88
     * @return Guzzle
89
     */
90
    public function getGuzzleClient() {
91
        return $this->guzzle;
92
    }
93
94
    /**
95
     * enable CURL debugging output
96
     *
97
     * @param   bool        $debug
98
     */
99
    public function setCurlDebugging($debug = true) {
100
        $this->options['debug'] = $debug;
101
102
        $this->guzzle = $this->createGuzzleClient();
103
    }
104
105
106
    /**
107
     * set cURL default option on Guzzle client
108
     * @param string    $key
109
     * @param bool      $value
110
     */
111
    public function setCurlDefaultOption($key, $value) {
112
        $this->curlOptions[$key] = $value;
113
114
        $this->guzzle = $this->createGuzzleClient();
115
    }
116
117
    /**
118
     * set the proxy config for Guzzle
119
     *
120
     * @param   $proxy
121
     */
122
    public function setProxy($proxy) {
123
        $this->options['proxy'] = $proxy;
124
125
        $this->guzzle = $this->createGuzzleClient();
126
    }
127
128
    /**
129
     * generic request executor
130
     *
131
     * @param   string          $method         GET, POST, PUT, DELETE
132
     * @param   string          $endpointUrl
133
     * @param   array           $queryString
134
     * @param   array|string    $body
135
     * @param   string          $auth           http-signatures to enable http-signature signing
136
     * @param   string          $contentMD5Mode body or url
137
     * @param   float           $timeout        timeout in seconds
138
     * @return Response
139
     */
140
    public function request($method, $endpointUrl, $queryString = null, $body = null, $auth = null, $contentMD5Mode = null, $timeout = null) {
141
        $request = $this->buildRequest($method, $endpointUrl, $queryString, $body, $auth, $contentMD5Mode, $timeout);
142
        if ($this->throttler) {
143 33
            $this->throttler->waitForThrottle();
144 33
        }
145
        $response = $this->guzzle->send($request, ['auth' => $auth, 'timeout' => $timeout]);
146 33
147 33
        return $this->responseHandler($response);
148
    }
149
}
150