Skip to content

Commit 1c9484a

Browse files
Merge pull request #513 from softwarespot/master
Fixed missing semi-colon and implemented #510
2 parents 13c2070 + 805b82c commit 1c9484a

File tree

4 files changed

+180
-78
lines changed

4 files changed

+180
-78
lines changed

application/config/rest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@
270270
| `id` INT(11) NOT NULL AUTO_INCREMENT,
271271
| `key` VARCHAR(40) NOT NULL,
272272
| `level` INT(2) NOT NULL,
273-
| `ignore_limits` TINY(1) NOT NULL DEFAULT '0',
273+
| `ignore_limits` TINYINT(1) NOT NULL DEFAULT '0',
274274
| `is_private_key` TINYINT(1) NOT NULL DEFAULT '0',
275275
| `ip_addresses` TEXT NULL DEFAULT NULL,
276276
| `date_created` INT(11) NOT NULL,
@@ -291,6 +291,21 @@
291291
*/
292292
$config['rest_key_column'] = 'key';
293293

294+
/*
295+
|--------------------------------------------------------------------------
296+
| REST API Limits method
297+
|--------------------------------------------------------------------------
298+
|
299+
| Specify the method used to limit the API calls
300+
|
301+
| Available methods are :
302+
| $config['rest_limits_method'] = 'API_KEY'; // Put a limit per api key
303+
| $config['rest_limits_method'] = 'METHOD_NAME'; // Put a limit on method calls
304+
| $config['rest_limits_method'] = 'ROUTED_URL'; // Put a limit on the routed URL
305+
|
306+
*/
307+
$config['rest_limits_method'] = 'ROUTED_URL';
308+
294309
/*
295310
|--------------------------------------------------------------------------
296311
| REST Key Length

application/controllers/api/Example.php

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,34 @@ function __construct()
3030
$this->methods['user_delete']['limit'] = 50; // 50 requests per hour per user/key
3131
}
3232

33-
public function user_get()
33+
public function user_get($id = NULL)
3434
{
35-
if (!$this->get('id'))
35+
// If the id has not been passed via the URL e.g. example/user/:id, then
36+
// check the id query parameter id=? instead
37+
if ($id === NULL)
38+
{
39+
$id = $this->get('id');
40+
}
41+
42+
// Cast as an int
43+
$id = (int) $id;
44+
45+
// If not a valid id
46+
if ($id <= 0)
3647
{
3748
// Set the response and exit
3849
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
3950
}
4051

41-
// $user = $this->some_model->getSomething($this->get('id'));
52+
// $user = $this->some_model->getSomething($id);
4253
$users = [
4354
1 => ['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'],
4455
2 => ['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'],
4556
3 => ['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
4657
];
4758

4859
// Get the user from the array, by retrieving the id from the GET request
49-
$user = isset($users[$this->get('id')]) ? $users[$this->get('id')] : NULL;
60+
$user = isset($users[$id]) ? $users[$id] : NULL;
5061

5162
if ($user)
5263
{
@@ -57,15 +68,15 @@ public function user_get()
5768
$this->set_response([
5869
'status' => FALSE,
5970
'error' => 'User could not be found'
60-
], REST_Controller::NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
71+
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
6172
}
6273
}
6374

6475
public function user_post()
6576
{
66-
// $this->some_model->update_user($this->get('id'));
77+
// $this->some_model->update_user( ... );
6778
$message = [
68-
'id' => $this->get('id'),
79+
'id' => 100, // Automatically generated by the model
6980
'name' => $this->post('name'),
7081
'email' => $this->post('email'),
7182
'message' => 'Added a resource'
@@ -76,9 +87,26 @@ public function user_post()
7687

7788
public function user_delete()
7889
{
79-
// $this->some_model->delete_something($this->get());
90+
// If the id has not been passed via the URL e.g. example/user/:id, then
91+
// check the id query parameter id=? instead
92+
if ($id === NULL)
93+
{
94+
$id = $this->get('id');
95+
}
96+
97+
// Cast as an int
98+
$id = (int) $id;
99+
100+
// If not a valid id
101+
if ($id <= 0)
102+
{
103+
// Set the response and exit
104+
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
105+
}
106+
107+
// $this->some_model->delete_something($id);
80108
$message = [
81-
'id' => $this->get('id'),
109+
'id' => $id,
82110
'message' => 'Deleted the resource'
83111
];
84112

@@ -101,9 +129,9 @@ public function users_get()
101129
else
102130
{
103131
$this->set_response([
104-
'status' => FALSE,
105-
'error' => 'No users were found'
106-
], REST_Controller::NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
132+
'status' => FALSE,
133+
'error' => 'No users were found'
134+
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
107135
}
108136
}
109137
}

application/libraries/Format.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,9 @@ public function to_xml($data = NULL, $structure = NULL, $basenode = 'xml')
222222
$attributes = get_object_vars($attributes);
223223
}
224224

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

358358
foreach ($data as $record)
359359
{
360+
// If the record is not an array, then break. This is because the 2nd param of
361+
// fputcsv() should be an array
362+
if (is_array($record) === FALSE)
363+
{
364+
break;
365+
}
366+
360367
// Returns the length of the string written or FALSE
361368
fputcsv($handle, $record, $delimiter, $enclosure);
362369
}

0 commit comments

Comments
 (0)