From 44a92e5dd64ab76090c79c9b19d262e63895a659 Mon Sep 17 00:00:00 2001 From: softwarespot Date: Wed, 22 Jul 2015 16:24:27 +0300 Subject: [PATCH 1/5] Re-wrote example to be on par with the RESTful approach --- application/controllers/api/Example.php | 68 +++++++++++++------------ 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/application/controllers/api/Example.php b/application/controllers/api/Example.php index 3a09c699..3d75c156 100644 --- a/application/controllers/api/Example.php +++ b/application/controllers/api/Example.php @@ -30,9 +30,38 @@ function __construct() $this->methods['user_delete']['limit'] = 50; // 50 requests per hour per user/key } - public function user_get($id = NULL) + public function users_get($id = NULL) { - // If the id has not been passed via the URL e.g. example/user/:id, then + // Users from a data store e.g. database + // $user = $this->some_model->getSomething($id); + $users = [ + ['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'], + ['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'], + ['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]], + ]; + + // If the id parameter and query parameter don't exist, return all users instead + if ($id === NULL && $this->get('id') === NULL) + { + // Check if the users data store contains users (in case the database result returns NULL) + if ($users) + { + // Set the response and exit + $this->response($users, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code + } + else + { + // Set the response and exit + $this->response([ + 'status' => FALSE, + 'error' => 'No users were found' + ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code + } + + } + + + // If the id has not been passed via the URL e.g. example/users/:id, then // check the id query parameter id=? instead if ($id === NULL) { @@ -49,16 +78,10 @@ public function user_get($id = NULL) $this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code } - // $user = $this->some_model->getSomething($id); - $users = [ - 1 => ['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'], - 2 => ['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'], - 3 => ['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]], - ]; - // Get the user from the array, by retrieving the id from the GET request $user = isset($users[$id]) ? $users[$id] : NULL; + // If a user exists in the data store e.g. database if ($user) { $this->set_response($user, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code @@ -72,7 +95,7 @@ public function user_get($id = NULL) } } - public function user_post() + public function users_post() { // $this->some_model->update_user( ... ); $message = [ @@ -85,9 +108,9 @@ public function user_post() $this->set_response($message, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code } - public function user_delete() + public function users_delete() { - // If the id has not been passed via the URL e.g. example/user/:id, then + // If the id has not been passed via the URL e.g. example/users/:id, then // check the id query parameter id=? instead if ($id === NULL) { @@ -113,25 +136,4 @@ public function user_delete() $this->set_response($message, REST_Controller::HTTP_NO_CONTENT); // NO_CONTENT (204) being the HTTP response code } - public function users_get() - { - // $users = $this->some_model->get_something($this->get('limit')); - $users = [ - ['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'], - ['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'], - 3 => ['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]], - ]; - - if ($users) - { - $this->set_response($users, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code - } - else - { - $this->set_response([ - 'status' => FALSE, - 'error' => 'No users were found' - ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code - } - } } From 43cb19affaf4bde0bc6dbd9df8da03c2d8c9b4cd Mon Sep 17 00:00:00 2001 From: softwarespot Date: Wed, 22 Jul 2015 20:33:11 +0300 Subject: [PATCH 2/5] Tidied comments and removed whitespace --- application/controllers/api/Key.php | 46 +++++++---------------------- 1 file changed, 11 insertions(+), 35 deletions(-) diff --git a/application/controllers/api/Key.php b/application/controllers/api/Key.php index 2eccab14..0e97016c 100644 --- a/application/controllers/api/Key.php +++ b/application/controllers/api/Key.php @@ -26,11 +26,10 @@ class Key extends REST_Controller { ]; /** - * Key Create * Insert a key into the database * - * @access public - * @return void + * @access public + * @return void */ public function index_put() { @@ -58,14 +57,11 @@ public function index_put() } } - // -------------------------------------------------------------------- - /** - * Key Delete * Remove a key from the database to stop it working * - * @access public - * @return void + * @access public + * @return void */ public function index_delete() { @@ -91,14 +87,11 @@ public function index_delete() ], REST_Controller::HTTP_NO_CONTENT); // NO_CONTENT (204) being the HTTP response code } - // -------------------------------------------------------------------- - /** - * Update Key * Change the level * - * @access public - * @return void + * @access public + * @return void */ public function level_post() { @@ -132,14 +125,11 @@ public function level_post() } } - // -------------------------------------------------------------------- - /** - * Update Key * Change the level * - * @access public - * @return void + * @access public + * @return void */ public function suspend_post() { @@ -172,14 +162,11 @@ public function suspend_post() } } - // -------------------------------------------------------------------- - /** - * Regenerate Key * Remove a key from the database to stop it working * - * @access public - * @return void + * @access public + * @return void */ public function regenerate_post() { @@ -219,8 +206,6 @@ public function regenerate_post() } } - // -------------------------------------------------------------------- - /* Helper Methods */ private function _generate_key() @@ -243,8 +228,6 @@ private function _generate_key() return $new_key; } - // -------------------------------------------------------------------- - /* Private Data Methods */ private function _get_key($key) @@ -255,8 +238,6 @@ private function _get_key($key) ->row(); } - // -------------------------------------------------------------------- - private function _key_exists($key) { return $this->db @@ -264,8 +245,6 @@ private function _key_exists($key) ->count_all_results(config_item('rest_keys_table')) > 0; } - // -------------------------------------------------------------------- - private function _insert_key($key, $data) { $data[config_item('rest_key_column')] = $key; @@ -276,8 +255,6 @@ private function _insert_key($key, $data) ->insert(config_item('rest_keys_table')); } - // -------------------------------------------------------------------- - private function _update_key($key, $data) { return $this->db @@ -285,12 +262,11 @@ private function _update_key($key, $data) ->update(config_item('rest_keys_table'), $data); } - // -------------------------------------------------------------------- - private function _delete_key($key) { return $this->db ->where(config_item('rest_key_column'), $key) ->delete(config_item('rest_keys_table')); } + } From df382cb222f8edcb9499020848853e569dac416c Mon Sep 17 00:00:00 2001 From: softwarespot Date: Wed, 22 Jul 2015 20:57:28 +0300 Subject: [PATCH 3/5] Index values match id values so as not to cause confusion --- application/controllers/api/Example.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/controllers/api/Example.php b/application/controllers/api/Example.php index 3d75c156..c0627015 100644 --- a/application/controllers/api/Example.php +++ b/application/controllers/api/Example.php @@ -35,9 +35,9 @@ public function users_get($id = NULL) // Users from a data store e.g. database // $user = $this->some_model->getSomething($id); $users = [ - ['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'], - ['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'], - ['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]], + 1 => ['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'], + 2 => ['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'], + 3 => ['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]], ]; // If the id parameter and query parameter don't exist, return all users instead From 4e4afa12d06a79f39c34ddaed9a21b612758e6cb Mon Sep 17 00:00:00 2001 From: softwarespot Date: Wed, 22 Jul 2015 21:12:25 +0300 Subject: [PATCH 4/5] Integer type checking --- application/controllers/api/Example.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/application/controllers/api/Example.php b/application/controllers/api/Example.php index c0627015..2b0c5633 100644 --- a/application/controllers/api/Example.php +++ b/application/controllers/api/Example.php @@ -60,10 +60,9 @@ public function users_get($id = NULL) } - // If the id has not been passed via the URL e.g. example/users/:id, then // check the id query parameter id=? instead - if ($id === NULL) + if ($id === NULL || ctype_digit($id) === FALSE) { $id = $this->get('id'); } From 7962955cb84418cf0321eb3bd2dbd2b774060519 Mon Sep 17 00:00:00 2001 From: softwarespot Date: Wed, 22 Jul 2015 21:26:59 +0300 Subject: [PATCH 5/5] Re-tweaked example (again) This should take into consideration many forms --- application/controllers/api/Example.php | 42 ++++++++++++++++--------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/application/controllers/api/Example.php b/application/controllers/api/Example.php index 2b0c5633..c221ea9c 100644 --- a/application/controllers/api/Example.php +++ b/application/controllers/api/Example.php @@ -30,7 +30,7 @@ function __construct() $this->methods['user_delete']['limit'] = 50; // 50 requests per hour per user/key } - public function users_get($id = NULL) + public function users_get($id_param = NULL) { // Users from a data store e.g. database // $user = $this->some_model->getSomething($id); @@ -40,8 +40,17 @@ public function users_get($id = NULL) 3 => ['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]], ]; + // Get the id parameter value + $id = $this->get('id'); + + // If NULL, then check the id passed as users/:id + if ($id === NULL) + { + $id = $id_param; + } + // If the id parameter and query parameter don't exist, return all users instead - if ($id === NULL && $this->get('id') === NULL) + if ($id === NULL) { // Check if the users data store contains users (in case the database result returns NULL) if ($users) @@ -60,16 +69,13 @@ public function users_get($id = NULL) } - // If the id has not been passed via the URL e.g. example/users/:id, then - // check the id query parameter id=? instead - if ($id === NULL || ctype_digit($id) === FALSE) + // Check if the id is a valid integer + if (ctype_digit($id)) { - $id = $this->get('id'); + // Cast as an int + $id = (int) $id; } - // Cast as an int - $id = (int) $id; - // If not a valid id if ($id <= 0) { @@ -107,17 +113,23 @@ public function users_post() $this->set_response($message, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code } - public function users_delete() + public function users_delete($id_param = NULL) { - // If the id has not been passed via the URL e.g. example/users/:id, then - // check the id query parameter id=? instead + // Get the id parameter value + $id = $this->get('id'); + + // If NULL, then check the id passed as users/:id if ($id === NULL) { - $id = $this->get('id'); + $id = $id_param; } - // Cast as an int - $id = (int) $id; + // Check if the id is a valid integer + if (ctype_digit($id)) + { + // Cast as an int + $id = (int) $id; + } // If not a valid id if ($id <= 0)