feat: ping endpoint

This commit is contained in:
loks0n
2024-10-04 23:23:18 +01:00
parent 36d110b809
commit ac5bc22817
4 changed files with 135 additions and 0 deletions
+22
View File
@@ -4541,6 +4541,28 @@ $consoleCollections = array_merge([
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('pingCount'),
'type' => Database::VAR_INTEGER,
'format' => '',
'size' => 0,
'signed' => false,
'required' => false,
'default' => 0,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('pingedAt'),
'type' => Database::VAR_DATETIME,
'format' => '',
'size' => 0,
'signed' => false,
'required' => false,
'default' => null,
'array' => false,
'filters' => ['datetime'],
]
],
'indexes' => [
[
+46
View File
@@ -25,10 +25,12 @@ use Utopia\App;
use Utopia\CLI\Console;
use Utopia\Config\Config;
use Utopia\Database\Database;
use Utopia\Database\DateTime;
use Utopia\Database\Document;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
use Utopia\Database\Validator\UID;
use Utopia\Domains\Domain;
use Utopia\DSN\DSN;
use Utopia\Locale\Locale;
@@ -1048,6 +1050,50 @@ App::get('/.well-known/acme-challenge/*')
include_once __DIR__ . '/shared/api.php';
include_once __DIR__ . '/shared/api/auth.php';
App::get('/v1/ping')
->groups(['api', 'general'])
->desc('Test the connection between the Appwrite and the SDK.')
->label('scope', 'public')
->label('event', 'projects.[projectId].ping')
->param('projectId', '', new UID(), 'Project unique ID.')
->inject('response')
->inject('console')
->inject('dbForConsole')
->inject('queueForEvents')
->action(function (string $projectId, Response $response, Document $project, Database $dbForConsole, Event $queueForEvents) {
if (empty($projectId) || $projectId === 'console') {
throw new AppwriteException(AppwriteException::PROJECT_NOT_FOUND);
}
Console::log('Ping' . json_encode(['projectId' => $projectId], JSON_PRETTY_PRINT));
$project = Authorization::skip(function () use ($dbForConsole, $projectId) {
return $dbForConsole->getDocument('projects', $projectId);
});
if ($project->isEmpty()) {
Console::log('Ping' . json_encode($project, JSON_PRETTY_PRINT));
throw new AppwriteException(AppwriteException::PROJECT_NOT_FOUND);
}
$pingCount = $project->getAttribute('pingCount', 0) + 1;
$pingedAt = DateTime::now();
$project
->setAttribute('pingCount', $pingCount)
->setAttribute('pingedAt', $pingedAt);
Authorization::skip(function () use ($dbForConsole, $project) {
$dbForConsole->updateDocument('projects', $project->getId(), $project);
});
$queueForEvents
->setParam('projectId', $projectId)
->setPayload($response->output($project, Response::MODEL_PROJECT));
$response->text('Pong!');
});
App::wildcard()
->groups(['api'])
->label('scope', 'global')
@@ -234,6 +234,18 @@ class Project extends Model
'default' => '',
'example' => 'tls',
])
->addRule('pingCount', [
'type' => self::TYPE_INTEGER,
'description' => 'Number of times the ping was received for this project.',
'default' => 0,
'example' => 1,
])
->addRule('pingedAt', [
'type' => self::TYPE_DATETIME,
'description' => 'Last ping datetime in ISO 8601 format.',
'default' => '',
'example' => self::TYPE_DATETIME_EXAMPLE,
])
;
$services = Config::getParam('services', []);
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace Tests\E2E\General;
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideClient;
class PingTest extends Scope
{
use ProjectCustom;
use SideClient;
public function testPing()
{
/**
* Test for SUCCESS
*/
// Without user session
$response = $this->client->call(Client::METHOD_GET, '/ping', [], [
'projectId' => $this->getProject()['$id'],
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('Pong!', $response['body']);
// With user session
$response = $this->client->call(Client::METHOD_GET, '/ping', $this->getHeaders(), [
'projectId' => $this->getProject()['$id'],
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('Pong!', $response['body']);
// With API key
$response = $this->client->call(Client::METHOD_GET, '/ping', [
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'projectId' => $this->getProject()['$id'],
]);
/**
* Test for FAILURE
*/
// Fake project ID
$response = $this->client->call(Client::METHOD_GET, '/ping', \array_merge([
'origin' => 'http://localhost',
]), [
'projectId' => 'fake-project-id',
]);
$this->assertEquals(404, $response['headers']['status-code']);
$this->assertNotContains('Pong!', $response['body']);
}
}