From 8f2cca5b0f80ec8cf5e4780d46c29d21ff1a40ed Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Fri, 10 Jan 2025 16:53:09 +0900 Subject: [PATCH] Fix Response Models --- app/controllers/api/users.php | 7 --- app/controllers/general.php | 29 ++++++++- docs/tutorials/add-route.md | 20 ++++--- src/Appwrite/SDK/Method.php | 98 +++++++++++++++++++++++++++++++ src/Appwrite/Utopia/Request.php | 10 +++- tests/unit/Utopia/RequestTest.php | 10 +++- 6 files changed, 154 insertions(+), 20 deletions(-) diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index b80bce1a2b..c9fc6a5cd0 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -188,13 +188,6 @@ App::post('/v1/users') ->label('scope', 'users.write') ->label('audits.event', 'user.create') ->label('audits.resource', 'user/{response.$id}') - ->label('sdk.auth', [APP_AUTH_TYPE_KEY]) - ->label('sdk.namespace', 'users') - ->label('sdk.method', 'create') - ->label('sdk.description', '/docs/references/users/create-user.md') - ->label('sdk.response.code', Response::STATUS_CODE_CREATED) - ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_USER) ->label('sdk', new Method( namespace: 'users', name: 'create', diff --git a/app/controllers/general.php b/app/controllers/general.php index 663242882a..4cee363317 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -10,6 +10,10 @@ use Appwrite\Event\Func; use Appwrite\Event\Usage; use Appwrite\Extend\Exception as AppwriteException; use Appwrite\Network\Validator\Origin; +use Appwrite\SDK\AuthType; +use Appwrite\SDK\Method; +use Appwrite\SDK\Response as SDKResponse; +use Appwrite\SDK\ResponseType; use Appwrite\Utopia\Request; use Appwrite\Utopia\Request\Filters\V16 as RequestV16; use Appwrite\Utopia\Request\Filters\V17 as RequestV17; @@ -93,8 +97,29 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo $type = $route->getAttribute('resourceType'); if ($type === 'function') { - $utopia->getRoute()?->label('sdk.namespace', 'functions'); - $utopia->getRoute()?->label('sdk.method', 'createExecution'); + $method = $utopia->getRoute()?->getLabel('sdk', null); + + if (empty($method)) { + $utopia->getRoute()?->label('sdk', new Method( + namespace: 'functions', + name: 'createExecution', + description: '/docs/references/functions/create-execution.md', + auth: [AuthType::SESSION, AuthType::KEY, AuthType::JWT], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_CREATED, + model: Response::MODEL_EXECUTION, + ) + ], + responseType: ResponseType::MULTIPART, + requestType: 'application/json', + )); + } else { + /** @var Method $method */ + $method->setNamespace('functions'); + $method->setMethodName('createExecution'); + $utopia->getRoute()?->label('sdk', $method); + } if (System::getEnv('_APP_OPTIONS_FUNCTIONS_FORCE_HTTPS', 'disabled') === 'enabled') { // Force HTTPS if ($request->getProtocol() !== 'https') { diff --git a/docs/tutorials/add-route.md b/docs/tutorials/add-route.md index ac6fd40bdb..53926f7002 100644 --- a/docs/tutorials/add-route.md +++ b/docs/tutorials/add-route.md @@ -85,13 +85,19 @@ App::post('/v1/account/create') ```php App::post('/v1/account/jwt') - ->label('sdk.auth', [APP_AUTH_TYPE_SESSION]) - ->label('sdk.namespace', 'account') - ->label('sdk.method', 'createJWT') - ->label('sdk.description', '/docs/references/account/create-jwt.md') - ->label('sdk.response.code', Response::STATUS_CODE_CREATED) - ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_JWT) + ->label('sdk', new Method( + namespace: 'account', + name: 'createJWT', + description: '/docs/references/account/create-jwt.md', + auth: [], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_CREATED, + model: Response::MODEL_JWT, + ) + ], + responseType: ResponseType::JSON, + )) ``` #### Cache diff --git a/src/Appwrite/SDK/Method.php b/src/Appwrite/SDK/Method.php index 6a809662d2..5032c6df53 100644 --- a/src/Appwrite/SDK/Method.php +++ b/src/Appwrite/SDK/Method.php @@ -211,6 +211,104 @@ class Method return $this->parameters; } + public function setNamespace(string $namespace): self + { + $this->namespace = $namespace; + return $this; + } + + public function setMethodName(string $name): self + { + $this->name = $name; + return $this; + } + + public function setDescription(string $description): self + { + $this->description = $description; + return $this; + } + + public function setAuth(array $auth): self + { + $this->validateAuthTypes($auth); + $this->auth = $auth; + return $this; + } + + /** + * @param Array $responses + */ + public function setResponses(array $responses): self + { + foreach ($responses as $response) { + $this->validateResponseModel($response->getModel()); + $this->validateNoContent($response); + } + $this->responses = $responses; + return $this; + } + + public function setResponseType(ResponseType $responseType): self + { + $this->responseType = $responseType; + return $this; + } + + public function setMethodType(?MethodType $methodType): self + { + $this->methodType = $methodType; + return $this; + } + + public function setOfflineKey(?string $offlineKey): self + { + $this->offlineKey = $offlineKey; + return $this; + } + + public function setOfflineModel(?string $offlineModel): self + { + $this->offlineModel = $offlineModel; + return $this; + } + + public function setOfflineResponseKey(?string $offlineResponseKey): self + { + $this->offlineResponseKey = $offlineResponseKey; + return $this; + } + + public function setDeprecated(bool $deprecated): self + { + $this->deprecated = $deprecated; + return $this; + } + + public function setHide(bool|array $hide): self + { + $this->hide = $hide; + return $this; + } + + public function setPackaging(bool $packaging): self + { + $this->packaging = $packaging; + return $this; + } + + public function setRequestType(string $requestType): self + { + $this->requestType = $requestType; + return $this; + } + + public function setParameters(array $parameters): self + { + $this->parameters = $parameters; + return $this; + } + // Throw any errors that were found during initialization public static function finaliseInitialization(): void { diff --git a/src/Appwrite/Utopia/Request.php b/src/Appwrite/Utopia/Request.php index 26c1baf188..a4fc0e0f63 100644 --- a/src/Appwrite/Utopia/Request.php +++ b/src/Appwrite/Utopia/Request.php @@ -28,8 +28,14 @@ class Request extends UtopiaRequest $parameters = parent::getParams(); if ($this->hasFilters() && self::hasRoute()) { - $method = self::getRoute()->getLabel('sdk.method', 'unknown'); - $endpointIdentifier = self::getRoute()->getLabel('sdk.namespace', 'unknown') . '.' . $method; + $method = self::getRoute()->getLabel('sdk', null); + /** @var \Appwrite\SDK\Method $method */ + + if (empty($method)) { + $endpointIdentifier = 'unknown.unknown'; + } else { + $endpointIdentifier = $method->getNamespace() . '.' . $method->getMethodName(); + } foreach ($this->getFilters() as $filter) { $parameters = $filter->parse($parameters, $endpointIdentifier); diff --git a/tests/unit/Utopia/RequestTest.php b/tests/unit/Utopia/RequestTest.php index 73daaa88bc..e19fdbe01f 100644 --- a/tests/unit/Utopia/RequestTest.php +++ b/tests/unit/Utopia/RequestTest.php @@ -2,6 +2,7 @@ namespace Tests\Unit\Utopia; +use Appwrite\SDK\Method; use Appwrite\Utopia\Request; use PHPUnit\Framework\TestCase; use Swoole\Http\Request as SwooleRequest; @@ -31,8 +32,13 @@ class RequestTest extends TestCase $this->assertCount(2, $this->request->getFilters()); $route = new Route(Request::METHOD_GET, '/test'); - $route->label('sdk.method', 'method'); - $route->label('sdk.namespace', 'namespace'); + $route->label('sdk', new Method( + namespace: 'namespace', + name: 'method', + description: 'description', + auth: [], + responses: [], + )); // set test header to prevent header populaten inside the request class $this->request->addHeader('EXAMPLE', 'VALUE'); $this->request->setRoute($route);