Updated all response structures

This commit is contained in:
Eldad Fux
2020-08-08 13:41:17 +03:00
parent ba2393ac39
commit c8d08ac29e
10 changed files with 85 additions and 20 deletions
+2 -2
View File
@@ -1336,7 +1336,7 @@ $collections = [
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Code Path',
'key' => 'codePath',
'key' => 'path',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'default' => '',
'required' => false,
@@ -1345,7 +1345,7 @@ $collections = [
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Code Size',
'key' => 'codeSize',
'key' => 'size',
'type' => Database::SYSTEM_VAR_TYPE_NUMERIC,
'default' => '',
'required' => false,
+6 -6
View File
@@ -401,11 +401,11 @@ App::post('/v1/functions/:functionId/tags')
'read' => [],
'write' => [],
],
'dateCreated' => time(),
'functionId' => $function->getId(),
'dateCreated' => time(),
'command' => $command,
'codePath' => $path,
'codeSize' => $size,
'path' => $path,
'size' => $size,
]);
if (false === $tag) {
@@ -413,7 +413,7 @@ App::post('/v1/functions/:functionId/tags')
}
$usage
->setParam('storage', $tag->getAttribute('codeSize', 0))
->setParam('storage', $tag->getAttribute('size', 0))
;
$response->setStatusCode(Response::STATUS_CODE_CREATED);
@@ -518,7 +518,7 @@ App::delete('/v1/functions/:functionId/tags/:tagId')
$device = Storage::getDevice('functions');
if ($device->delete($tag->getAttribute('codePath', ''))) {
if ($device->delete($tag->getAttribute('path', ''))) {
if (!$projectDB->deleteDocument($tag->getId())) {
throw new Exception('Failed to remove tag from DB', 500);
}
@@ -535,7 +535,7 @@ App::delete('/v1/functions/:functionId/tags/:tagId')
}
$usage
->setParam('storage', $tag->getAttribute('codeSize', 0) * -1)
->setParam('storage', $tag->getAttribute('size', 0) * -1)
;
$response->noContent();
+1 -1
View File
@@ -332,7 +332,7 @@ App::get('/v1/projects/:projectId/usage')
) +
$projectDB->getCount(
[
'attribute' => 'codeSize',
'attribute' => 'size',
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_TAGS,
],
+1 -1
View File
@@ -112,7 +112,7 @@ $timeout = $this->getParam('timeout', 900);
<b data-ls-bind="{{tag.$id}}"></b> &nbsp;
<span class="text-fade" data-ls-bind="{{tag.command}}"></span>
<div class="text-size-small margin-top-small clear">
<span class="pull-start" data-ls-bind="Created {{tag.dateCreated|timeSince}} &nbsp; | &nbsp; {{tag.codeSize|humanFileSize}}"></span>
<span class="pull-start" data-ls-bind="Created {{tag.dateCreated|timeSince}} &nbsp; | &nbsp; {{tag.size|humanFileSize}}"></span>
<form data-ls-if="{{tag.$id}} !== {{project-function.tag}}" name="functions.deleteTag" class="pull-start"
data-analytics-event="submit"
+3 -3
View File
@@ -73,11 +73,11 @@ class DeletesV1
'functionId='.$document->getId(),
], $projectDB, function(Document $document) use ($device) {
if ($device->delete($document->getAttribute('codePath', ''))) {
Console::success('Delete code tag: '.$document->getAttribute('codePath', ''));
if ($device->delete($document->getAttribute('path', ''))) {
Console::success('Delete code tag: '.$document->getAttribute('path', ''));
}
else {
Console::error('Dailed to delete code tag: '.$document->getAttribute('codePath', ''));
Console::error('Dailed to delete code tag: '.$document->getAttribute('path', ''));
}
});
+2 -2
View File
@@ -266,14 +266,14 @@ class FunctionsV1
$value = "\t\t\t--env {$key}={$value} \\";
});
$tagPath = $tag->getAttribute('codePath', '');
$tagPath = $tag->getAttribute('path', '');
$tagPathTarget = '/tmp/project-'.$projectId.'/'.$tag->getId().'/code.tar.gz';
$tagPathTargetDir = \pathinfo($tagPathTarget, PATHINFO_DIRNAME);
$container = 'appwrite-function-'.$tag->getId();
$command = \escapeshellcmd($tag->getAttribute('command', ''));
if(!\is_readable($tagPath)) {
throw new Exception('Code is not readable: '.$tag->getAttribute('codePath', ''));
throw new Exception('Code is not readable: '.$tag->getAttribute('path', ''));
}
if (!\file_exists($tagPathTargetDir)) {
+5 -1
View File
@@ -15,6 +15,7 @@ use Appwrite\Utopia\Response\Model\Session;
use Appwrite\Utopia\Response\Model\Team;
use Appwrite\Utopia\Response\Model\Locale;
use Appwrite\Utopia\Response\Model\Membership;
use Appwrite\Utopia\Response\Model\Tag;
use Utopia\Response as UtopiaResponse;
class Response extends UtopiaResponse
@@ -85,6 +86,7 @@ class Response extends UtopiaResponse
->setModel(new Team())
->setModel(new Membership())
->setModel(new Func())
->setModel(new Tag())
;
parent::__construct($time);
@@ -150,13 +152,15 @@ class Response extends UtopiaResponse
$document->setAttribute($key, $rule['default']);
}
else {
var_dump($data);
throw new Exception('Missing response key: '.$key);
}
}
if($rule['array']) {
if(!is_array($data[$key])) {
throw new Exception($key.' must be an array of '.$rule['type'].' types');
var_dump($data);
throw new Exception($key.' must be an array of type '.$rule['type']);
}
foreach ($data[$key] as &$item) {
+3 -1
View File
@@ -55,7 +55,7 @@ class Func extends Model
->addRule('events', [
'type' => 'string',
'description' => 'Function trigger events.',
'default' => '',
'default' => [],
'example' => 'account.create',
'array' => true,
])
@@ -69,11 +69,13 @@ class Func extends Model
'type' => 'integer',
'description' => 'Function next scheduled execution date in Unix timestamp.',
'example' => 1592981292,
'default' => 0,
])
->addRule('previous', [
'type' => 'integer',
'description' => 'Function next scheduled execution date in Unix timestamp.',
'example' => 1592981237,
'default' => 0,
])
->addRule('timeout', [
'type' => 'integer',
@@ -0,0 +1,60 @@
<?php
namespace Appwrite\Utopia\Response\Model;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Model;
class Tag extends Model
{
public function __construct()
{
$this
->addRule('$id', [
'type' => 'string',
'description' => 'Tag ID.',
'example' => '5e5ea5c16897e',
])
->addRule('functionId', [
'type' => 'string',
'description' => 'Function ID.',
'example' => '5e5ea6g16897e',
])
->addRule('dateCreated', [
'type' => 'integer',
'description' => 'The tag creation date in Unix timestamp.',
'example' => 1592981250,
])
->addRule('command', [
'type' => 'string',
'description' => 'The entrypoint command in use to execute the tag code.',
'example' => 'enabled',
])
->addRule('size', [
'type' => 'string',
'description' => 'The code size in bytes.',
'example' => 'python-3.8',
])
;
}
/**
* Get Name
*
* @return string
*/
public function getName():string
{
return 'Tag';
}
/**
* Get Collection
*
* @return string
*/
public function getType():string
{
return Response::MODEL_TAG;
}
}
@@ -192,8 +192,7 @@ class FunctionsConsoleServerTest extends Scope
$this->assertNotEmpty($tag['body']['$id']);
$this->assertIsInt($tag['body']['dateCreated']);
$this->assertEquals('php function.php', $tag['body']['command']);
$this->assertStringStartsWith('/storage/functions/app-', $tag['body']['codePath']);
$this->assertEquals(751, $tag['body']['codeSize']);
$this->assertEquals(751, $tag['body']['size']);
/**
* Test for FAILURE
@@ -265,7 +264,7 @@ class FunctionsConsoleServerTest extends Scope
], $this->getHeaders()));
$this->assertEquals(200, $function['headers']['status-code']);
$this->assertEquals(751, $function['body']['codeSize']);
$this->assertEquals(751, $function['body']['size']);
/**
* Test for FAILURE