From 44ee22eb3e9e18dba89d8de288fd83cb32ce1959 Mon Sep 17 00:00:00 2001 From: eldadfux Date: Sun, 9 Jun 2019 11:51:10 +0300 Subject: [PATCH] Changed status code to 201 for endpoints creating new resources --- app/app.php | 11 ++++-- app/controllers/database.php | 15 ++++++-- app/controllers/health.php | 4 ++ app/controllers/locale.php | 2 +- app/controllers/projects.php | 26 ++++++++++--- app/controllers/storage.php | 6 ++- app/controllers/teams.php | 15 ++++++-- app/controllers/users.php | 55 +++++++++++++++------------- src/Database/Validator/Structure.php | 2 - 9 files changed, 91 insertions(+), 45 deletions(-) diff --git a/app/app.php b/app/app.php index f40b0ca7af..7f648e447c 100644 --- a/app/app.php +++ b/app/app.php @@ -550,7 +550,8 @@ $utopia->get('/v1/open-api-2.json') 'email' => APP_EMAIL_TEAM, ], 'license' => [ - 'name' => 'MIT', + 'name' => 'BSD 3-Clause', + 'url' => 'https://raw.githubusercontent.com/appwrite/appwrite/master/LICENSE', ], ], 'host' => $domain, @@ -562,13 +563,13 @@ $utopia->get('/v1/open-api-2.json') 'Project' => [ 'type' => 'apiKey', 'name' => 'X-Appwrite-Project', - 'description' => '', + 'description' => 'Your Appwrite project ID. You can find your project ID in your Appwrite console project settings.', 'in' => 'header', ], 'Key' => [ 'type' => 'apiKey', 'name' => 'X-Appwrite-Key', - 'description' => '', + 'description' => 'Your Appwrite project secret key. You can can create a new API key from your Appwrite console API keys dashboard.', 'in' => 'header', ], 'Locale' => [ @@ -639,6 +640,10 @@ $utopia->get('/v1/open-api-2.json') ), ), ), + 'externalDocs' => [ + 'description' => 'Full API docs, specs and tutorials', + 'url' => APP_PROTOCOL . '://' . $domain . '/docs' + ] ]; foreach ($utopia->getRoutes() as $key => $method) { diff --git a/app/controllers/database.php b/app/controllers/database.php index c86c1b017d..25f8c11359 100644 --- a/app/controllers/database.php +++ b/app/controllers/database.php @@ -4,6 +4,7 @@ global $utopia, $register, $request, $response, $webhook, $audit, $projectDB; use Utopia\App; use Utopia\Exception; +use Utopia\Response; use Utopia\Validator\Range; use Utopia\Validator\WhiteList; use Utopia\Validator\Text; @@ -173,7 +174,10 @@ $utopia->post('/v1/database') /** * View */ - $response->json($data); + $response + ->setStatusCode(Response::STATUS_CODE_CREATED) + ->json($data) + ; } ); @@ -181,8 +185,8 @@ $utopia->put('/v1/database/:collectionId') ->desc('Update Team') ->label('scope', 'collections.write') ->label('sdk.namespace', 'teams') - ->label('sdk.method', 'updateTeam') - ->label('sdk.description', 'Update team by its unique ID. Only team owners have write access for this resource.') + ->label('sdk.method', 'updateCollection') + ->label('sdk.description', 'Update collection by its unique ID.') ->param('collectionId', '', function () {return new UID();}, 'Collection unique ID.') ->param('name', null, function () {return new Text(256);}, 'Collection name.') ->param('read', [], function () {return new ArrayList(new Text(64));}, 'An array of read permissions. [Learn more about permissions and roles](/docs/permissions).', true) @@ -461,7 +465,10 @@ $utopia->post('/v1/database/:collectionId') /** * View */ - $response->json($data); + $response + ->setStatusCode(Response::STATUS_CODE_CREATED) + ->json($data) + ; } ); diff --git a/app/controllers/health.php b/app/controllers/health.php index 569d53b737..d5bc948bbf 100644 --- a/app/controllers/health.php +++ b/app/controllers/health.php @@ -166,6 +166,10 @@ $utopia->get('/v1/health/stats') $response ->json([ + 'server' => [ + 'name' => 'nginx', + 'version' => shell_exec('nginx -v 2>&1'), + ], 'storage' => [ 'used' => $device->human($device->getDirectorySize($device->getRoot() . '/')), 'partitionTotal' => $device->human($device->getPartitionTotalSpace()), diff --git a/app/controllers/locale.php b/app/controllers/locale.php index 7bf10801da..2aca374876 100644 --- a/app/controllers/locale.php +++ b/app/controllers/locale.php @@ -10,7 +10,7 @@ $utopia->get('/v1/locale') ->desc('Get User Locale') ->label('scope', 'locale.read') ->label('sdk.namespace', 'locale') - ->label('sdk.method', 'get') + ->label('sdk.method', 'getLocale') ->label('sdk.description', 'Get the current user location based on IP. Returns an object with user country code, country name, continent name, continent code, ip address and suggested currency. You can use the locale header to get the data in supported language.') ->action( function() use ($response, $request, $utopia) diff --git a/app/controllers/projects.php b/app/controllers/projects.php index b098b68de8..9c986fa664 100644 --- a/app/controllers/projects.php +++ b/app/controllers/projects.php @@ -3,6 +3,7 @@ global $utopia, $request, $response, $register, $user, $consoleDB, $projectDB, $providers; use Utopia\Exception; +use Utopia\Response; use Utopia\Validator\ArrayList; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; @@ -286,7 +287,10 @@ $utopia->post('/v1/projects') $consoleDB->createNamespace($project->getUid()); - $response->json($project->getArrayCopy()); + $response + ->setStatusCode(Response::STATUS_CODE_CREATED) + ->json($project->getArrayCopy()) + ; } ); @@ -527,7 +531,10 @@ $utopia->post('/v1/projects/:projectId/webhooks') throw new Exception('Failed saving project to DB', 500); } - $response->json($webhook->getArrayCopy()); + $response + ->setStatusCode(Response::STATUS_CODE_CREATED) + ->json($webhook->getArrayCopy()) + ; } ); @@ -703,7 +710,10 @@ $utopia->post('/v1/projects/:projectId/keys') throw new Exception('Failed saving project to DB', 500); } - $response->json($key->getArrayCopy()); + $response + ->setStatusCode(Response::STATUS_CODE_CREATED) + ->json($key->getArrayCopy()) + ; } ); @@ -917,7 +927,10 @@ $utopia->post('/v1/projects/:projectId/tasks') ResqueScheduler::enqueueAt($next, 'v1-tasks', 'TasksV1', $task->getArrayCopy()); } - $response->json($task->getArrayCopy()); + $response + ->setStatusCode(Response::STATUS_CODE_CREATED) + ->json($task->getArrayCopy()) + ; } ); @@ -1117,7 +1130,10 @@ $utopia->post('/v1/projects/:projectId/platforms') throw new Exception('Failed saving project to DB', 500); } - $response->json($platform->getArrayCopy()); + $response + ->setStatusCode(Response::STATUS_CODE_CREATED) + ->json($platform->getArrayCopy()) + ; } ); diff --git a/app/controllers/storage.php b/app/controllers/storage.php index e8ce0d5915..052b4b52c3 100644 --- a/app/controllers/storage.php +++ b/app/controllers/storage.php @@ -3,6 +3,7 @@ global $utopia, $request, $response, $register, $user, $audit, $usage, $project, $projectDB; use Utopia\Exception; +use Utopia\Response; use Utopia\Validator\ArrayList; use Utopia\Validator\WhiteList; use Utopia\Validator\Range; @@ -527,7 +528,10 @@ $utopia->post('/v1/storage/files') $list[] = $file->getArrayCopy(); } - $response->json($list); + $response + ->setStatusCode(Response::STATUS_CODE_CREATED) + ->json($list) + ; } ); diff --git a/app/controllers/teams.php b/app/controllers/teams.php index 1475e36824..dfe9b14ede 100644 --- a/app/controllers/teams.php +++ b/app/controllers/teams.php @@ -3,6 +3,7 @@ global $utopia, $register, $request, $response, $projectDB, $project, $user, $audit, $mode; use Utopia\Exception; +use Utopia\Response; use Utopia\Validator\Email; use Utopia\Validator\Text; use Utopia\Validator\Host; @@ -174,7 +175,10 @@ $utopia->post('/v1/teams') } } - $response->json($team->getArrayCopy()); + $response + ->setStatusCode(Response::STATUS_CODE_CREATED) + ->json($team->getArrayCopy()) + ; } ); @@ -385,7 +389,9 @@ $utopia->post('/v1/teams/:teamId/memberships') ->setParam('event', 'auth.invite') ; - $response->noContent(); + $response + //->setStatusCode(Response::STATUS_CODE_CREATED) TODO change response of this endpoint + ->noContent(); } ); @@ -462,7 +468,10 @@ $utopia->post('/v1/teams/:teamId/memberships/:inviteId/resend') ->setParam('event', 'auth.invite.resend') ; - $response->noContent(); + $response + // ->setStatusCode(Response::STATUS_CODE_CREATED) TODO change response of this endpoint + ->noContent() + ; } ); diff --git a/app/controllers/users.php b/app/controllers/users.php index 35e605e129..1160acf2e6 100644 --- a/app/controllers/users.php +++ b/app/controllers/users.php @@ -5,6 +5,7 @@ global $utopia, $response, $projectDB; use Auth\Auth; use Auth\Validator\Password; use Utopia\Exception; +use Utopia\Response; use Utopia\Validator\WhiteList; use Utopia\Validator\Email; use Utopia\Validator\Text; @@ -318,37 +319,39 @@ $utopia->post('/v1/users') 'name' => $name, ]); - $response->json(array_merge($user->getArrayCopy([ - '$uid', - 'status', - 'email', - 'registration', - 'confirm', - 'name', - 'oauthBitbucket', - 'oauthBitBucketAccessToken', - 'oauthFacebook', - 'oauthFacebookAccessToken', - 'oauthGithub', - 'oauthGithubAccessToken', - 'oauthGitlab', - 'oauthGitlabAccessToken', - 'oauthGoogle', - 'oauthGoogleAccessToken', - 'oauthInstagram', - 'oauthInstagramAccessToken', - 'oauthLinkedin', - 'oauthLinkedinAccessToken', - 'oauthMicrosoft', - 'oauthMicrosoftAccessToken', - 'oauthTwitter', - 'oauthTwitterAccessToken', + $response + ->setStatusCode(Response::STATUS_CODE_CREATED) + ->json(array_merge($user->getArrayCopy([ + '$uid', + 'status', + 'email', + 'registration', + 'confirm', + 'name', + 'oauthBitbucket', + 'oauthBitBucketAccessToken', + 'oauthFacebook', + 'oauthFacebookAccessToken', + 'oauthGithub', + 'oauthGithubAccessToken', + 'oauthGitlab', + 'oauthGitlabAccessToken', + 'oauthGoogle', + 'oauthGoogleAccessToken', + 'oauthInstagram', + 'oauthInstagramAccessToken', + 'oauthLinkedin', + 'oauthLinkedinAccessToken', + 'oauthMicrosoft', + 'oauthMicrosoftAccessToken', + 'oauthTwitter', + 'oauthTwitterAccessToken', ]), ['roles' => Authorization::getRoles()])); } ); $utopia->patch('/v1/users/:userId/status') - ->desc('Block User') + ->desc('Update user status') ->label('scope', 'users.write') ->label('sdk.namespace', 'users') ->label('sdk.method', 'updateUserStatus') diff --git a/src/Database/Validator/Structure.php b/src/Database/Validator/Structure.php index b623954e8b..d1689efd10 100644 --- a/src/Database/Validator/Structure.php +++ b/src/Database/Validator/Structure.php @@ -126,8 +126,6 @@ class Structure extends Validator $this->id = $document->getUid(); - $original = (!empty($this->id)) ? $this->database->getDocument($document->getUid()) : new Document([]); - if(is_null($document->getCollection())) { $this->message = 'Missing collection attribute $collection'; return false;