This repository has been archived by the owner on Jan 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewRelicPartnerAPI.class.php
377 lines (304 loc) · 7.72 KB
/
NewRelicPartnerAPI.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
require_once('Exceptions.php');
require_once('Objects/Account.php');
require_once('Objects/User.php');
require_once('Objects/Subscription.php');
/**
* Class NewRelicPartnerAPI
*
* Interface to the NewRelic Partner API v2
*
* @author Robin Corps <[email protected]>
* @version 0.1
*/
class NewRelicPartnerAPI
{
const STAGING = 1;
const LIVE = 2;
const STAGING_URL = 'https://staging.newrelic.com/api/v2/partners/';
const LIVE_URL = 'https://rpm.newrelic.com/api/v2/partners/';
const GET = 1;
const POST = 2;
const PUT = 3;
const DELETE = 4;
public $account;
public $user;
public $subscription;
private $curl = null;
private $curl_opts = array();
private $ipv4_fallback = true; // if server is using IPv6 but not setup correctly fall back to IPv4
private $mode;
private $partner_id;
private $api_key;
/**
* Construct a new NewRelic Partner API interface
*
* @param string $partner_id
* @param string $api_key
* @param int $mode
*
* @throws NewRelicApiException
*/
public function __construct($partner_id, $api_key, $mode = null)
{
if (!is_int($partner_id) || !is_string($api_key))
{
throw new NewRelicApiException('You must specify a Partner ID and API key.');
}
$this->setPartnerId($partner_id);
$this->setApiKey($api_key);
if ($mode === null)
{
$mode = self::LIVE;
}
$this->setMode($mode);
$this->initCurl();
$this->account = new NewRelicPartnerAPIAccount($this);
$this->user = new NewRelicPartnerAPIUser($this);
$this->subscription = new NewRelicPartnerAPISubscription($this);
}
/**
* Initialize cURL
*/
private function initCurl()
{
$this->curl = curl_init($this->getEndpoint());
$this->setCurlOpts(array(
CURLOPT_HTTPHEADER => array(
'x-api-key: ' . $this->getApiKey(),
'Content-Type: application/json'
),
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => 'php-new-relic2.0'
));
}
/**
* Get the endpoint URL based on the mode set
*
* @return string
*/
private function getEndpoint()
{
switch ($this->getMode())
{
case self::STAGING:
return self::STAGING_URL . $this->getPartnerId() . '/';
default:
case self::LIVE:
return self::LIVE_URL . $this->getPartnerId() . '/';
}
}
/**
* Get the mode of the API (staging/live)
*
* @return int
*/
public function getMode()
{
return $this->mode;
}
/**
* Set the mode of the API (staging/live)
*
* @param int $mode
*/
public function setMode($mode)
{
$this->mode = $mode;
}
/**
* Get the Partner ID in use
*
* @return string
*/
public function getPartnerId()
{
return $this->partner_id;
}
/**
* Set the Partner ID to use
*
* @param string $partner_id
*/
public function setPartnerId($partner_id)
{
$this->partner_id = $partner_id;
}
/**
* @return mixed
*/
public function getApiKey()
{
return $this->api_key;
}
/**
* @param mixed $api_key
*/
public function setApiKey($api_key)
{
$this->api_key = $api_key;
}
/**
* Call the API
*
* @param string $url
* @param array $params
* @param int $type
*/
public function call($url, $params = null, $type = null)
{
$this->setCurlOpt(CURLOPT_URL, $this->getEndpoint() . $url);
if ($params !== null)
{
$this->setCurlOpt(CURLOPT_POSTFIELDS, json_encode($params, JSON_NUMERIC_CHECK));
}
switch ($type)
{
case self::POST:
$this->setCurlOpt(CURLOPT_POST, true);
break;
case self::PUT:
$this->setCurlOpt(CURLOPT_PUT, true);
$this->setCurlOpt(CURLOPT_CUSTOMREQUEST, 'PUT');
break;
case self::DELETE:
$this->setCurlOpt(CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}
$response = curl_exec($this->curl);
$status = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
if (curl_errno($this->curl) == 60)
{
throw new NewRelicApiException('Invalid or no certificate authority found');
}
if ($response === false && !$this->curlOptSet(CURLOPT_IPRESOLVE))
{
$matches = array();
$regex = '/Failed to connect to ([^:].*): Network is unreachable/';
if (preg_match($regex, curl_error($this->curl), $matches))
{
if (strlen(@inet_pton($matches[1])) === 16)
{
if (!$this->ipv4_fallback)
{
throw new NewRelicApiTransportException(
'Invalid IPv6 configuration on server, please disable or get native IPv6 on your server.',
curl_errno($this->curl),
$this->getCurlOpts()
);
}
$this->setCurlOpt(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$response = curl_exec($this->curl);
}
}
}
if ($response === false)
{
curl_close($this->curl);
throw new NewRelicApiTransportException(
curl_error($this->curl),
curl_errno($this->curl),
$this->getCurlOpts()
);
}
curl_close($this->curl);
$result = json_decode($response, true);
if ($result === null)
{
if ($status == 500)
{
throw new NewRelicApiException('NewRelic threw a 500 error...');
}
throw new NewRelicApiException('Error decoding result as JSON (' . $status . '): ' . $response);
}
if (array_key_exists('error', $result))
{
$code = 0;
$error = $result['error'];
if (is_array($result['error']))
{
if (array_key_exists('accountview', $result['error']) && strpos($result['error']['accountview'][0], 'exists') !== false)
{
$code = NewRelicApiException::USER_EXISTS;
}
$error = var_export($result['error'], true);
}
throw new NewRelicApiException('NewRelic returned the error: ' . $error, $code);
}
return $result;
}
/**
* Return the value of a set cURL option
*
* @param $opt
*
* @return mixed
*/
public function getCurlOpt($opt)
{
if (array_key_exists($opt, $this->curl_opts))
{
return $this->curl_opts[$opt];
}
return null;
}
/**
* Allow setting of a cURL option
*
* @param int $opt
* @param mixed $value
*/
public function setCurlOpt($opt, $value)
{
$this->curl_opts[$opt] = $value;
curl_setopt($this->curl, $opt, $value);
}
/**
* Return whether or not a cURL option has been set
*
* @param int $opt
*
* @return bool
*/
public function curlOptSet($opt)
{
return array_key_exists($opt, $this->getCurlOpts());
}
/**
* Get an array of the set cURL options (can be useful for debugging)
*
* @return array
*/
public function getCurlOpts()
{
return $this->curl_opts;
}
/**
* Allow setting of an array of cURL options
*
* @param array $options
*/
public function setCurlOpts($options)
{
$this->curl_opts = $options + $this->curl_opts;
curl_setopt_array($this->curl, $options);
}
/**
* Get the URL of the API call
*
* @return string
*/
public function getUrl()
{
return $this->getCurlOpt(CURLOPT_URL);
}
/**
* Reset the cURL settings so the same API instance can be reused
*/
public function reset()
{
$this->initCurl();
}
}