• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hyperwallet / php-sdk / 3912820865

pending completion
3912820865

push

github

GitHub
DTPAYETWO-761- Added 'isDefaultTransferMethod' attribute for default accounts(V3) (#125)

12 of 12 new or added lines in 4 files covered. (100.0%)

1821 of 1861 relevant lines covered (97.85%)

18.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.65
/src/Hyperwallet/Util/ApiClient.php
1
<?php
2
namespace Hyperwallet\Util;
3
use GuzzleHttp\Client;
4
use GuzzleHttp\Exception\BadResponseException;
5
use GuzzleHttp\Exception\ConnectException;
6
use Hyperwallet\Exception\HyperwalletApiException;
7
use Hyperwallet\Exception\HyperwalletException;
8
use Hyperwallet\Model\BaseModel;
9
use Hyperwallet\Response\ErrorResponse;
10

11
/**
12
 * The internal API client
13
 *
14
 * @package Hyperwallet\Util
15
 */
16
class ApiClient {
17

18
    /**
19
     * The SDK version number
20
     *
21
     * @var string
22
     */
23
    const VERSION = '1.7.3';
24

25
    /**
26
     * The Guzzle http client
27
     *
28
     * @var Client
29
     */
30
    private $client;
31

32
    /**
33
     * The Encryption service for http client requests/responses
34
     *
35
     * @var HyperwalletEncryption
36
     */
37
    private $encryption;
38

39
    /**
40
     * The UUID generator for http request/response
41
     *
42
     * @var HyperwalletUUID
43
     */
44
    private $uuid;
45

46
    /**
47
     * Boolean flag that checks if ApiClient is constructed with encryption enabled or not
48
     *
49
     * @var boolean
50
     */
51
    private $isEncrypted = false;
52

53
    /**
54
     * Creates a instance of the API client
55
     *
56
     * @param string $username The API username
57
     * @param string $password The API password
58
     * @param string $server The API server to connect to
59
     * @param array $clientOptions Guzzle Client Options
60
     * @param array $encryptionData Encryption data to initialize ApiClient with encryption enabled
61
     */
62
    public function __construct($username, $password, $server, $clientOptions = array(), $encryptionData = array()) {
63
        $this->uuid = HyperwalletUUID::v4();
352✔
64
        // Setup http client if not specified
65
        $this->client = new Client(array_merge_recursive(array(
352✔
66
            'base_uri' => $server,
352✔
67
            'auth' => array($username, $password),
352✔
68
            'headers' => array(
69
                'User-Agent' => 'Hyperwallet PHP SDK v' . self::VERSION,
352✔
70
                'Accept' => 'application/json',
352✔
71
                'x-sdk-version' => self::VERSION,
352✔
72
                'x-sdk-type' => 'PHP',
352✔
73
                'x-sdk-contextId' => $this->uuid)
352✔
74
        ), $clientOptions));
352✔
75
        if (!empty($encryptionData) && isset($encryptionData['clientPrivateKeySetLocation']) &&
352✔
76
            isset($encryptionData['hyperwalletKeySetLocation'])) {
352✔
77
            $this->isEncrypted = true;
5✔
78
            $this->encryption = new HyperwalletEncryption($encryptionData['clientPrivateKeySetLocation'], $encryptionData['hyperwalletKeySetLocation']);
5✔
79
        }
5✔
80
    }
352✔
81

82
    /**
83
     * Do a POST call to the Hyperwallet API server
84
     *
85
     * @param string $partialUrl The url template
86
     * @param array $uriParams The url template parameters
87
     * @param BaseModel $data The data to submit
88
     * @param array $query Query parameters
89
     * @param array $headers Additional headers
90
     * @return array
91
     *
92
     * @throws HyperwalletApiException
93
     */
94
    public function doPost($partialUrl, array $uriParams, BaseModel $data = null, array $query = array(), array $headers = array()) {
95
       return $this->doRequest('POST', $partialUrl, $uriParams, array(
17✔
96
            'query' => $query,
17✔
97
            'body' => $data ? \GuzzleHttp\json_encode($data->getPropertiesForCreate(), JSON_FORCE_OBJECT) : '{}',
17✔
98
            'headers' => array_merge($headers, array(
17✔
99
                'Content-Type' => 'application/json'
100
            ))
17✔
101
        ));
17✔
102
    }
103

104
    /**
105
     * Do a PUT call to the Hyperwallet API server
106
     *
107
     * @param string $partialUrl The url template
108
     * @param array $uriParams The url template parameters
109
     * @param BaseModel $data The data to update
110
     * @param array $query Query parameters
111
     * @return array
112
     *
113
     * @throws HyperwalletApiException
114
     */
115
    public function doPut($partialUrl, array $uriParams, BaseModel $data, array $query) {
116
        return $this->doRequest('PUT', $partialUrl, $uriParams, array(
8✔
117
            'query' => $query,
8✔
118
            'body' => \GuzzleHttp\json_encode($data->getPropertiesForUpdate(), JSON_FORCE_OBJECT),
8✔
119
            'headers' => array(
120
                'Content-Type' => 'application/json'
121
            )
8✔
122
        ));
8✔
123
    }
124

125
    /**
126
     * Do a GET call to the Hyperwallet API server
127
     *
128
     * @param string $partialUrl The url template
129
     * @param array $uriParams The url template parameters
130
     * @param array $query Query parameters
131
     * @return array
132
     *
133
     * @throws HyperwalletApiException
134
     */
135
    public function doGet($partialUrl, array $uriParams, array $query) {
136
        return $this->doRequest('GET', $partialUrl, $uriParams, array(
9✔
137
            'query' => $query
138
        ));
9✔
139
    }
140

141
    /**
142
     * Execute API call and map error messages
143
     *
144
     * @param string $method The http method
145
     * @param string $url The url template
146
     * @param array $urlParams The url template parameters
147
     * @param array $options The request options
148
     * @return array
149
     *
150
     * @throws HyperwalletApiException
151
     */
152
    private function doRequest($method, $url, array $urlParams, array $options) {
153
        try {
154
            $uri = new HyperwalletUriTemplate();
34✔
155
            if (!isset($options['headers'])) {
34✔
156
                $options[] = array('headers' => array());
9✔
157
            }
9✔
158
            $options['headers']['Accept'] = 'application/json';
34✔
159
            if ($this->isEncrypted) {
34✔
160
                $options['headers']['Accept'] = 'application/jose+json';
5✔
161
                $options['headers']['Content-Type'] = 'application/jose+json';
5✔
162
                if (isset($options['body'])) {
5✔
163
                    $options['body'] = $this->encryption->encrypt(json_decode($options['body'], true));
5✔
164
                }
5✔
165
            }
5✔
166
            $response = $this->client->request($method, $uri->expand($url, $urlParams), $options);
34✔
167
            if ($response->getStatusCode() === 204) {
23✔
168
                return array();
4✔
169
            }
170
            $this->checkResponseHeaderContentType($response);
19✔
171
            $body = $this->isEncrypted ? \GuzzleHttp\json_decode(\GuzzleHttp\json_encode($this->encryption->decrypt($response->getBody())), true) :
17✔
172
                \GuzzleHttp\json_decode($response->getBody(), true);
17✔
173
            if (isset($body['links'])) {
17✔
174
                unset($body['links']);
14✔
175
            }
14✔
176
            return $body;
17✔
177
        } catch (ConnectException $e) {
13✔
178
            $errorResponse = new ErrorResponse(0, array('errors' => array(
3✔
179
                array(
180
                    'message' => 'Could not communicate with ' . $this->client->getConfig('base_uri'),
3✔
181
                    'code' => 'COMMUNICATION_ERROR'
1✔
182
                )
3✔
183
            )));
3✔
184
            throw new HyperwalletApiException($errorResponse, $e);
3✔
185
        } catch (BadResponseException $e) {
10✔
186
            $body = \GuzzleHttp\json_decode($e->getResponse()->getBody(), true);
8✔
187
            if (is_null($body) || !isset($body['errors']) || empty($body['errors'])) {
8✔
188
                $body = array('errors' => array(
189
                    array(
190
                        'message' => 'Failed to get any error message from response',
1✔
191
                        'code' => 'BAD_REQUEST'
192
                    )
1✔
193
                ));
1✔
194
            }
1✔
195
            $errorResponse = new ErrorResponse($e->getResponse()->getStatusCode(), $body);
8✔
196
            throw new HyperwalletApiException($errorResponse, $e);
8✔
197
        }
198
    }
199

200
    /**
201
     * Checks whether Content-Type header is valid in response
202
     *
203
     * @param string $response Response to be checked
204
     *
205
     * @throws HyperwalletException
206
     */
207
    private function checkResponseHeaderContentType($response) {
208
        $contentType = implode('', $response->getHeader('Content-Type'));
19✔
209
        $expectedContentType = $this->isEncrypted ? 'application/jose+json' : 'application/json';
19✔
210
        $invalidContentType = $response->getStatusCode() !== 204 && !empty($contentType) && strpos($contentType, $expectedContentType) === false;
19✔
211
        if ($invalidContentType) {
19✔
212
             throw new HyperwalletException('Invalid Content-Type specified in Response Header');
2✔
213
        }
214
    }
17✔
215

216
    /**
217
     * Do a PUT call to the Hyperwallet API server
218
     *
219
     * @param string $partialUrl The url template
220
     * @param array $uriParams The url template parameters
221
     * @param array $options The request options
222
     * @return array
223
     *
224
     * @throws HyperwalletApiException
225
     */
226
    public function putMultipartData($partialUrl, array $uriParams, array $options) {
227
        return $this->doRequest('PUT', $partialUrl, $uriParams, $options);
×
228
    }
229
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc