1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Artstorm\MonkeyLearn\Api; |
4
|
|
|
|
5
|
|
|
use Artstorm\MonkeyLearn\HttpClient\Response; |
6
|
|
|
use Artstorm\MonkeyLearn\Client; |
7
|
|
|
|
8
|
|
|
abstract class ApiAbstract |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* HTTP client. |
12
|
|
|
* |
13
|
|
|
* @var Client |
14
|
|
|
*/ |
15
|
|
|
protected $client; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Assign dependencies. |
19
|
|
|
* |
20
|
|
|
* @param Client $client |
21
|
|
|
*/ |
22
|
12 |
|
public function __construct(Client $client) |
23
|
|
|
{ |
24
|
12 |
|
$this->client = $client; |
25
|
12 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Send a POST request with JSON encoded parameters. |
29
|
|
|
* |
30
|
|
|
* @param string $path |
31
|
|
|
* @param array $parameters |
32
|
|
|
* @param array $headers |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
6 |
|
protected function post($path, array $parameters = [], array $headers = []) |
37
|
|
|
{ |
38
|
6 |
|
return $this->postRaw( |
39
|
6 |
|
$path, |
40
|
6 |
|
$this->createJsonBody($parameters), |
41
|
|
|
$headers |
42
|
6 |
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Send a POST request with raw data. |
47
|
|
|
* |
48
|
|
|
* @param string $path |
49
|
|
|
* @param mixed $body |
50
|
|
|
* @param array $headers |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
6 |
|
protected function postRaw($path, $body, array $headers = []) |
55
|
|
|
{ |
56
|
6 |
|
$response = $this->client->getHttpClient()->post( |
57
|
6 |
|
$path, |
58
|
6 |
|
$body, |
59
|
|
|
$headers |
60
|
6 |
|
); |
61
|
|
|
|
62
|
6 |
|
return $this->getResponse($response); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Create a JSON encoded version of an array of parameters. |
67
|
|
|
* |
68
|
|
|
* @param array $parameters |
69
|
|
|
* |
70
|
|
|
* @return null|string |
71
|
|
|
*/ |
72
|
6 |
|
protected function createJsonBody(array $parameters) |
73
|
|
|
{ |
74
|
6 |
|
if (count($parameters) === 0) { |
75
|
2 |
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
4 |
|
return json_encode($parameters); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Extracts the relevant content from the response. |
83
|
|
|
* |
84
|
|
|
* @param Response $response |
85
|
|
|
* |
86
|
|
|
* @return array|mixed |
87
|
|
|
*/ |
88
|
6 |
|
protected function getResponse(Response $response) |
89
|
|
|
{ |
90
|
6 |
|
$body = $response->getBody(); |
91
|
6 |
|
$content = json_decode($body, true); |
92
|
|
|
|
93
|
6 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
94
|
2 |
|
return $body; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Add remainin query limits to the response |
98
|
4 |
|
$content['limits'] = $this->getQueryLimitHeaders($response); |
99
|
|
|
|
100
|
4 |
|
return $content; |
101
|
|
|
} |
102
|
|
|
|
103
|
4 |
|
protected function getQueryLimitHeaders(Response $response, array $limits = []) |
104
|
|
|
{ |
105
|
|
|
$headers = [ |
106
|
4 |
|
'X-Query-Limit-Limit', |
107
|
4 |
|
'X-Query-Limit-Remaining', |
108
|
|
|
'X-Query-Limit-Request-Queries' |
109
|
4 |
|
]; |
110
|
|
|
|
111
|
4 |
|
foreach ($headers as $header) { |
112
|
4 |
|
$limits[$header] = $response->getHeader($header); |
113
|
4 |
|
} |
114
|
|
|
|
115
|
4 |
|
return $limits; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|