1
0
Fork 0
mirror of synced 2024-06-01 10:29:48 +12:00

First draft for API routes

This commit is contained in:
Eldad Fux 2020-05-05 20:30:12 +03:00
parent 09e57b46ce
commit 2213c6622f
6 changed files with 454 additions and 103 deletions

View file

@ -1196,18 +1196,18 @@ $collections = [
'rules' => [
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Name',
'label' => 'Date Created',
'key' => 'dateCreated',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'type' => Database::SYSTEM_VAR_TYPE_NUMERIC,
'default' => 0,
'required' => false,
'array' => false,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Folder ID',
'key' => 'folderId',
'type' => Database::SYSTEM_VAR_TYPE_KEY,
'label' => 'Date Updated',
'key' => 'dateUpdated',
'type' => Database::SYSTEM_VAR_TYPE_NUMERIC,
'default' => 0,
'required' => false,
'array' => false,
@ -1218,113 +1218,95 @@ $collections = [
'key' => 'name',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'default' => '',
'required' => true,
'required' => false,
'array' => false,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Path',
'key' => 'path',
'label' => 'Tag',
'key' => 'tag',
'type' => Database::SYSTEM_VAR_TYPE_KEY,
'default' => '',
'required' => false,
'array' => false,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Vars',
'key' => 'vars',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'default' => '',
'required' => true,
'required' => false,
'array' => false,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Signature',
'key' => 'signature',
'label' => 'Trigger',
'key' => 'trigger',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'default' => '',
'required' => true,
'required' => false,
'array' => false,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Mime Type',
'key' => 'mimeType',
'label' => 'Events',
'key' => 'events',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'default' => '',
'required' => true,
'array' => false,
'required' => false,
'array' => true,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Original Size',
'key' => 'sizeOriginal',
'label' => 'Schedule',
'key' => 'schedule',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'default' => '',
'required' => false,
'array' => true,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Timeout',
'key' => 'timeout',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'default' => '',
'required' => false,
'array' => true,
],
],
],
Database::SYSTEM_COLLECTION_TAGS => [
'$collection' => Database::SYSTEM_COLLECTION_COLLECTIONS,
'$id' => Database::SYSTEM_COLLECTION_TAGS,
'$permissions' => ['read' => ['*']],
'name' => 'Functions',
'structure' => true,
'rules' => [
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Date Created',
'key' => 'dateCreated',
'type' => Database::SYSTEM_VAR_TYPE_NUMERIC,
'default' => 0,
'required' => true,
'required' => false,
'array' => false,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Actual Size',
'key' => 'sizeActual',
'type' => Database::SYSTEM_VAR_TYPE_NUMERIC,
'default' => 0,
'required' => true,
'array' => false,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Algorithm',
'key' => 'algorithm',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'default' => '',
'required' => true,
'array' => false,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Token',
'key' => 'token',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'default' => '',
'required' => true,
'array' => false,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'Comment',
'key' => 'comment',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'label' => 'Function ID',
'key' => 'functionId',
'type' => Database::SYSTEM_VAR_TYPE_KEY,
'default' => '',
'required' => false,
'array' => false,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'File OpenSSL Version',
'key' => 'fileOpenSSLVersion',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'default' => '',
'required' => false,
'array' => false,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'File OpenSSL Cipher',
'key' => 'fileOpenSSLCipher',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'default' => '',
'required' => false,
'array' => false,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'File OpenSSL Tag',
'key' => 'fileOpenSSLTag',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'default' => '',
'required' => false,
'array' => false,
],
[
'$collection' => Database::SYSTEM_COLLECTION_RULES,
'label' => 'File OpenSSL IV',
'key' => 'fileOpenSSLIV',
'type' => Database::SYSTEM_VAR_TYPE_TEXT,
'label' => 'Code',
'key' => 'code',
'type' => Database::SYSTEM_VAR_TYPE_KEY,
'default' => '',
'required' => false,
'array' => false,

View file

@ -3,9 +3,14 @@
global $utopia, $response, $projectDB;
use Appwrite\Database\Database;
use Appwrite\Database\Document;
use Appwrite\Database\Validator\UID;
use Appwrite\Task\Validator\Cron;
use Utopia\Response;
use Utopia\Validator\ArrayList;
use Utopia\Validator\Assoc;
use Utopia\Validator\Text;
use Utopia\Validator\Range;
use Utopia\Validator\WhiteList;
include_once __DIR__ . '/../shared/api.php';
@ -15,30 +20,395 @@ $utopia->post('/v1/functions')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'create')
->label('sdk.description', '/docs/references/users/create-function.md')
->label('sdk.description', '/docs/references/functions/create-function.md')
->param('name', '', function () { return new Text(128); }, 'Function name.')
->param('vars', '', function () { return new Assoc();}, 'Key-value JSON object.')
->param('trigger', '', function () { return new WhiteList(['event', 'scheudle']); }, 'Function trigger type.')
->param('events', null, function () { return new ArrayList(new Text(256)); }, 'Events list.')
->param('schedule', null, function () { return new Cron(); }, 'Schedule CRON syntax.')
->param('timeout', '', function () { return new Range(1, 10); }, 'Function maximum execution time in seconds.')
->action(
function ($name, $timeout) use ($response, $projectDB) {
$function = new Document([
'$collection' => Database::SYSTEM_COLLECTION_MEMBERSHIPS,
function ($name, $vars, $trigger, $events, $schedule, $timeout) use ($response, $projectDB) {
$function = $projectDB->createDocument([
'$collection' => Database::SYSTEM_COLLECTION_FUNCTIONS,
'$permissions' => [
'read' => [],
'write' => [],
],
'dateCreated' => time(),
'dateUpdated' => time(),
'name' => $name,
'tag' => '',
'vars' => $vars, // Should be encrypted
'trigger' => $trigger,
'events' => $events,
'schedule' => $schedule,
'timeout' => $timeout,
]);
// $response
// ->setStatusCode(Response::STATUS_CODE_CREATED)
// ->json(array_merge($user->getArrayCopy(array_merge([
// '$id',
// 'status',
// 'email',
// 'registration',
// 'emailVerification',
// 'name',
// ], $oauth2Keys)), ['roles' => []]));
if (false === $function) {
throw new Exception('Failed saving function to DB', 500);
}
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->json($function->getArrayCopy())
;
}
);
$utopia->get('/v1/functions')
->desc('List Functions')
->label('scope', 'functions.read')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'list')
->label('sdk.description', '/docs/references/functions/list-functions.md')
->param('search', '', function () { return new Text(256); }, 'Search term to filter your list results.', true)
->param('limit', 25, function () { return new Range(0, 100); }, 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, function () { return new Range(0, 2000); }, 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('orderType', 'ASC', function () { return new WhiteList(['ASC', 'DESC']); }, 'Order result by ASC or DESC order.', true)
->action(
function ($search, $limit, $offset, $orderType) use ($response, $projectDB) {
$results = $projectDB->getCollection([
'limit' => $limit,
'offset' => $offset,
'orderField' => 'dateCreated',
'orderType' => $orderType,
'orderCast' => 'int',
'search' => $search,
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_FUNCTIONS,
],
]);
$response->json(['sum' => $projectDB->getSum(), 'functions' => $results]);
}
);
$utopia->get('/v1/functions/:functionId')
->desc('Get Function')
->label('scope', 'functions.read')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'get')
->label('sdk.description', '/docs/references/functions/get-function.md')
->param('functionId', '', function () { return new UID(); }, 'Function unique ID.')
->action(
function ($functionId) use ($response, $projectDB) {
$function = $projectDB->getDocument($functionId);
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
throw new Exception('function not found', 404);
}
$response->json($function->getArrayCopy());
}
);
$utopia->put('/v1/functions/:functionId')
->desc('Update Function')
->label('scope', 'functions.write')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'update')
->label('sdk.description', '/docs/references/functions/update-function.md')
->param('functionId', '', function () { return new UID(); }, 'Function unique ID.')
->param('name', '', function () { return new Text(128); }, 'Function name.')
->param('vars', '', function () { return new Assoc();}, 'Key-value JSON object.')
->param('trigger', '', function () { return new WhiteList(['event', 'scheudle']); }, 'Function trigger type.')
->param('events', null, function () { return new ArrayList(new Text(256)); }, 'Events list.')
->param('schedule', null, function () { return new Cron(); }, 'Schedule CRON syntax.')
->param('timeout', '', function () { return new Range(1, 10); }, 'Function maximum execution time in seconds.')
->action(
function ($functionId, $name, $vars, $trigger, $events, $schedule, $timeout) use ($response, $projectDB) {
$function = $projectDB->getDocument($functionId);
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
throw new Exception('Function not found', 404);
}
$function = $projectDB->updateDocument(array_merge($function->getArrayCopy(), [
'dateUpdated' => time(),
'name' => $name,
'vars' => $vars, //TODO Should be encrypted
'trigger' => $trigger,
'events' => $events,
'schedule' => $schedule,
'timeout' => $timeout,
]));
if (false === $function) {
throw new Exception('Failed saving function to DB', 500);
}
$response->json($function->getArrayCopy());
}
);
$utopia->delete('/v1/functions/:functionId')
->desc('Delete Function')
->label('scope', 'functions.write')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'delete')
->label('sdk.description', '/docs/references/functions/delete-function.md')
->param('functionId', '', function () { return new UID(); }, 'Function unique ID.')
->action(
function ($functionId) use ($response, $projectDB, $webhook, $audit, $usage) {
$function = $projectDB->getDocument($functionId);
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
throw new Exception('Function not found', 404);
}
$response->noContent();
}
);
$utopia->post('/v1/functions/:functionId/tags')
->desc('Create Tag')
->label('scope', 'functions.write')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'createTag')
->label('sdk.description', '/docs/references/functions/create-tag.md')
->param('functionId', '', function () { return new UID(); }, 'Function unique ID.')
->param('code', '', function () { return new Text(128); }, 'Code package. Use the '.APP_NAME.' code packager to create a deployable package file.')
->action(
function ($functionId, $code) use ($response, $projectDB) {
$function = $projectDB->getDocument($functionId);
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
throw new Exception('Function not found', 404);
}
$tag = $projectDB->createDocument([
'$collection' => Database::SYSTEM_COLLECTION_TAGS,
'$permissions' => [
'read' => [],
'write' => [],
],
'dateCreated' => time(),
'functionId' => $function->getId(),
'code' => $code,
]);
if (false === $tag) {
throw new Exception('Failed saving tag to DB', 500);
}
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->json($tag->getArrayCopy())
;
}
);
$utopia->get('/v1/functions/:functionId/tags')
->desc('List Tags')
->label('scope', 'functions.read')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'listTags')
->label('sdk.description', '/docs/references/functions/list-tags.md')
->param('search', '', function () { return new Text(256); }, 'Search term to filter your list results.', true)
->param('limit', 25, function () { return new Range(0, 100); }, 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, function () { return new Range(0, 2000); }, 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('orderType', 'ASC', function () { return new WhiteList(['ASC', 'DESC']); }, 'Order result by ASC or DESC order.', true)
->action(
function ($functionId, $search, $limit, $offset, $orderType) use ($response, $projectDB) {
$function = $projectDB->getDocument($functionId);
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
throw new Exception('Function not found', 404);
}
$results = $projectDB->getCollection([
'limit' => $limit,
'offset' => $offset,
'orderField' => 'dateCreated',
'orderType' => $orderType,
'orderCast' => 'int',
'search' => $search,
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_TAGS,
'functionId='.$function->getId(),
],
]);
$response->json(['sum' => $projectDB->getSum(), 'tags' => $results]);
}
);
$utopia->get('/v1/functions/:functionId/tags/:tagId')
->desc('Get Tag')
->label('scope', 'functions.read')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'getTag')
->label('sdk.description', '/docs/references/functions/get-tag.md')
->param('functionId', '', function () { return new UID(); }, 'Function unique ID.')
->param('tagId', '', function () { return new UID(); }, 'Tag unique ID.')
->action(
function ($functionId, $tagId) use ($response, $projectDB) {
$function = $projectDB->getDocument($functionId);
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
throw new Exception('Function not found', 404);
}
$tag = $projectDB->getDocument($tagId);
if($tag->getAttribute('functionId') !== $function->getId()) {
throw new Exception('Tag not found', 404);
}
if (empty($tag->getId()) || Database::SYSTEM_COLLECTION_TAGS != $tag->getCollection()) {
throw new Exception('Tag not found', 404);
}
$response->json($tag->getArrayCopy());
}
);
$utopia->delete('/v1/functions/:functionId/tags/:tagId')
->desc('Delete Tag')
->label('scope', 'functions.write')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'deleteTag')
->label('sdk.description', '/docs/references/functions/delete-tag.md')
->param('functionId', '', function () { return new UID(); }, 'Function unique ID.')
->param('tagId', '', function () { return new UID(); }, 'Tag unique ID.')
->action(
function ($functionId, $tagId) use ($response, $projectDB) {
$function = $projectDB->getDocument($functionId);
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
throw new Exception('Function not found', 404);
}
$tag = $projectDB->getDocument($tagId);
if($tag->getAttribute('functionId') !== $function->getId()) {
throw new Exception('Tag not found', 404);
}
if (empty($tag->getId()) || Database::SYSTEM_COLLECTION_TAGS != $tag->getCollection()) {
throw new Exception('Tag not found', 404);
}
$response->noContent();
}
);
$utopia->post('/v1/functions/:functionId/executions')
->desc('Create Execution')
->label('scope', 'functions.write')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'createExecution')
->label('sdk.description', '/docs/references/functions/create-execution.md')
->param('functionId', '', function () { return new UID(); }, 'Function unique ID.')
->param('code', '', function () { return new Text(128); }, 'Code package. Use the '.APP_NAME.' code packager to create a deployable package file.')
->action(
function ($functionId, $code) use ($response, $projectDB) {
$function = $projectDB->getDocument($functionId);
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
throw new Exception('Function not found', 404);
}
$execution = $projectDB->createDocument([
'$collection' => Database::SYSTEM_COLLECTION_TAGS,
'$permissions' => [
'read' => [],
'write' => [],
],
'dateCreated' => time(),
'functionId' => $function->getId(),
'status' => 'waiting',
'exitCode' => 0,
'stdout' => '',
'stderr' => '',
'time' => 0,
]);
if (false === $execution) {
throw new Exception('Failed saving execution to DB', 500);
}
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->json($execution->getArrayCopy())
;
}
);
$utopia->get('/v1/functions/:functionId/executions')
->desc('List Executions')
->label('scope', 'functions.read')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'listExecutions')
->label('sdk.description', '/docs/references/functions/list-executions.md')
->param('search', '', function () { return new Text(256); }, 'Search term to filter your list results.', true)
->param('limit', 25, function () { return new Range(0, 100); }, 'Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.', true)
->param('offset', 0, function () { return new Range(0, 2000); }, 'Results offset. The default value is 0. Use this param to manage pagination.', true)
->param('orderType', 'ASC', function () { return new WhiteList(['ASC', 'DESC']); }, 'Order result by ASC or DESC order.', true)
->action(
function ($functionId, $search, $limit, $offset, $orderType) use ($response, $projectDB) {
$function = $projectDB->getDocument($functionId);
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
throw new Exception('Function not found', 404);
}
$results = $projectDB->getCollection([
'limit' => $limit,
'offset' => $offset,
'orderField' => 'dateCreated',
'orderType' => $orderType,
'orderCast' => 'int',
'search' => $search,
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_TAGS,
'functionId='.$function->getId(),
],
]);
$response->json(['sum' => $projectDB->getSum(), 'executions' => $results]);
}
);
$utopia->get('/v1/functions/:functionId/executions/:executionId')
->desc('Get Execution')
->label('scope', 'functions.read')
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'getExecution')
->label('sdk.description', '/docs/references/functions/get-execution.md')
->param('functionId', '', function () { return new UID(); }, 'Function unique ID.')
->param('executionId', '', function () { return new UID(); }, 'Execution unique ID.')
->action(
function ($functionId, $executionId) use ($response, $projectDB) {
$function = $projectDB->getDocument($functionId);
if (empty($function->getId()) || Database::SYSTEM_COLLECTION_FUNCTIONS != $function->getCollection()) {
throw new Exception('Function not found', 404);
}
$execution = $projectDB->getDocument($executionId);
if($execution->getAttribute('functionId') !== $function->getId()) {
throw new Exception('Execution not found', 404);
}
if (empty($execution->getId()) || Database::SYSTEM_COLLECTION_TAGS != $execution->getCollection()) {
throw new Exception('Execution not found', 404);
}
$response->json($execution->getArrayCopy());
}
);

