Completed
Pull Request — master (#49)
by thomas
97:33 queued 28:50
created

RestClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
    public function __construct($apiEndpoint, $apiVersion, $apiKey, $apiSecret) {
37
        parent::__construct($apiEndpoint, $apiVersion, $apiKey, $apiSecret);
38
        $this->guzzle = $this->createGuzzleClient();
39
    }
40
41
    /**
42
     * @param array $options
43
     * @param array $curlOptions
44
     * @return Guzzle
45
     */
46
    protected function createGuzzleClient(array $options = [], array $curlOptions = []) {
47
        $options = $options + $this->options;
48
        $curlOptions = $curlOptions + $this->curlOptions;
49
50
        $context = new Context([
51
            'keys' => [$this->apiKey => $this->apiSecret],
52
            'algorithm' => 'hmac-sha256',
53
            'headers' => ['(request-target)', 'Content-MD5', 'Date'],
54
        ]);
55
56
        $curlHandler = new CurlHandler($curlOptions);
57
        $handler = HandlerStack::create($curlHandler);
58
        $handler->push(GuzzleHttpSignatures::middlewareFromContext($context));
59
60
        return new Guzzle($options + array(
61
            'handler' => $handler,
62
            'base_uri' => $this->apiEndpoint,
63
            'headers' => array(
64
                'User-Agent' => Blocktrail::SDK_USER_AGENT . '/' . Blocktrail::SDK_VERSION,
65
            ),
66
            'http_errors' => false,
67
            'connect_timeout' => 3,
68
            'timeout' => 20.0, // tmp until we have a good matrix of all the requests and their expect min/max time
69
            'verify' => true,
70
            'proxy' => '',
71
            'debug' => false,
72 116
            'config' => array(),
73 116
            'auth' => '',
74 116
        ));
75 116
    }
76 116
77
    /**
78 116
     * @return Guzzle
79 116
     */
80
    public function getGuzzleClient() {
81
        return $this->guzzle;
82
    }
83
84
    /**
85
     * enable CURL debugging output
86 116
     *
87 116
     * @param   bool        $debug
88 116
     */
89
    public function setCurlDebugging($debug = true) {
90 116
        $this->options['debug'] = $debug;
91 116
92 116
        $this->guzzle = $this->createGuzzleClient();
93
    }
94
95
96 116
    /**
97 116
     * set cURL default option on Guzzle client
98 116
     * @param string    $key
99
     * @param bool      $value
100 116
     */
101 116
    public function setCurlDefaultOption($key, $value) {
102 116
        $this->curlOptions[$key] = $value;
103
104
        $this->guzzle = $this->createGuzzleClient();
105
    }
106
107 116
    /**
108 116
     * set the proxy config for Guzzle
109
     *
110 116
     * @param   $proxy
111
     */
112
    public function setProxy($proxy) {
113 116
        $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
    public function request($method, $endpointUrl, $queryString = null, $body = null, $auth = null, $contentMD5Mode = null, $timeout = null) {
131
        $request = $this->buildRequest($method, $endpointUrl, $queryString, $body, $auth, $contentMD5Mode, $timeout);
132
        $response = $this->guzzle->send($request, ['auth' => $auth, 'timeout' => $timeout]);
133
134
        return $this->responseHandler($response);
135
    }
136
}
137