Skip to content

Commit

Permalink
add limit configuration #109
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuNls committed Feb 8, 2016
1 parent 5461c0e commit b34769b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
7 changes: 4 additions & 3 deletions application/config/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,9 @@
| `uri` VARCHAR(255) NOT NULL,
| `count` INT(10) NOT NULL,
| `hour_started` INT(11) NOT NULL,
| `api_key` VARCHAR(40) NOT NULL,
| PRIMARY KEY (`id`)
| `api_key_id` INT(11) NOT NULL,
| PRIMARY KEY (`id`),
| FOREIGN KEY (`api_key_id`) REFERENCES `keys`(`id`) ON DELETE CASCADE
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
| To specify the limits within the controller's __construct() method, add per-method
Expand All @@ -462,7 +463,7 @@
|
| See application/controllers/api/example.php for examples
*/
$config['rest_enable_limits'] = FALSE;
$config['rest_enable_limits'] = TRUE;

/*
|--------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion application/controllers/api/Users_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Users_api extends REST_Controller {
protected $methods = [
'index_put' => ['key' => false],
'index_post' => ['key' => false],
'index_delete' => ['key' => true]
'index_delete' => ['key' => true, 'limit' => 20]
];

/**
Expand Down
8 changes: 4 additions & 4 deletions application/libraries/REST_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ protected function _check_limit($controller_method)
// Get data about a keys' usage and limit to one row
$result = $this->rest->db
->where('uri', $limited_uri)
->where('api_key', $this->rest->key)
->where('api_key_id', $this->rest->key_id)
->get($this->config->item('rest_limits_table'))
->row();

Expand All @@ -1167,7 +1167,7 @@ protected function _check_limit($controller_method)
// Create a new row for the following key
$this->rest->db->insert($this->config->item('rest_limits_table'), [
'uri' => $limited_uri,
'api_key' => isset($this->rest->key) ? $this->rest->key : '',
'api_key_id' => isset($this->rest->key_id) ? $this->rest->key_id : '',
'count' => 1,
'hour_started' => time()
]);
Expand All @@ -1179,7 +1179,7 @@ protected function _check_limit($controller_method)
// Reset the started period and count
$this->rest->db
->where('uri', $limited_uri)
->where('api_key', isset($this->rest->key) ? $this->rest->key : '')
->where('api_key_id', isset($this->rest->key_id) ? $this->rest->key_id : '')
->set('hour_started', time())
->set('count', 1)
->update($this->config->item('rest_limits_table'));
Expand All @@ -1197,7 +1197,7 @@ protected function _check_limit($controller_method)
// Increase the count by one
$this->rest->db
->where('uri', $limited_uri)
->where('api_key', $this->rest->key)
->where('api_key_id', $this->rest->key_id)
->set('count', 'count + 1', FALSE)
->update($this->config->item('rest_limits_table'));
}
Expand Down

0 comments on commit b34769b

Please sign in to comment.