View file

@ -432,7 +432,6 @@ $utopia->patch('/v1/users/:userId/prefs')
}
);
$utopia->delete('/v1/users/:userId/sessions/:sessionId')
->desc('Delete User Session')
->label('scope', 'users.write')

View file

@ -36,7 +36,7 @@
"utopia-php/abuse": "0.2.*",
"utopia-php/audit": "0.3.*",
"utopia-php/cache": "0.2.*",
"utopia-php/cli": "0.4.*",
"utopia-php/cli": "0.5.*",
"utopia-php/config": "0.2.*",
"utopia-php/locale": "0.2.*",
"utopia-php/registry": "0.2.*",

12
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "41b9cc9221e2531109d2b2547a5b4209",
"content-hash": "3439666d352f5f9429cd83e0daccc9dc",
"packages": [
{
"name": "appwrite/php-clamav",
@ -1458,16 +1458,16 @@
},
{
"name": "utopia-php/cli",
"version": "0.4.0",
"version": "0.5.0",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/cli.git",
"reference": "a3e893c49546fa7d8254a63ec6f919653d292af8"
"reference": "c66255fed87f00c5a8f7cc0c482cdb082260cd32"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/cli/zipball/a3e893c49546fa7d8254a63ec6f919653d292af8",
"reference": "a3e893c49546fa7d8254a63ec6f919653d292af8",
"url": "https://api.github.com/repos/utopia-php/cli/zipball/c66255fed87f00c5a8f7cc0c482cdb082260cd32",
"reference": "c66255fed87f00c5a8f7cc0c482cdb082260cd32",
"shasum": ""
},
"require": {
@ -1502,7 +1502,7 @@
"upf",
"utopia"
],
"time": "2020-03-25T04:46:43+00:00"
"time": "2020-05-05T09:02:10+00:00"
},
{
"name": "utopia-php/config",

View file

@ -37,7 +37,7 @@ class Database
// Functions
const SYSTEM_COLLECTION_FUNCTIONS = 'functions';
const SYSTEM_COLLECTION_PACKAGES = 'packages';
const SYSTEM_COLLECTION_TAGS = 'tags';
const SYSTEM_COLLECTION_EXECUTIONS = 'executions';
// Var Types