Skip to content

Commit

Permalink
Merge pull request #24 from cronitorio/1.0.5
Browse files Browse the repository at this point in the history
Fix bugs and compatibility issues
  • Loading branch information
aflanagan authored Mar 3, 2021
2 parents f0dddc7 + c0421d0 commit d74aa54
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.4
1.0.5
16 changes: 8 additions & 8 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Client
{
private const MONITOR_TYPES = ['job', 'event', 'synthetic'];
private const BASE_CONFIG_KEYS = ['apiKey', 'apiVersion', 'environment'];

public $config;
public $apiKey;
public $apiVersion;
Expand Down Expand Up @@ -46,9 +46,9 @@ public function readConfig($path = null, $output = false)
}
}

$this->apiKey = $conf['apiKey'] ?: $this->apiKey;
$this->apiVersion = $conf['apiVersion'] ?: $this->apiVersion;
$this->environment = $conf['environment'] ?: $this->environment;
$this->apiKey = isset($conf['apiKey']) ? $conf['apiKey'] : $this->apiKey;
$this->apiVersion = isset($conf['apiVersion']) ? $conf['apiVersion'] : $this->apiVersion;
$this->environment = isset($conf['environment']) ? $conf['environment'] : $this->environment;

if (!$output) {
return;
Expand All @@ -57,7 +57,7 @@ public function readConfig($path = null, $output = false)
$monitors = [];
foreach (self::MONITOR_TYPES as $t) {
$pluralType = $this->pluralizeType($t);
$toParse = $conf[$t] ?: $conf[$pluralType] ?: null;
$toParse = isset($conf[$t]) ? $conf[$t] : (isset($conf[$pluralType]) ? $conf[$pluralType] : null);
if (!$toParse) {
continue;
}
Expand All @@ -83,8 +83,8 @@ public function applyConfig($rollback = false)
try {
$conf = $this->readConfig(null, true);
$params = [
'monitors' => $conf['monitors'] ?: [],
'rollback' => $rollback
'monitors' => isset($conf['monitors']) ? $conf['monitors'] : [],
'rollback' => $rollback,
];
$monitors = $this->monitors->put($params);
echo count($monitors) . " monitors " . ($rollback ? 'validated' : 'synced to Cronitor');
Expand Down Expand Up @@ -115,7 +115,7 @@ public function job($key, $callback)
$monitor->ping([
'state' => 'fail',
'message' => $truncatedMessage,
'series' => $series
'series' => $series,
]);
throw $e;
}
Expand Down
17 changes: 9 additions & 8 deletions lib/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,37 @@ public function __construct($baseUrl, $apiKey, $apiVersion)
$this->apiVersion = $apiVersion;
}

public function get($path, $params = array())
public function get($path, $params = [])
{
return $this->request($path, 'GET', $params);
}

public function delete($path, $params = array())
public function delete($path, $params = [])
{
return $this->request($path, 'DELETE', $params);
}

public function put($path, $body = array(), $params = array())
public function put($path, $body = [], $params = [])
{
return $this->request($path, 'PUT', $params, $body);
}

private function request($path, $httpMethod, $params = array(), $body = null)
private function request($path, $httpMethod, $params = [], $body = null)
{
$options = array(
CURLOPT_HTTPHEADER => $this->headers(),
CURLOPT_CUSTOMREQUEST => $httpMethod,
CURLOPT_USERPWD => $this->apiKey . ":",
CURLOPT_TIMEOUT => $params['timeout'] ?: 5,
CURLOPT_TIMEOUT => isset($params['timeout']) ? $params['timeout'] : 5,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => 0
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => 0,
);

$url = $this->baseUrl . $path;
$ch = curl_init($url);
curl_setopt_array($ch, $options);

if (!is_null($body)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body, JSON_UNESCAPED_SLASHES));
}
Expand All @@ -54,7 +55,7 @@ private function request($path, $httpMethod, $params = array(), $body = null)
curl_close($ch);
return [
'code' => $code,
'content' => $content
'content' => $content,
];
}

Expand Down
21 changes: 11 additions & 10 deletions lib/Monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ private static function getMonitorHttpClient($apiKey, $apiVersion)

public static function put($apiKey, $apiVersion, $params = [])
{
$rollback = $params['rollback'] ?: false;
$rollback = isset($params['rollback']) ? $params['rollback'] : false;
unset($params['rollback']);
$monitors = $params['monitors'] ?: [$params];
$monitors = isset($params['monitors']) ? $params['monitors'] : [$params];

$client = self::getMonitorHttpClient($apiKey, $apiVersion);
$response = $client->put('', [
Expand All @@ -48,7 +48,8 @@ public static function put($apiKey, $apiVersion, $params = [])
$out = [];
$data = json_decode($response['content'], true);

foreach ($data['monitors'] ?: [] as &$md) {
$dataMonitors = isset($data['monitors']) ? $data['monitors'] : [];
foreach ($dataMonitors as &$md) {
$m = new Monitor($md['key']);
$m->data = $md;
array_push($out, $m);
Expand All @@ -66,7 +67,7 @@ public static function delete($apiKey, $apiVersion, $key)
{
$client = self::getMonitorHttpClient($apiKey, $apiVersion);
$response = $client->delete("/$key", ['timeout' => 10]);

if ($response['code'] != 204) {
\error_log("Error deleting monitor: $key", 0);
return false;
Expand Down Expand Up @@ -114,7 +115,7 @@ public function pause($hours = null)
if ($hours) {
$path .= "/$hours";
}

$response = $this->monitorClient->get($path, ['timeout' => 5]);
return $response['code'] >= 200 && $response['code'] <= 299;
}
Expand All @@ -134,7 +135,7 @@ public function getData()
\error_log('No API key detected. Initialize CronitorClient with a valid API key.', 0);
return null;
}

$response = $this->monitorClient->get('', ['timeout' => 10]);
$this->data = json_decode($response['content']);
return $this->data;
Expand All @@ -157,8 +158,8 @@ private function cleanParams($params)
'state' => isset($params['state']) ? $params['state'] : null,
'message' => isset($params['message']) ? $params['message'] : null,
'series' => isset($params['series']) ? $params['series'] : null,
'host' => isset($params['host']) ? $params['host'] : null, // todo: params.fetch(:host, Socket.gethostname),
'metric' => $params['metrics'] ? $this->cleanMetrics($params['metrics']) : null,
'host' => isset($params['host']) ? $params['host'] : gethostname(),
'metric' => isset($params['metrics']) ? $this->cleanMetrics($params['metrics']) : null,
'stamp' => microtime(true),
'env' => isset($params['env']) ? $params['env'] : null,
];
Expand Down Expand Up @@ -196,9 +197,9 @@ private function getFallbackPingApiUrl()
private function buildPingQuery($params)
{
$cleanParams = $this->cleanParams($params);
$metrics = $cleanParams['metric'];
$metrics = isset($cleanParams['metric']) ? $cleanParams['metric'] : null;
unset($cleanParams['metric']);

$queryParams = array_map(function ($key) use ($cleanParams) {
$value = $cleanParams[$key];
return "$key=$value";
Expand Down

0 comments on commit d74aa54

Please sign in to comment.