1 | <?php |
||
9 | class GuzzleClient extends Client |
||
10 | { |
||
11 | /** |
||
12 | * @var GuzzleHttpClient |
||
13 | */ |
||
14 | protected $client; |
||
15 | |||
16 | /** |
||
17 | * GuzzleClient constructor. |
||
18 | */ |
||
19 | 54 | public function __construct() |
|
20 | { |
||
21 | 54 | $this->client = new GuzzleHttpClient([ |
|
22 | 54 | 'base_uri' => self::API_BASE_URL, |
|
23 | 'headers' => [ |
||
24 | 'User-Agent' => 'MercadoPago PHP SDK v' . MercadoPago::VERSION, |
||
25 | 'Content-Type' => 'application/json', |
||
26 | 'Accept' => 'application/json', |
||
27 | ], |
||
28 | ]); |
||
29 | 54 | } |
|
30 | |||
31 | /** |
||
32 | * @inheritdoc |
||
33 | */ |
||
34 | 24 | public function request($uri = '/', $method = 'GET', array $data = [], array $params = []) |
|
35 | { |
||
36 | 24 | $options = []; |
|
37 | |||
38 | 24 | if ($method !== self::METHOD_GET && !empty($data)) { |
|
39 | 15 | if (empty($params['form'])) { |
|
40 | 9 | $options['json'] = $data; |
|
41 | } else { |
||
42 | 6 | $options['form_params'] = $data; |
|
43 | } |
||
44 | } |
||
45 | |||
46 | 24 | if ($method === self::METHOD_GET && !empty($data)) { |
|
47 | 3 | $options['query'] = $data; |
|
48 | } |
||
49 | |||
50 | 24 | if (!empty($params['access_token'])) { |
|
51 | 18 | $options['query']['access_token'] = $params['access_token']; |
|
52 | } |
||
53 | |||
54 | try { |
||
55 | 24 | $response = $this->client->request($method, $uri, $options); |
|
56 | 12 | } catch (ClientException $exception) { |
|
57 | 12 | $response = $exception->getResponse(); |
|
58 | } |
||
59 | |||
60 | 24 | return new Response($response->getStatusCode(), $response->getBody()); |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | 9 | public function get($uri = '/', array $data = [], array $params = []) |
|
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | 15 | public function post($uri = '/', array $data = [], array $params = []) |
|
78 | |||
79 | /** |
||
80 | * @inheritdoc |
||
81 | */ |
||
82 | public function put($uri = '/', array $data = [], array $params = []) |
||
86 | |||
87 | /** |
||
88 | * @inheritdoc |
||
89 | */ |
||
90 | public function delete($uri = '/', array $data = [], array $params = []) |
||
94 | } |
||
95 |