Add endpoint to get template by ID
This commit is contained in:
parent
939adfd09b
commit
395f431718
5 changed files with 62 additions and 5 deletions
|
@ -529,6 +529,11 @@ return [
|
||||||
'description' => 'Synchronous function execution timed out. Use asynchronous execution instead, or ensure the execution duration doesn\'t exceed 30 seconds.',
|
'description' => 'Synchronous function execution timed out. Use asynchronous execution instead, or ensure the execution duration doesn\'t exceed 30 seconds.',
|
||||||
'code' => 408,
|
'code' => 408,
|
||||||
],
|
],
|
||||||
|
Exception::FUNCTION_TEMPLATE_NOT_FOUND => [
|
||||||
|
'name' => Exception::FUNCTION_TEMPLATE_NOT_FOUND,
|
||||||
|
'description' => 'Function Template with the requested ID could not be found.',
|
||||||
|
'code' => 404,
|
||||||
|
],
|
||||||
|
|
||||||
/** Builds */
|
/** Builds */
|
||||||
Exception::BUILD_NOT_FOUND => [
|
Exception::BUILD_NOT_FOUND => [
|
||||||
|
|
|
@ -2392,3 +2392,28 @@ App::get('/v1/functions/templates')
|
||||||
'total' => \count($responseTemplates),
|
'total' => \count($responseTemplates),
|
||||||
]), Response::MODEL_TEMPLATE_FUNCTION_LIST);
|
]), Response::MODEL_TEMPLATE_FUNCTION_LIST);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
App::get('/v1/functions/templates/:templateId')
|
||||||
|
->desc('Get function template')
|
||||||
|
->label('scope', 'public')
|
||||||
|
->label('sdk.namespace', 'functions')
|
||||||
|
->label('sdk.method', 'getTemplate')
|
||||||
|
->label('sdk.description', '/docs/references/functions/get-template.md')
|
||||||
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
||||||
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
||||||
|
->label('sdk.response.model', Response::MODEL_TEMPLATE_FUNCTION)
|
||||||
|
->param('templateId', '', new Text(128), 'Template ID.')
|
||||||
|
->inject('response')
|
||||||
|
->action(function (string $templateId, Response $response) {
|
||||||
|
$templates = Config::getParam('function-templates', []);
|
||||||
|
|
||||||
|
$template = array_shift(\array_filter($templates, function ($template) use ($templateId) {
|
||||||
|
return $template['id'] === $templateId;
|
||||||
|
}));
|
||||||
|
|
||||||
|
if (empty($template)) {
|
||||||
|
throw new Exception(Exception::FUNCTION_TEMPLATE_NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
$response->dynamic(new Document($template), Response::MODEL_TEMPLATE_FUNCTION);
|
||||||
|
});
|
||||||
|
|
1
docs/references/functions/get-template.md
Normal file
1
docs/references/functions/get-template.md
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Get a function template using ID. You can use template details in [createFunction](/docs/references/cloud/server-nodejs/functions#create) method.
|
|
@ -156,6 +156,7 @@ class Exception extends \Exception
|
||||||
public const FUNCTION_RUNTIME_UNSUPPORTED = 'function_runtime_unsupported';
|
public const FUNCTION_RUNTIME_UNSUPPORTED = 'function_runtime_unsupported';
|
||||||
public const FUNCTION_ENTRYPOINT_MISSING = 'function_entrypoint_missing';
|
public const FUNCTION_ENTRYPOINT_MISSING = 'function_entrypoint_missing';
|
||||||
public const FUNCTION_SYNCHRONOUS_TIMEOUT = 'function_synchronous_timeout';
|
public const FUNCTION_SYNCHRONOUS_TIMEOUT = 'function_synchronous_timeout';
|
||||||
|
public const FUNCTION_TEMPLATE_NOT_FOUND = 'function_template_not_found';
|
||||||
|
|
||||||
/** Deployments */
|
/** Deployments */
|
||||||
public const DEPLOYMENT_NOT_FOUND = 'deployment_not_found';
|
public const DEPLOYMENT_NOT_FOUND = 'deployment_not_found';
|
||||||
|
|
|
@ -965,7 +965,7 @@ class FunctionsCustomClientTest extends Scope
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetFunctionTemplates()
|
public function testListTemplates()
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Test for SUCCESS
|
* Test for SUCCESS
|
||||||
|
@ -973,7 +973,6 @@ class FunctionsCustomClientTest extends Scope
|
||||||
$expectedTemplates = array_slice(Config::getParam('function-templates', []), 0, 25);
|
$expectedTemplates = array_slice(Config::getParam('function-templates', []), 0, 25);
|
||||||
$templates = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
|
$templates = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
|
||||||
'content-type' => 'application/json',
|
'content-type' => 'application/json',
|
||||||
'x-appwrite-project' => $this->getProject()['$id'],
|
|
||||||
], $this->getHeaders()));
|
], $this->getHeaders()));
|
||||||
|
|
||||||
$this->assertEquals(200, $templates['headers']['status-code']);
|
$this->assertEquals(200, $templates['headers']['status-code']);
|
||||||
|
@ -994,7 +993,6 @@ class FunctionsCustomClientTest extends Scope
|
||||||
|
|
||||||
$templates_offset = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
|
$templates_offset = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
|
||||||
'content-type' => 'application/json',
|
'content-type' => 'application/json',
|
||||||
'x-appwrite-project' => $this->getProject()['$id'],
|
|
||||||
], $this->getHeaders()), [
|
], $this->getHeaders()), [
|
||||||
'limit' => 1,
|
'limit' => 1,
|
||||||
'offset' => 2
|
'offset' => 2
|
||||||
|
@ -1007,7 +1005,6 @@ class FunctionsCustomClientTest extends Scope
|
||||||
|
|
||||||
$templates = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
|
$templates = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
|
||||||
'content-type' => 'application/json',
|
'content-type' => 'application/json',
|
||||||
'x-appwrite-project' => $this->getProject()['$id'],
|
|
||||||
], $this->getHeaders()), [
|
], $this->getHeaders()), [
|
||||||
'useCases' => ['starter', 'ai'],
|
'useCases' => ['starter', 'ai'],
|
||||||
'runtimes' => ['bun-1.0', 'dart-2.16']
|
'runtimes' => ['bun-1.0', 'dart-2.16']
|
||||||
|
@ -1046,7 +1043,6 @@ class FunctionsCustomClientTest extends Scope
|
||||||
*/
|
*/
|
||||||
$templates = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
|
$templates = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
|
||||||
'content-type' => 'application/json',
|
'content-type' => 'application/json',
|
||||||
'x-appwrite-project' => $this->getProject()['$id'],
|
|
||||||
], $this->getHeaders()), [
|
], $this->getHeaders()), [
|
||||||
'limit' => 5001,
|
'limit' => 5001,
|
||||||
'offset' => 10,
|
'offset' => 10,
|
||||||
|
@ -1066,4 +1062,33 @@ class FunctionsCustomClientTest extends Scope
|
||||||
$this->assertEquals(400, $templates['headers']['status-code']);
|
$this->assertEquals(400, $templates['headers']['status-code']);
|
||||||
$this->assertEquals('Invalid `offset` param: Value must be a valid range between 0 and 5,000', $templates['body']['message']);
|
$this->assertEquals('Invalid `offset` param: Value must be a valid range between 0 and 5,000', $templates['body']['message']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetTemplate()
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Test for SUCCESS
|
||||||
|
*/
|
||||||
|
$template = $this->client->call(Client::METHOD_GET, '/functions/templates/query-neo4j-auradb', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
], $this->getHeaders()), []);
|
||||||
|
|
||||||
|
$this->assertEquals(200, $template['headers']['status-code']);
|
||||||
|
$this->assertIsArray($template['body']);
|
||||||
|
$this->assertEquals('query-neo4j-auradb', $template['body']['id']);
|
||||||
|
$this->assertEquals('Query Neo4j AuraDB', $template['body']['name']);
|
||||||
|
$this->assertEquals('icon-neo4j', $template['body']['icon']);
|
||||||
|
$this->assertEquals('Graph database with focus on relations between data.', $template['body']['tagline']);
|
||||||
|
$this->assertEquals(['databases'], $template['body']['useCases']);
|
||||||
|
$this->assertEquals('github', $template['body']['vcsProvider']);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for FAILURE
|
||||||
|
*/
|
||||||
|
$template = $this->client->call(Client::METHOD_GET, '/functions/templates/invalid-template-id', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
], $this->getHeaders()), []);
|
||||||
|
|
||||||
|
$this->assertEquals(404, $template['headers']['status-code']);
|
||||||
|
$this->assertEquals('Function Template with the requested ID could not be found.', $template['body']['message']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue