Skip to content

Commit a7751d6

Browse files
feat: add model, endpoint and tests for REST API logs
1 parent 79340d8 commit a7751d6

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace RESTAPI\Endpoints;
4+
5+
require_once 'RESTAPI/autoloader.inc';
6+
7+
use RESTAPI\Core\Endpoint;
8+
9+
/**
10+
* Defines an Endpoint for interacting with many RESTAPILog Models at /api/v2/status/logs/packages/restapi.
11+
*/
12+
class StatusLogsPackagesRESTAPIEndpoint extends Endpoint {
13+
public function __construct() {
14+
# Set Endpoint attributes
15+
$this->url = '/api/v2/status/logs/packages/restapi';
16+
$this->model_name = 'RESTAPILog';
17+
$this->many = true;
18+
$this->request_method_options = ['GET'];
19+
20+
# Construct the parent Endpoint object
21+
parent::__construct();
22+
}
23+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace RESTAPI\Models;
4+
5+
use RESTAPI\Core\Model;
6+
use RESTAPI\Fields\StringField;
7+
use RESTAPI\ModelTraits\LogFileModelTraits;
8+
9+
/**
10+
* Defines a Model for interacting with the REST API log at /var/log/restapi.log.
11+
*/
12+
class RESTAPILog extends Model {
13+
use LogFileModelTraits;
14+
15+
/**
16+
* @var string $log_file The path to the base REST API log file this Model will read. Any log files that have
17+
* been rotated will be read as well.
18+
*/
19+
public string $log_file = '/var/log/restapi.log';
20+
public StringField $text;
21+
22+
public function __construct(mixed $id = null, mixed $parent_id = null, mixed $data = [], ...$options) {
23+
# Set model attributes
24+
$this->internal_callable = 'get_restapi_log';
25+
$this->many = true;
26+
27+
$this->text = new StringField(default: '', help_text: 'The raw text of the REST API log entry.');
28+
29+
parent::__construct($id, $parent_id, $data, ...$options);
30+
}
31+
32+
/**
33+
* Obtains the REST API log as an array. This method is the internal callable for this Model.
34+
* @return array The REST API log as an array of objects.
35+
*/
36+
protected function get_restapi_log(): array {
37+
return $this->read_log($this->log_file);
38+
}
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace RESTAPI\Tests;
4+
5+
use RESTAPI\Core\TestCase;
6+
use RESTAPI\Models\RESTAPILog;
7+
8+
class APIModelsRESTAPILogTestCase extends TestCase {
9+
/**
10+
* Checks if we can correctly read the restapi logs.
11+
*/
12+
public function test_read(): void {
13+
$dhcp_logs = RESTAPILog::read_all(limit: 10);
14+
foreach ($dhcp_logs->model_objects as $dhcp_log) {
15+
$this->assert_is_not_empty($dhcp_log->text->value);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)