1 | <?php |
||
9 | abstract class Resource |
||
10 | { |
||
11 | /** |
||
12 | * The API key used to access the New Relic API, should be an admin or REST api key |
||
13 | * @see https://docs.newrelic.com/docs/apis/rest-api-v2/requirements/api-keys |
||
14 | * |
||
15 | * @param string |
||
16 | */ |
||
17 | protected $apiKey; |
||
18 | |||
19 | /** |
||
20 | * The endpoint requests will be sent to |
||
21 | * @see https://docs.newrelic.com/docs/apis/rest-api-v2/requirements/new-relic-rest-api-v2-getting-started#appid |
||
22 | * |
||
23 | * @param string |
||
24 | */ |
||
25 | private $apiUrl = 'https://api.newrelic.com/v2/'; |
||
26 | |||
27 | /** |
||
28 | * Payload to be sent with the request |
||
29 | * |
||
30 | * @param array |
||
31 | */ |
||
32 | private $data = []; |
||
33 | |||
34 | /** |
||
35 | * Monolog instance to be used for logging messages |
||
36 | * |
||
37 | * @param Logger |
||
38 | */ |
||
39 | protected $logger; |
||
40 | |||
41 | /** |
||
42 | * Create a new resource instance |
||
43 | * |
||
44 | * @param string $apiKey |
||
45 | * @param Logger $logger |
||
46 | */ |
||
47 | public function __construct($apiKey, Logger $logger = null) |
||
52 | |||
53 | /** |
||
54 | * Add some data to the payload to be sent with the request |
||
55 | * |
||
56 | * @param string $name |
||
57 | * @param mixed $value |
||
58 | * @return void |
||
59 | */ |
||
60 | public function addData($name, $value) |
||
64 | |||
65 | /** |
||
66 | * Log a message |
||
67 | * |
||
68 | * @param string $type |
||
69 | * @param string $message |
||
70 | * @param array $data |
||
71 | * @return void |
||
72 | */ |
||
73 | protected function addLog($type, $message, array $data = []) |
||
89 | |||
90 | /** |
||
91 | * Check a string is valid and within a set length |
||
92 | * |
||
93 | * @param string $name |
||
94 | * @param string $string |
||
95 | * @param integer $length |
||
96 | * @throws Exception |
||
97 | * @return boolean |
||
98 | */ |
||
99 | protected function checkString($name, $string, $length = 0) |
||
111 | |||
112 | /** |
||
113 | * Check if an integer is valid and positive |
||
114 | * |
||
115 | * @param string $name |
||
116 | * @param integer $integer |
||
117 | * @param boolean $positive |
||
118 | * @throws Exception |
||
119 | * @return boolean |
||
120 | */ |
||
121 | protected function checkInteger($name, $integer, $positive = true) |
||
140 | |||
141 | /** |
||
142 | * Convert most date/times to a timestamp |
||
143 | * |
||
144 | * @param mixed $date |
||
145 | * @param string $format |
||
146 | * @return int |
||
147 | */ |
||
148 | protected function getDate($date, $format = 'U') |
||
157 | |||
158 | /** |
||
159 | * Log a critical message and throw an exception |
||
160 | * |
||
161 | * @param string $message |
||
162 | * @throws Exception |
||
163 | * @return void |
||
164 | */ |
||
165 | protected function throwException($message) |
||
171 | |||
172 | /** |
||
173 | * Send a request to the API endpoint and return json response |
||
174 | * |
||
175 | * @param string $uri |
||
176 | * @param string $method |
||
177 | * @throws GuzzleHttp\Exception\ClientException Exception thrown by Guzzle |
||
178 | * @return string |
||
179 | */ |
||
180 | protected function request($uri, $method = 'GET') |
||
193 | } |
||
194 |