2020-05-05 02:35:01 +12:00
< ? php
use Appwrite\Database\Database ;
2020-10-31 08:53:27 +13:00
use Appwrite\Database\Document ;
2020-12-30 12:00:44 +13:00
use Appwrite\Database\Validator\Authorization ;
2020-05-06 05:30:12 +12:00
use Appwrite\Database\Validator\UID ;
2021-01-22 21:28:33 +13:00
use Utopia\Storage\Storage ;
use Utopia\Storage\Validator\File ;
2021-01-28 02:15:44 +13:00
use Utopia\Storage\Validator\FileExt ;
2021-01-22 21:28:33 +13:00
use Utopia\Storage\Validator\FileSize ;
use Utopia\Storage\Validator\Upload ;
2020-10-31 08:53:27 +13:00
use Appwrite\Utopia\Response ;
2020-05-06 05:30:12 +12:00
use Appwrite\Task\Validator\Cron ;
2020-07-13 09:18:52 +12:00
use Utopia\App ;
2020-05-06 05:30:12 +12:00
use Utopia\Validator\ArrayList ;
use Utopia\Validator\Assoc ;
2020-05-05 02:35:01 +12:00
use Utopia\Validator\Text ;
use Utopia\Validator\Range ;
2020-05-06 05:30:12 +12:00
use Utopia\Validator\WhiteList ;
2020-07-15 04:13:18 +12:00
use Utopia\Config\Config ;
2020-05-13 10:00:48 +12:00
use Cron\CronExpression ;
2020-12-30 12:00:44 +13:00
use Utopia\Exception ;
2020-05-05 02:35:01 +12:00
include_once __DIR__ . '/../shared/api.php' ;
2020-07-13 09:18:52 +12:00
App :: post ( '/v1/functions' )
-> groups ([ 'api' , 'functions' ])
2020-05-05 02:35:01 +12:00
-> desc ( 'Create Function' )
-> label ( 'scope' , 'functions.write' )
-> label ( 'sdk.platform' , [ APP_PLATFORM_SERVER ])
-> label ( 'sdk.namespace' , 'functions' )
-> label ( 'sdk.method' , 'create' )
2020-05-06 05:30:12 +12:00
-> label ( 'sdk.description' , '/docs/references/functions/create-function.md' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_FUNCTION )
2020-09-11 02:40:14 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Function name. Max length: 128 chars.' )
2020-12-30 12:00:44 +13:00
-> param ( 'execute' , [], new ArrayList ( new Text ( 64 )), 'An array of strings with execution permissions. By default no user is granted with any execute permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.' )
2020-09-11 02:40:14 +12:00
-> param ( 'env' , '' , new WhiteList ( array_keys ( Config :: getParam ( 'environments' )), true ), 'Execution enviornment.' )
-> param ( 'vars' , [], new Assoc (), 'Key-value JSON object.' , true )
-> param ( 'events' , [], new ArrayList ( new WhiteList ( array_keys ( Config :: getParam ( 'events' )), true )), 'Events list.' , true )
-> param ( 'schedule' , '' , new Cron (), 'Schedule CRON syntax.' , true )
-> param ( 'timeout' , 15 , new Range ( 1 , 900 ), 'Function maximum execution time in seconds.' , true )
2020-12-27 04:20:08 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
2020-12-30 12:00:44 +13:00
-> action ( function ( $name , $execute , $env , $vars , $events , $schedule , $timeout , $response , $projectDB ) {
2021-01-17 12:38:13 +13:00
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
2020-07-13 09:18:52 +12:00
$function = $projectDB -> createDocument ([
'$collection' => Database :: SYSTEM_COLLECTION_FUNCTIONS ,
'$permissions' => [
2020-12-30 12:00:44 +13:00
'execute' => $execute ,
2020-07-13 09:18:52 +12:00
],
'dateCreated' => time (),
'dateUpdated' => time (),
2020-10-31 08:53:27 +13:00
'status' => 'disabled' ,
2020-07-13 09:18:52 +12:00
'name' => $name ,
2020-07-15 04:13:18 +12:00
'env' => $env ,
2020-07-13 09:18:52 +12:00
'tag' => '' ,
'vars' => $vars ,
'events' => $events ,
'schedule' => $schedule ,
2020-10-31 08:53:27 +13:00
'schedulePrevious' => null ,
'scheduleNext' => null ,
2020-07-13 09:18:52 +12:00
'timeout' => $timeout ,
]);
if ( false === $function ) {
throw new Exception ( 'Failed saving function to DB' , 500 );
2020-05-06 05:30:12 +12:00
}
2020-10-31 21:42:41 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $function , Response :: MODEL_FUNCTION )
;
2020-12-27 04:20:08 +13:00
});
2020-07-13 09:18:52 +12:00
App :: get ( '/v1/functions' )
-> groups ([ 'api' , 'functions' ])
2020-05-06 05:30:12 +12:00
-> 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' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_FUNCTION_LIST )
2020-09-11 02:40:14 +12:00
-> param ( 'search' , '' , new Text ( 256 ), 'Search term to filter your list results. Max length: 256 chars.' , true )
-> param ( 'limit' , 25 , 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 , new Range ( 0 , 2000 ), 'Results offset. The default value is 0. Use this param to manage pagination.' , true )
-> param ( 'orderType' , 'ASC' , new WhiteList ([ 'ASC' , 'DESC' ], true ), 'Order result by ASC or DESC order.' , true )
2020-12-27 04:20:08 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
2020-07-13 09:18:52 +12:00
-> action ( function ( $search , $limit , $offset , $orderType , $response , $projectDB ) {
2021-01-17 12:38:13 +13:00
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
2020-07-13 09:18:52 +12:00
$results = $projectDB -> getCollection ([
'limit' => $limit ,
'offset' => $offset ,
2020-12-30 08:30:49 +13:00
'orderType' => $orderType ,
2020-07-13 09:18:52 +12:00
'search' => $search ,
'filters' => [
'$collection=' . Database :: SYSTEM_COLLECTION_FUNCTIONS ,
],
]);
2020-10-31 08:53:27 +13:00
$response -> dynamic ( new Document ([
'sum' => $projectDB -> getSum (),
'functions' => $results
]), Response :: MODEL_FUNCTION_LIST );
2020-12-27 04:20:08 +13:00
});
2020-07-13 09:18:52 +12:00
App :: get ( '/v1/functions/:functionId' )
-> groups ([ 'api' , 'functions' ])
2020-05-06 05:30:12 +12:00
-> 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' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_FUNCTION )
2020-09-11 02:40:14 +12:00
-> param ( 'functionId' , '' , new UID (), 'Function unique ID.' )
2020-12-27 04:20:08 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
2020-07-13 09:18:52 +12:00
-> action ( function ( $functionId , $response , $projectDB ) {
2021-01-17 12:38:13 +13:00
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
2020-07-13 09:18:52 +12:00
$function = $projectDB -> getDocument ( $functionId );
2020-05-06 05:30:12 +12:00
2020-07-13 09:18:52 +12:00
if ( empty ( $function -> getId ()) || Database :: SYSTEM_COLLECTION_FUNCTIONS != $function -> getCollection ()) {
2020-10-31 08:53:27 +13:00
throw new Exception ( 'Function not found' , 404 );
2020-05-06 05:30:12 +12:00
}
2020-10-31 08:53:27 +13:00
$response -> dynamic ( $function , Response :: MODEL_FUNCTION );
2020-12-27 04:20:08 +13:00
});
2020-07-13 09:18:52 +12:00
2020-10-31 08:53:27 +13:00
App :: get ( '/v1/functions/:functionId/usage' )
-> desc ( 'Get Function Usage' )
-> groups ([ 'api' , 'functions' ])
-> label ( 'scope' , 'functions.read' )
-> label ( 'sdk.platform' , [ APP_PLATFORM_CONSOLE ])
-> label ( 'sdk.namespace' , 'functions' )
-> label ( 'sdk.method' , 'getUsage' )
-> param ( 'functionId' , '' , new UID (), 'Function unique ID.' )
-> param ( 'range' , '30d' , new WhiteList ([ '24h' , '7d' , '30d' , '90d' ]), 'Date range.' , true )
2020-12-27 04:20:08 +13:00
-> inject ( 'response' )
-> inject ( 'project' )
-> inject ( 'projectDB' )
-> inject ( 'register' )
2020-10-31 08:53:27 +13:00
-> action ( function ( $functionId , $range , $response , $project , $projectDB , $register ) {
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Document $project */
/** @var Appwrite\Database\Database $consoleDB */
/** @var Appwrite\Database\Database $projectDB */
/** @var Utopia\Registry\Registry $register */
$function = $projectDB -> getDocument ( $functionId );
if ( empty ( $function -> getId ()) || Database :: SYSTEM_COLLECTION_FUNCTIONS != $function -> getCollection ()) {
throw new Exception ( 'Function not found' , 404 );
}
2021-01-13 19:25:43 +13:00
2021-01-15 18:30:49 +13:00
if ( App :: getEnv ( '_APP_USAGE_STATS' , 'enabled' ) == 'enabled' ) {
2021-01-13 14:10:52 +13:00
$period = [
'24h' => [
'start' => DateTime :: createFromFormat ( 'U' , \strtotime ( '-24 hours' )),
'end' => DateTime :: createFromFormat ( 'U' , \strtotime ( '+1 hour' )),
'group' => '30m' ,
],
'7d' => [
'start' => DateTime :: createFromFormat ( 'U' , \strtotime ( '-7 days' )),
'end' => DateTime :: createFromFormat ( 'U' , \strtotime ( 'now' )),
'group' => '1d' ,
],
'30d' => [
'start' => DateTime :: createFromFormat ( 'U' , \strtotime ( '-30 days' )),
'end' => DateTime :: createFromFormat ( 'U' , \strtotime ( 'now' )),
'group' => '1d' ,
],
'90d' => [
'start' => DateTime :: createFromFormat ( 'U' , \strtotime ( '-90 days' )),
'end' => DateTime :: createFromFormat ( 'U' , \strtotime ( 'now' )),
'group' => '1d' ,
],
];
$client = $register -> get ( 'influxdb' );
$executions = [];
$failures = [];
$compute = [];
if ( $client ) {
$start = $period [ $range ][ 'start' ] -> format ( DateTime :: RFC3339 );
$end = $period [ $range ][ 'end' ] -> format ( DateTime :: RFC3339 );
$database = $client -> selectDB ( 'telegraf' );
// Executions
$result = $database -> query ( 'SELECT sum(value) AS "value" FROM "appwrite_usage_executions_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "project"=\'' . $project -> getId () . '\' AND "functionId"=\'' . $function -> getId () . '\' GROUP BY time(' . $period [ $range ][ 'group' ] . ') FILL(null)' );
$points = $result -> getPoints ();
foreach ( $points as $point ) {
$executions [] = [
'value' => ( ! empty ( $point [ 'value' ])) ? $point [ 'value' ] : 0 ,
'date' => \strtotime ( $point [ 'time' ]),
];
}
// Failures
$result = $database -> query ( 'SELECT sum(value) AS "value" FROM "appwrite_usage_executions_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "project"=\'' . $project -> getId () . '\' AND "functionId"=\'' . $function -> getId () . '\' AND "functionStatus"=\'failed\' GROUP BY time(' . $period [ $range ][ 'group' ] . ') FILL(null)' );
$points = $result -> getPoints ();
foreach ( $points as $point ) {
$failures [] = [
'value' => ( ! empty ( $point [ 'value' ])) ? $point [ 'value' ] : 0 ,
'date' => \strtotime ( $point [ 'time' ]),
];
}
// Compute
$result = $database -> query ( 'SELECT sum(value) AS "value" FROM "appwrite_usage_executions_time" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "project"=\'' . $project -> getId () . '\' AND "functionId"=\'' . $function -> getId () . '\' GROUP BY time(' . $period [ $range ][ 'group' ] . ') FILL(null)' );
$points = $result -> getPoints ();
foreach ( $points as $point ) {
$compute [] = [
'value' => round (( ! empty ( $point [ 'value' ])) ? $point [ 'value' ] / 1000 : 0 , 2 ), // minutes
'date' => \strtotime ( $point [ 'time' ]),
];
}
2020-10-31 08:53:27 +13:00
}
2021-01-13 14:10:52 +13:00
$response -> json ([
'range' => $range ,
'executions' => [
'data' => $executions ,
'total' => \array_sum ( \array_map ( function ( $item ) {
return $item [ 'value' ];
}, $executions )),
],
'failures' => [
'data' => $failures ,
'total' => \array_sum ( \array_map ( function ( $item ) {
return $item [ 'value' ];
}, $failures )),
],
'compute' => [
'data' => $compute ,
'total' => \array_sum ( \array_map ( function ( $item ) {
return $item [ 'value' ];
}, $compute )),
],
]);
} else {
$response -> json ([]);
2020-10-31 08:53:27 +13:00
}
2020-12-27 04:20:08 +13:00
});
2020-10-31 08:53:27 +13:00
2020-07-13 09:18:52 +12:00
App :: put ( '/v1/functions/:functionId' )
-> groups ([ 'api' , 'functions' ])
2020-05-06 05:30:12 +12:00
-> 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' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_FUNCTION )
2020-09-11 02:40:14 +12:00
-> param ( 'functionId' , '' , new UID (), 'Function unique ID.' )
-> param ( 'name' , '' , new Text ( 128 ), 'Function name. Max length: 128 chars.' )
2020-12-31 01:25:32 +13:00
-> param ( 'execute' , [], new ArrayList ( new Text ( 64 )), 'An array of strings with execution permissions. By default no user is granted with any execute permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.' )
2020-09-11 02:40:14 +12:00
-> param ( 'vars' , [], new Assoc (), 'Key-value JSON object.' , true )
-> param ( 'events' , [], new ArrayList ( new WhiteList ( array_keys ( Config :: getParam ( 'events' )), true )), 'Events list.' , true )
-> param ( 'schedule' , '' , new Cron (), 'Schedule CRON syntax.' , true )
-> param ( 'timeout' , 15 , new Range ( 1 , 900 ), 'Function maximum execution time in seconds.' , true )
2020-12-27 04:20:08 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
2021-01-17 12:38:13 +13:00
-> inject ( 'project' )
-> action ( function ( $functionId , $name , $execute , $vars , $events , $schedule , $timeout , $response , $projectDB , $project ) {
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
/** @var Appwrite\Database\Document $project */
2020-07-13 09:18:52 +12:00
$function = $projectDB -> getDocument ( $functionId );
if ( empty ( $function -> getId ()) || Database :: SYSTEM_COLLECTION_FUNCTIONS != $function -> getCollection ()) {
throw new Exception ( 'Function not found' , 404 );
}
2021-01-17 13:07:43 +13:00
$original = $function -> getAttribute ( 'schedule' , '' );
2021-02-22 10:37:22 +13:00
$cron = ( ! empty ( $function -> getAttribute ( 'tag' , null )) && ! empty ( $schedule )) ? new CronExpression ( $schedule ) : null ;
2020-07-13 09:18:52 +12:00
$next = ( ! empty ( $function -> getAttribute ( 'tag' , null )) && ! empty ( $schedule )) ? $cron -> getNextRunDate () -> format ( 'U' ) : null ;
$function = $projectDB -> updateDocument ( array_merge ( $function -> getArrayCopy (), [
2020-12-31 01:25:32 +13:00
'$permissions' => [
'execute' => $execute ,
],
2020-07-13 09:18:52 +12:00
'dateUpdated' => time (),
'name' => $name ,
'vars' => $vars ,
'events' => $events ,
'schedule' => $schedule ,
2020-10-31 08:53:27 +13:00
'scheduleNext' => $next ,
2021-01-17 12:38:13 +13:00
'timeout' => $timeout ,
2020-07-13 09:18:52 +12:00
]));
if ( false === $function ) {
throw new Exception ( 'Failed saving function to DB' , 500 );
2020-05-06 05:30:12 +12:00
}
2021-01-17 13:07:43 +13:00
if ( $next && $schedule !== $original ) {
2021-01-17 12:38:13 +13:00
ResqueScheduler :: enqueueAt ( $next , 'v1-functions' , 'FunctionsV1' , [
'projectId' => $project -> getId (),
'functionId' => $function -> getId (),
'executionId' => null ,
'trigger' => 'schedule' ,
]); // Async task rescheduale
}
2020-10-31 08:53:27 +13:00
$response -> dynamic ( $function , Response :: MODEL_FUNCTION );
2020-12-27 04:20:08 +13:00
});
2020-07-13 09:18:52 +12:00
App :: patch ( '/v1/functions/:functionId/tag' )
-> groups ([ 'api' , 'functions' ])
2020-05-11 16:39:00 +12:00
-> desc ( 'Update Function Tag' )
2020-05-07 05:35:56 +12:00
-> label ( 'scope' , 'functions.write' )
-> label ( 'sdk.platform' , [ APP_PLATFORM_SERVER ])
-> label ( 'sdk.namespace' , 'functions' )
2020-05-11 08:44:28 +12:00
-> label ( 'sdk.method' , 'updateTag' )
2020-12-31 21:25:11 +13:00
-> label ( 'sdk.description' , '/docs/references/functions/update-function-tag.md' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_FUNCTION )
2020-09-11 02:40:14 +12:00
-> param ( 'functionId' , '' , new UID (), 'Function unique ID.' )
-> param ( 'tag' , '' , new UID (), 'Tag unique ID.' )
2020-12-27 04:20:08 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
2021-01-17 12:38:13 +13:00
-> inject ( 'project' )
-> action ( function ( $functionId , $tag , $response , $projectDB , $project ) {
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
/** @var Appwrite\Database\Document $project */
2020-07-13 09:18:52 +12:00
$function = $projectDB -> getDocument ( $functionId );
2020-07-16 07:09:33 +12:00
$tag = $projectDB -> getDocument ( $tag );
2020-05-07 05:35:56 +12:00
2020-07-13 09:18:52 +12:00
if ( empty ( $function -> getId ()) || Database :: SYSTEM_COLLECTION_FUNCTIONS != $function -> getCollection ()) {
throw new Exception ( 'Function not found' , 404 );
}
2020-05-13 10:00:48 +12:00
2020-07-16 07:09:33 +12:00
if ( empty ( $tag -> getId ()) || Database :: SYSTEM_COLLECTION_TAGS != $tag -> getCollection ()) {
throw new Exception ( 'Tag not found' , 404 );
}
2020-07-13 09:18:52 +12:00
$schedule = $function -> getAttribute ( 'schedule' , '' );
2021-02-22 10:37:22 +13:00
$cron = ( empty ( $function -> getAttribute ( 'tag' )) && ! empty ( $schedule )) ? new CronExpression ( $schedule ) : null ;
2021-01-17 13:07:43 +13:00
$next = ( empty ( $function -> getAttribute ( 'tag' )) && ! empty ( $schedule )) ? $cron -> getNextRunDate () -> format ( 'U' ) : null ;
2020-05-07 05:35:56 +12:00
2020-07-13 09:18:52 +12:00
$function = $projectDB -> updateDocument ( array_merge ( $function -> getArrayCopy (), [
2020-07-16 07:09:33 +12:00
'tag' => $tag -> getId (),
2020-10-31 08:53:27 +13:00
'scheduleNext' => $next ,
2020-07-13 09:18:52 +12:00
]));
2020-05-07 05:35:56 +12:00
2021-01-17 12:38:13 +13:00
if ( $next ) { // Init first schedule
ResqueScheduler :: enqueueAt ( $next , 'v1-functions' , 'FunctionsV1' , [
'projectId' => $project -> getId (),
'functionId' => $function -> getId (),
'executionId' => null ,
'trigger' => 'schedule' ,
]); // Async task rescheduale
}
2020-07-13 09:18:52 +12:00
if ( false === $function ) {
throw new Exception ( 'Failed saving function to DB' , 500 );
2020-05-07 05:35:56 +12:00
}
2020-10-31 08:53:27 +13:00
$response -> dynamic ( $function , Response :: MODEL_FUNCTION );
2020-12-27 04:20:08 +13:00
});
2020-07-13 09:18:52 +12:00
App :: delete ( '/v1/functions/:functionId' )
-> groups ([ 'api' , 'functions' ])
2020-05-06 05:30:12 +12:00
-> 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' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2020-09-11 02:40:14 +12:00
-> param ( 'functionId' , '' , new UID (), 'Function unique ID.' )
2020-12-27 04:20:08 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
-> inject ( 'deletes' )
2020-10-31 08:53:27 +13:00
-> action ( function ( $functionId , $response , $projectDB , $deletes ) {
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
/** @var Appwrite\Event\Event $deletes */
2020-07-13 09:18:52 +12:00
$function = $projectDB -> getDocument ( $functionId );
2020-05-06 05:30:12 +12:00
2020-07-13 09:18:52 +12:00
if ( empty ( $function -> getId ()) || Database :: SYSTEM_COLLECTION_FUNCTIONS != $function -> getCollection ()) {
throw new Exception ( 'Function not found' , 404 );
}
2020-05-07 00:12:52 +12:00
2020-07-13 09:18:52 +12:00
if ( ! $projectDB -> deleteDocument ( $function -> getId ())) {
throw new Exception ( 'Failed to remove function from DB' , 500 );
2020-05-05 02:35:01 +12:00
}
2020-05-06 05:30:12 +12:00
2020-10-31 08:53:27 +13:00
$deletes
2020-12-19 03:05:15 +13:00
-> setParam ( 'type' , DELETE_TYPE_DOCUMENT )
2020-10-31 08:53:27 +13:00
-> setParam ( 'document' , $function -> getArrayCopy ())
;
2021-03-19 09:32:16 +13:00
$response -> dynamic ( new Document (), Response :: MODEL_NONE );
2020-12-27 04:20:08 +13:00
});
2020-07-13 09:18:52 +12:00
App :: post ( '/v1/functions/:functionId/tags' )
-> groups ([ 'api' , 'functions' ])
2020-05-06 05:30:12 +12:00
-> 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' )
2021-01-30 03:29:53 +13:00
-> label ( 'sdk.packaging' , true )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.request.type' , 'multipart/form-data' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TAG )
2020-09-11 02:40:14 +12:00
-> param ( 'functionId' , '' , new UID (), 'Function unique ID.' )
-> param ( 'command' , '' , new Text ( '1028' ), 'Code execution command.' )
2021-02-01 08:11:03 +13:00
-> param ( 'code' , null , new File (), 'Gzip file with your code package. When used with the Appwrite CLI, pass the path to your code directory, and the CLI will automatically package your code. Use a path that is within the current directory.' , false )
2020-12-27 04:20:08 +13:00
-> inject ( 'request' )
-> inject ( 'response' )
-> inject ( 'projectDB' )
-> inject ( 'usage' )
2021-01-30 00:37:00 +13:00
-> action ( function ( $functionId , $command , $file , $request , $response , $projectDB , $usage ) {
2021-01-17 12:38:13 +13:00
/** @var Utopia\Swoole\Request $request */
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
/** @var Appwrite\Event\Event $usage */
2020-07-13 09:18:52 +12:00
$function = $projectDB -> getDocument ( $functionId );
if ( empty ( $function -> getId ()) || Database :: SYSTEM_COLLECTION_FUNCTIONS != $function -> getCollection ()) {
throw new Exception ( 'Function not found' , 404 );
}
2020-07-15 08:33:52 +12:00
2021-02-01 08:11:03 +13:00
$file = $request -> getFiles ( 'code' );
2020-07-15 09:20:46 +12:00
$device = Storage :: getDevice ( 'functions' );
2021-01-28 02:15:44 +13:00
$fileExt = new FileExt ([ FileExt :: TYPE_GZIP ]);
2020-07-15 08:33:52 +12:00
$fileSize = new FileSize ( App :: getEnv ( '_APP_STORAGE_LIMIT' , 0 ));
$upload = new Upload ();
if ( empty ( $file )) {
throw new Exception ( 'No file sent' , 400 );
}
// Make sure we handle a single file and multiple files the same way
$file [ 'name' ] = ( \is_array ( $file [ 'name' ]) && isset ( $file [ 'name' ][ 0 ])) ? $file [ 'name' ][ 0 ] : $file [ 'name' ];
$file [ 'tmp_name' ] = ( \is_array ( $file [ 'tmp_name' ]) && isset ( $file [ 'tmp_name' ][ 0 ])) ? $file [ 'tmp_name' ][ 0 ] : $file [ 'tmp_name' ];
$file [ 'size' ] = ( \is_array ( $file [ 'size' ]) && isset ( $file [ 'size' ][ 0 ])) ? $file [ 'size' ][ 0 ] : $file [ 'size' ];
2021-01-28 07:08:46 +13:00
if ( ! $fileExt -> isValid ( $file [ 'name' ])) { // Check if file type is allowed
2021-01-27 11:15:20 +13:00
throw new Exception ( 'File type not allowed' , 400 );
}
2020-07-15 08:33:52 +12:00
if ( ! $fileSize -> isValid ( $file [ 'size' ])) { // Check if file size is exceeding allowed limit
throw new Exception ( 'File size not allowed' , 400 );
}
if ( ! $upload -> isValid ( $file [ 'tmp_name' ])) {
throw new Exception ( 'Invalid file' , 403 );
}
// Save to storage
$size = $device -> getFileSize ( $file [ 'tmp_name' ]);
$path = $device -> getPath ( \uniqid () . '.' . \pathinfo ( $file [ 'name' ], PATHINFO_EXTENSION ));
if ( ! $device -> upload ( $file [ 'tmp_name' ], $path )) { // TODO deprecate 'upload' and replace with 'move'
throw new Exception ( 'Failed moving file' , 500 );
}
2020-07-13 09:18:52 +12:00
$tag = $projectDB -> createDocument ([
'$collection' => Database :: SYSTEM_COLLECTION_TAGS ,
'$permissions' => [
'read' => [],
'write' => [],
],
'functionId' => $function -> getId (),
2020-10-31 08:53:27 +13:00
'dateCreated' => time (),
2020-07-13 09:18:52 +12:00
'command' => $command ,
2020-10-31 08:53:27 +13:00
'path' => $path ,
'size' => $size ,
2020-07-13 09:18:52 +12:00
]);
if ( false === $tag ) {
throw new Exception ( 'Failed saving tag to DB' , 500 );
2020-05-06 05:30:12 +12:00
}
2020-07-19 09:48:28 +12:00
$usage
2020-10-31 08:53:27 +13:00
-> setParam ( 'storage' , $tag -> getAttribute ( 'size' , 0 ))
2020-07-19 09:48:28 +12:00
;
2020-10-31 21:42:41 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $tag , Response :: MODEL_TAG )
;
2020-12-27 04:20:08 +13:00
});
2020-07-13 09:18:52 +12:00
App :: get ( '/v1/functions/:functionId/tags' )
-> groups ([ 'api' , 'functions' ])
2020-05-06 05:30:12 +12:00
-> 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' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TAG_LIST )
2020-09-11 02:40:14 +12:00
-> param ( 'functionId' , '' , new UID (), 'Function unique ID.' )
-> param ( 'search' , '' , new Text ( 256 ), 'Search term to filter your list results. Max length: 256 chars.' , true )
-> param ( 'limit' , 25 , 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 , new Range ( 0 , 2000 ), 'Results offset. The default value is 0. Use this param to manage pagination.' , true )
-> param ( 'orderType' , 'ASC' , new WhiteList ([ 'ASC' , 'DESC' ], true ), 'Order result by ASC or DESC order.' , true )
2020-12-27 04:20:08 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
2020-07-13 09:18:52 +12:00
-> action ( function ( $functionId , $search , $limit , $offset , $orderType , $response , $projectDB ) {
2021-01-17 12:38:13 +13:00
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
2020-07-13 09:18:52 +12:00
$function = $projectDB -> getDocument ( $functionId );
2020-05-06 05:30:12 +12:00
2020-07-13 09:18:52 +12:00
if ( empty ( $function -> getId ()) || Database :: SYSTEM_COLLECTION_FUNCTIONS != $function -> getCollection ()) {
throw new Exception ( 'Function not found' , 404 );
}
$results = $projectDB -> getCollection ([
'limit' => $limit ,
'offset' => $offset ,
2020-12-30 08:30:49 +13:00
'orderType' => $orderType ,
2020-07-13 09:18:52 +12:00
'search' => $search ,
'filters' => [
'$collection=' . Database :: SYSTEM_COLLECTION_TAGS ,
'functionId=' . $function -> getId (),
],
]);
2020-10-31 08:53:27 +13:00
$response -> dynamic ( new Document ([
'sum' => $projectDB -> getSum (),
'tags' => $results
]), Response :: MODEL_TAG_LIST );
2020-12-27 04:20:08 +13:00
});
2020-07-13 09:18:52 +12:00
App :: get ( '/v1/functions/:functionId/tags/:tagId' )
-> groups ([ 'api' , 'functions' ])
2020-05-06 05:30:12 +12:00
-> 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' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TAG )
2020-09-11 02:40:14 +12:00
-> param ( 'functionId' , '' , new UID (), 'Function unique ID.' )
-> param ( 'tagId' , '' , new UID (), 'Tag unique ID.' )
2020-12-27 04:20:08 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
2020-07-13 09:18:52 +12:00
-> action ( function ( $functionId , $tagId , $response , $projectDB ) {
2021-01-17 12:38:13 +13:00
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
2020-07-13 09:18:52 +12:00
$function = $projectDB -> getDocument ( $functionId );
2020-05-06 05:30:12 +12:00
2020-07-13 09:18:52 +12:00
if ( empty ( $function -> getId ()) || Database :: SYSTEM_COLLECTION_FUNCTIONS != $function -> getCollection ()) {
throw new Exception ( 'Function not found' , 404 );
}
2020-05-06 05:30:12 +12:00
2020-07-13 09:18:52 +12:00
$tag = $projectDB -> getDocument ( $tagId );
2020-05-06 05:30:12 +12:00
2020-10-28 08:46:15 +13:00
if ( $tag -> getAttribute ( 'functionId' ) !== $function -> getId ()) {
2020-07-13 09:18:52 +12:00
throw new Exception ( 'Tag not found' , 404 );
}
2020-05-06 05:30:12 +12:00
2020-07-13 09:18:52 +12:00
if ( empty ( $tag -> getId ()) || Database :: SYSTEM_COLLECTION_TAGS != $tag -> getCollection ()) {
throw new Exception ( 'Tag not found' , 404 );
2020-05-06 05:30:12 +12:00
}
2020-10-31 08:53:27 +13:00
$response -> dynamic ( $tag , Response :: MODEL_TAG );
2020-12-27 04:20:08 +13:00
});
2020-07-13 09:18:52 +12:00
App :: delete ( '/v1/functions/:functionId/tags/:tagId' )
-> groups ([ 'api' , 'functions' ])
2020-05-06 05:30:12 +12:00
-> 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' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2020-09-11 02:40:14 +12:00
-> param ( 'functionId' , '' , new UID (), 'Function unique ID.' )
-> param ( 'tagId' , '' , new UID (), 'Tag unique ID.' )
2020-12-27 04:20:08 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
-> inject ( 'usage' )
2020-07-19 09:48:28 +12:00
-> action ( function ( $functionId , $tagId , $response , $projectDB , $usage ) {
2021-01-17 12:38:13 +13:00
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
/** @var Appwrite\Event\Event $usage */
2020-07-13 09:18:52 +12:00
$function = $projectDB -> getDocument ( $functionId );
2020-05-06 05:30:12 +12:00
2020-07-13 09:18:52 +12:00
if ( empty ( $function -> getId ()) || Database :: SYSTEM_COLLECTION_FUNCTIONS != $function -> getCollection ()) {
throw new Exception ( 'Function not found' , 404 );
}
$tag = $projectDB -> getDocument ( $tagId );
2020-05-06 05:30:12 +12:00
2020-10-28 08:46:15 +13:00
if ( $tag -> getAttribute ( 'functionId' ) !== $function -> getId ()) {
2020-07-13 09:18:52 +12:00
throw new Exception ( 'Tag not found' , 404 );
}
2020-05-06 05:30:12 +12:00
2020-07-13 09:18:52 +12:00
if ( empty ( $tag -> getId ()) || Database :: SYSTEM_COLLECTION_TAGS != $tag -> getCollection ()) {
throw new Exception ( 'Tag not found' , 404 );
}
2020-05-07 00:12:52 +12:00
2020-07-19 09:48:28 +12:00
$device = Storage :: getDevice ( 'functions' );
2020-10-31 08:53:27 +13:00
if ( $device -> delete ( $tag -> getAttribute ( 'path' , '' ))) {
2020-07-19 09:48:28 +12:00
if ( ! $projectDB -> deleteDocument ( $tag -> getId ())) {
throw new Exception ( 'Failed to remove tag from DB' , 500 );
}
2020-05-06 05:30:12 +12:00
}
2020-10-31 08:53:27 +13:00
if ( $function -> getAttribute ( 'tag' ) === $tag -> getId ()) { // Reset function tag
$function = $projectDB -> updateDocument ( array_merge ( $function -> getArrayCopy (), [
'tag' => '' ,
]));
if ( false === $function ) {
throw new Exception ( 'Failed saving function to DB' , 500 );
}
}
2020-07-19 09:48:28 +12:00
$usage
2020-10-31 08:53:27 +13:00
-> setParam ( 'storage' , $tag -> getAttribute ( 'size' , 0 ) * - 1 )
2020-07-19 09:48:28 +12:00
;
2021-03-19 09:32:16 +13:00
$response -> dynamic ( new Document (), Response :: MODEL_NONE );
2020-12-27 04:20:08 +13:00
});
2020-07-13 09:18:52 +12:00
App :: post ( '/v1/functions/:functionId/executions' )
-> groups ([ 'api' , 'functions' ])
2020-05-06 05:30:12 +12:00
-> desc ( 'Create Execution' )
2020-12-30 20:26:01 +13:00
-> label ( 'scope' , 'execution.write' )
2020-12-30 12:00:44 +13:00
-> label ( 'sdk.platform' , [ APP_PLATFORM_CLIENT , APP_PLATFORM_SERVER ])
2020-05-06 05:30:12 +12:00
-> label ( 'sdk.namespace' , 'functions' )
-> label ( 'sdk.method' , 'createExecution' )
-> label ( 'sdk.description' , '/docs/references/functions/create-execution.md' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_EXECUTION )
2021-01-12 11:21:56 +13:00
-> label ( 'abuse-limit' , 60 )
-> label ( 'abuse-time' , 60 )
2020-09-11 02:40:14 +12:00
-> param ( 'functionId' , '' , new UID (), 'Function unique ID.' )
2020-10-31 08:53:27 +13:00
// ->param('async', 1, new Range(0, 1), 'Execute code asynchronously. Pass 1 for true, 0 for false. Default value is 1.', true)
2020-12-27 04:20:08 +13:00
-> inject ( 'response' )
-> inject ( 'project' )
-> inject ( 'projectDB' )
2020-10-31 08:53:27 +13:00
-> action ( function ( $functionId , /*$async,*/ $response , $project , $projectDB ) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2020-07-17 09:50:37 +12:00
/** @var Appwrite\Database\Document $project */
/** @var Appwrite\Database\Database $projectDB */
2020-12-30 12:00:44 +13:00
Authorization :: disable ();
2020-07-13 09:18:52 +12:00
$function = $projectDB -> getDocument ( $functionId );
if ( empty ( $function -> getId ()) || Database :: SYSTEM_COLLECTION_FUNCTIONS != $function -> getCollection ()) {
throw new Exception ( 'Function not found' , 404 );
}
2020-07-17 09:50:37 +12:00
$tag = $projectDB -> getDocument ( $function -> getAttribute ( 'tag' ));
2020-10-28 08:46:15 +13:00
if ( $tag -> getAttribute ( 'functionId' ) !== $function -> getId ()) {
2020-07-17 09:50:37 +12:00
throw new Exception ( 'Tag not found. Deploy tag before trying to execute a function' , 404 );
}
if ( empty ( $tag -> getId ()) || Database :: SYSTEM_COLLECTION_TAGS != $tag -> getCollection ()) {
throw new Exception ( 'Tag not found. Deploy tag before trying to execute a function' , 404 );
}
2020-12-30 12:00:44 +13:00
2020-12-30 20:26:01 +13:00
Authorization :: reset ();
2020-12-30 12:00:44 +13:00
$validator = new Authorization ( $function , 'execute' );
if ( ! $validator -> isValid ( $function -> getPermissions ())) { // Check if user has write access to execute function
throw new Exception ( $validator -> getDescription (), 401 );
}
2020-12-30 20:26:01 +13:00
Authorization :: disable ();
2020-07-13 09:18:52 +12:00
$execution = $projectDB -> createDocument ([
'$collection' => Database :: SYSTEM_COLLECTION_EXECUTIONS ,
'$permissions' => [
2020-12-30 20:26:01 +13:00
'read' => $function -> getPermissions ()[ 'execute' ] ? ? [],
2020-07-13 09:18:52 +12:00
'write' => [],
],
'dateCreated' => time (),
'functionId' => $function -> getId (),
2020-10-31 08:53:27 +13:00
'trigger' => 'http' , // http / schedule / event
2020-07-17 20:14:16 +12:00
'status' => 'waiting' , // waiting / processing / completed / failed
2020-07-13 09:18:52 +12:00
'exitCode' => 0 ,
'stdout' => '' ,
'stderr' => '' ,
'time' => 0 ,
]);
2020-12-30 12:00:44 +13:00
Authorization :: reset ();
2020-07-13 09:18:52 +12:00
if ( false === $execution ) {
throw new Exception ( 'Failed saving execution to DB' , 500 );
2020-05-06 05:30:12 +12:00
}
2020-10-31 08:53:27 +13:00
Resque :: enqueue ( 'v1-functions' , 'FunctionsV1' , [
'projectId' => $project -> getId (),
'functionId' => $function -> getId (),
'executionId' => $execution -> getId (),
'trigger' => 'http' ,
]);
2020-07-13 09:18:52 +12:00
2020-10-31 21:42:41 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $execution , Response :: MODEL_EXECUTION )
;
2020-12-27 04:20:08 +13:00
});
2020-07-13 09:18:52 +12:00
App :: get ( '/v1/functions/:functionId/executions' )
-> groups ([ 'api' , 'functions' ])
2020-05-06 05:30:12 +12:00
-> desc ( 'List Executions' )
2020-12-30 20:26:01 +13:00
-> label ( 'scope' , 'execution.read' )
2020-12-30 12:00:44 +13:00
-> label ( 'sdk.platform' , [ APP_PLATFORM_CLIENT , APP_PLATFORM_SERVER ])
2020-05-06 05:30:12 +12:00
-> label ( 'sdk.namespace' , 'functions' )
-> label ( 'sdk.method' , 'listExecutions' )
-> label ( 'sdk.description' , '/docs/references/functions/list-executions.md' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_EXECUTION_LIST )
2020-09-11 02:40:14 +12:00
-> param ( 'functionId' , '' , new UID (), 'Function unique ID.' )
-> param ( 'search' , '' , new Text ( 256 ), 'Search term to filter your list results. Max length: 256 chars.' , true )
-> param ( 'limit' , 25 , 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 , new Range ( 0 , 2000 ), 'Results offset. The default value is 0. Use this param to manage pagination.' , true )
-> param ( 'orderType' , 'ASC' , new WhiteList ([ 'ASC' , 'DESC' ], true ), 'Order result by ASC or DESC order.' , true )
2020-12-27 04:20:08 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
2020-07-13 09:18:52 +12:00
-> action ( function ( $functionId , $search , $limit , $offset , $orderType , $response , $projectDB ) {
2021-01-17 12:38:13 +13:00
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
2020-07-13 09:18:52 +12:00
$function = $projectDB -> getDocument ( $functionId );
2020-05-06 05:30:12 +12:00
2020-07-13 09:18:52 +12:00
if ( empty ( $function -> getId ()) || Database :: SYSTEM_COLLECTION_FUNCTIONS != $function -> getCollection ()) {
throw new Exception ( 'Function not found' , 404 );
}
$results = $projectDB -> getCollection ([
'limit' => $limit ,
'offset' => $offset ,
2020-12-30 08:30:49 +13:00
'orderType' => $orderType ,
2020-07-13 09:18:52 +12:00
'search' => $search ,
'filters' => [
'$collection=' . Database :: SYSTEM_COLLECTION_EXECUTIONS ,
'functionId=' . $function -> getId (),
],
]);
2020-10-31 08:53:27 +13:00
$response -> dynamic ( new Document ([
'sum' => $projectDB -> getSum (),
'executions' => $results
]), Response :: MODEL_EXECUTION_LIST );
2020-12-27 04:20:08 +13:00
});
2020-07-13 09:18:52 +12:00
App :: get ( '/v1/functions/:functionId/executions/:executionId' )
-> groups ([ 'api' , 'functions' ])
2020-05-06 05:30:12 +12:00
-> desc ( 'Get Execution' )
2020-12-30 20:26:01 +13:00
-> label ( 'scope' , 'execution.read' )
2020-12-30 12:00:44 +13:00
-> label ( 'sdk.platform' , [ APP_PLATFORM_CLIENT , APP_PLATFORM_SERVER ])
2020-05-06 05:30:12 +12:00
-> label ( 'sdk.namespace' , 'functions' )
-> label ( 'sdk.method' , 'getExecution' )
-> label ( 'sdk.description' , '/docs/references/functions/get-execution.md' )
2020-11-12 10:02:24 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_EXECUTION )
2020-09-11 02:40:14 +12:00
-> param ( 'functionId' , '' , new UID (), 'Function unique ID.' )
-> param ( 'executionId' , '' , new UID (), 'Execution unique ID.' )
2020-12-27 04:20:08 +13:00
-> inject ( 'response' )
-> inject ( 'projectDB' )
2020-07-13 09:18:52 +12:00
-> action ( function ( $functionId , $executionId , $response , $projectDB ) {
2021-01-17 12:38:13 +13:00
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
2020-07-13 09:18:52 +12:00
$function = $projectDB -> getDocument ( $functionId );
2020-05-06 05:30:12 +12:00
2020-07-13 09:18:52 +12:00
if ( empty ( $function -> getId ()) || Database :: SYSTEM_COLLECTION_FUNCTIONS != $function -> getCollection ()) {
throw new Exception ( 'Function not found' , 404 );
}
2020-05-06 05:30:12 +12:00
2020-07-13 09:18:52 +12:00
$execution = $projectDB -> getDocument ( $executionId );
2020-05-06 05:30:12 +12:00
2020-10-28 08:46:15 +13:00
if ( $execution -> getAttribute ( 'functionId' ) !== $function -> getId ()) {
2020-07-13 09:18:52 +12:00
throw new Exception ( 'Execution not found' , 404 );
}
2020-05-06 05:30:12 +12:00
2020-07-13 09:18:52 +12:00
if ( empty ( $execution -> getId ()) || Database :: SYSTEM_COLLECTION_EXECUTIONS != $execution -> getCollection ()) {
throw new Exception ( 'Execution not found' , 404 );
2020-05-06 05:30:12 +12:00
}
2020-07-13 09:18:52 +12:00
2020-10-31 08:53:27 +13:00
$response -> dynamic ( $execution , Response :: MODEL_EXECUTION );
2020-12-27 04:20:08 +13:00
});