Skip to content

Commit

Permalink
Merge pull request #613 from Muhammad235/update/send-postman-result
Browse files Browse the repository at this point in the history
dix: send-postman-result
  • Loading branch information
timiajayi authored Aug 25, 2024
2 parents d1af577 + 289f686 commit 5a9c98f
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions send-postman-api-tests-results.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@
function processJsonAndSend($baseUrl)
{
$jsonPath = 'result.json';

if (!file_exists($jsonPath)) {
die("Error: result.json file not found at $jsonPath\n");
}

$jsonContent = file_get_contents($jsonPath);
if ($jsonContent === false) {
die("Error: Unable to read result.json file\n");
}

$data = json_decode($jsonContent, true);
if (json_last_error() !== JSON_ERROR_NONE) {
die("Error: Invalid JSON in result.json - " . json_last_error_msg() . "\n");
}

$lastChecked = date('Y-m-d H:M:S', filemtime($jsonPath));

$processedData = [];
foreach ($data['run']['executions'] as $execution) {
$apiGroup = $execution['item']['name'] ?? 'Unknown API';
$responseTime = $execution['response']['responseTime'] ?? null;
$status = determineStatus($execution);
$details = getDetails($execution);

$processedData[] = [
'api_group' => $apiGroup,
'status' => $status,
Expand All @@ -43,25 +43,25 @@ function processJsonAndSend($baseUrl)
'details' => $details,
];
}

// echo "Processed Data:\n";
// print_r($processedData);
$ch = curl_init($baseUrl . '/api/v1/api-status/update');

$ch = curl_init($baseUrl . '/api/v1/api-status');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($processedData));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if (curl_errno($ch)) {
echo "cURL Error: " . curl_error($ch) . "\n";
}

curl_close($ch);

echo "Status: $httpCode\n";
echo "Response: $response\n";
}
Expand All @@ -74,7 +74,7 @@ function determineStatus($execution)
{
$responseCode = $execution['response']['code'] ?? null;
$responseTime = $execution['response']['responseTime'] ?? null;

if ($responseCode === null || $responseCode >= 500) {
return 'Down';
} elseif ($responseTime > 400) {
Expand All @@ -88,7 +88,7 @@ function getDetails($execution)
{
$responseCode = $execution['response']['code'] ?? null;
$responseTime = $execution['response']['responseTime'] ?? null;

if ($responseCode === null || $responseCode >= 500) {
return 'API not responding (HTTP ' . ($responseCode ?? 'Unknown') . ')';
} elseif ($responseTime > 400) {
Expand All @@ -98,4 +98,4 @@ function getDetails($execution)
}
}

processJsonAndSend($baseUrl);
processJsonAndSend($baseUrl);

0 comments on commit 5a9c98f

Please sign in to comment.