Skip to content

Commit

Permalink
Merge pull request #513 from softwarespot/master
Browse files Browse the repository at this point in the history
Fixed missing semi-colon and implemented #510
  • Loading branch information
chriskacerguis committed Jul 22, 2015
2 parents 13c2070 + 805b82c commit 1c9484a
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 78 deletions.
17 changes: 16 additions & 1 deletion application/config/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
| `id` INT(11) NOT NULL AUTO_INCREMENT,
| `key` VARCHAR(40) NOT NULL,
| `level` INT(2) NOT NULL,
| `ignore_limits` TINY(1) NOT NULL DEFAULT '0',
| `ignore_limits` TINYINT(1) NOT NULL DEFAULT '0',
| `is_private_key` TINYINT(1) NOT NULL DEFAULT '0',
| `ip_addresses` TEXT NULL DEFAULT NULL,
| `date_created` INT(11) NOT NULL,
Expand All @@ -291,6 +291,21 @@
*/
$config['rest_key_column'] = 'key';

/*
|--------------------------------------------------------------------------
| REST API Limits method
|--------------------------------------------------------------------------
|
| Specify the method used to limit the API calls
|
| Available methods are :
| $config['rest_limits_method'] = 'API_KEY'; // Put a limit per api key
| $config['rest_limits_method'] = 'METHOD_NAME'; // Put a limit on method calls
| $config['rest_limits_method'] = 'ROUTED_URL'; // Put a limit on the routed URL
|
*/
$config['rest_limits_method'] = 'ROUTED_URL';

/*
|--------------------------------------------------------------------------
| REST Key Length
Expand Down
52 changes: 40 additions & 12 deletions application/controllers/api/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,34 @@ function __construct()
$this->methods['user_delete']['limit'] = 50; // 50 requests per hour per user/key
}

public function user_get()
public function user_get($id = NULL)
{
if (!$this->get('id'))
// If the id has not been passed via the URL e.g. example/user/:id, then
// check the id query parameter id=? instead
if ($id === NULL)
{
$id = $this->get('id');
}

// Cast as an int
$id = (int) $id;

// If not a valid id
if ($id <= 0)
{
// Set the response and exit
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}

// $user = $this->some_model->getSomething($this->get('id'));
// $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[$this->get('id')]) ? $users[$this->get('id')] : NULL;
$user = isset($users[$id]) ? $users[$id] : NULL;

if ($user)
{
Expand All @@ -57,15 +68,15 @@ public function user_get()
$this->set_response([
'status' => FALSE,
'error' => 'User could not be found'
], REST_Controller::NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
}

public function user_post()
{
// $this->some_model->update_user($this->get('id'));
// $this->some_model->update_user( ... );
$message = [
'id' => $this->get('id'),
'id' => 100, // Automatically generated by the model
'name' => $this->post('name'),
'email' => $this->post('email'),
'message' => 'Added a resource'
Expand All @@ -76,9 +87,26 @@ public function user_post()

public function user_delete()
{
// $this->some_model->delete_something($this->get());
// If the id has not been passed via the URL e.g. example/user/:id, then
// check the id query parameter id=? instead
if ($id === NULL)
{
$id = $this->get('id');
}

// Cast as an int
$id = (int) $id;

// If not a valid id
if ($id <= 0)
{
// Set the response and exit
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}

// $this->some_model->delete_something($id);
$message = [
'id' => $this->get('id'),
'id' => $id,
'message' => 'Deleted the resource'
];

Expand All @@ -101,9 +129,9 @@ public function users_get()
else
{
$this->set_response([
'status' => FALSE,
'error' => 'No users were found'
], REST_Controller::NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
'status' => FALSE,
'error' => 'No users were found'
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
}
}
11 changes: 9 additions & 2 deletions application/libraries/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ public function to_xml($data = NULL, $structure = NULL, $basenode = 'xml')
$attributes = get_object_vars($attributes);
}

foreach ($attributes as $attributeName => $attributeValue)
foreach ($attributes as $attribute_name => $attribute_value)
{
$structure->addAttribute($attributeName, $attributeValue);
$structure->addAttribute($attribute_name, $attribute_value);
}
}
// if there is another array found recursively call this function
Expand Down Expand Up @@ -357,6 +357,13 @@ public function to_csv($data = NULL, $delimiter = ',', $enclosure = '"')

foreach ($data as $record)
{
// If the record is not an array, then break. This is because the 2nd param of
// fputcsv() should be an array
if (is_array($record) === FALSE)
{
break;
}

// Returns the length of the string written or FALSE
fputcsv($handle, $record, $delimiter, $enclosure);
}
Expand Down
Loading

0 comments on commit 1c9484a

Please sign in to comment.