1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Majora\Framework\Api\Client; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use Majora\Framework\Api\Request\RestApiRequestFactory; |
7
|
|
|
use Majora\Framework\Log\LoggableTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Rest api client which use standard Http client to handle http calls. |
11
|
|
|
* Use and returns non parsed data from api calls. |
12
|
|
|
*/ |
13
|
|
|
class RestApiClient implements ApiClientInterface |
14
|
|
|
{ |
15
|
|
|
use LoggableTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var ClientInterface |
19
|
|
|
*/ |
20
|
|
|
protected $httpClient; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var RestApiRequestFactory |
24
|
|
|
*/ |
25
|
|
|
protected $requestFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Construct. |
29
|
|
|
* |
30
|
|
|
* @param ClientInterface $httpClient |
31
|
|
|
* @param RestApiRequestFactory $requestFactory |
32
|
|
|
*/ |
33
|
|
|
public function __construct(ClientInterface $httpClient, RestApiRequestFactory $requestFactory) |
34
|
|
|
{ |
35
|
|
|
$this->httpClient = $httpClient; |
36
|
|
|
$this->requestFactory = $requestFactory; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @see ApiClientInterface::send() |
41
|
|
|
*/ |
42
|
|
|
public function send( |
43
|
|
|
$name, |
44
|
|
|
$method, |
45
|
|
|
array $query = array(), |
46
|
|
|
array $body = array(), |
47
|
|
|
array $options = array() |
48
|
|
|
) { |
49
|
|
|
return $this->httpClient->request( |
50
|
|
|
$method, |
51
|
|
|
$this->requestFactory->createRequestUri($name, $query), |
52
|
|
|
array_replace_recursive( |
53
|
|
|
$this->requestFactory->createRequestOptions($options), |
54
|
|
|
array('json' => $this->requestFactory->createRequestBodyData($body)) |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @see ApiClientInterface::cget() |
61
|
|
|
*/ |
62
|
|
|
public function cget(array $query = array(), array $options = array()) |
63
|
|
|
{ |
64
|
|
|
return $this->send('cget', 'GET', $query, array(), $options); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @see ApiClientInterface::get() |
69
|
|
|
*/ |
70
|
|
|
public function get(array $query = array(), array $options = array()) |
71
|
|
|
{ |
72
|
|
|
return $this->send('get', 'GET', $query, array(), $options); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @see ApiClientInterface::post() |
77
|
|
|
*/ |
78
|
|
|
public function post(array $query = array(), array $body = array(), array $options = array()) |
79
|
|
|
{ |
80
|
|
|
return $this->send('post', 'POST', $query, $body, $options); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @see ApiClientInterface::put() |
85
|
|
|
*/ |
86
|
|
|
public function put(array $query = array(), array $body = array(), array $options = array()) |
87
|
|
|
{ |
88
|
|
|
return $this->send('put', 'PUT', $query, $body, $options); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @see ApiClientInterface::delete() |
93
|
|
|
*/ |
94
|
|
|
public function delete(array $query = array(), array $body = array(), array $options = array()) |
95
|
|
|
{ |
96
|
|
|
return $this->send('delete', 'DELETE', $query, $body, $options); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|