Skip to content

Commit

Permalink
Merge pull request #518 from softwarespot/master
Browse files Browse the repository at this point in the history
Re-wrote example to be on par with the RESTful approach
  • Loading branch information
chriskacerguis committed Jul 23, 2015
2 parents 1c9484a + 7962955 commit 4d1a0cc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 76 deletions.
95 changes: 54 additions & 41 deletions application/controllers/api/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,51 @@ 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_param = NULL)
{
// If the id has not been passed via the URL e.g. example/user/:id, then
// check the id query parameter id=? instead
// Users from a data store e.g. database
// $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 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)
{
$id = $this->get('id');
// 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
}

}

// 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)
Expand All @@ -49,16 +83,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
Expand All @@ -72,7 +100,7 @@ public function user_get($id = NULL)
}
}

public function user_post()
public function users_post()
{
// $this->some_model->update_user( ... );
$message = [
Expand All @@ -85,17 +113,23 @@ 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($id_param = NULL)
{
// If the id has not been passed via the URL e.g. example/user/: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)
Expand All @@ -113,25 +147,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
}
}
}
46 changes: 11 additions & 35 deletions application/controllers/api/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -219,8 +206,6 @@ public function regenerate_post()
}
}

// --------------------------------------------------------------------

/* Helper Methods */

private function _generate_key()
Expand All @@ -243,8 +228,6 @@ private function _generate_key()
return $new_key;
}

// --------------------------------------------------------------------

/* Private Data Methods */

private function _get_key($key)
Expand All @@ -255,17 +238,13 @@ private function _get_key($key)
->row();
}

// --------------------------------------------------------------------

private function _key_exists($key)
{
return $this->db
->where(config_item('rest_key_column'), $key)
->count_all_results(config_item('rest_keys_table')) > 0;
}

// --------------------------------------------------------------------

private function _insert_key($key, $data)
{
$data[config_item('rest_key_column')] = $key;
Expand All @@ -276,21 +255,18 @@ private function _insert_key($key, $data)
->insert(config_item('rest_keys_table'));
}

// --------------------------------------------------------------------

private function _update_key($key, $data)
{
return $this->db
->where(config_item('rest_key_column'), $key)
->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'));
}

}

0 comments on commit 4d1a0cc

Please sign in to comment.