Skip to content

Commit 48e90d9

Browse files
committed
1. API return function
2. set custom data in api 3. set config file api_keys
1 parent 0ba60b2 commit 48e90d9

File tree

3 files changed

+118
-43
lines changed

3 files changed

+118
-43
lines changed

application/controllers/Api_test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function get_users()
2626
* type :: ['header', 'get', 'post']
2727
* key :: ['table : Check Key in Database', 'key']
2828
*/
29-
'key' => ['GET'], // type, {key}|table (by default)
29+
// 'key' => ['GET'], // type, {key}|table (by default)
3030
]);
3131

3232

application/libraries/API_Controller.php

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,48 @@ class API_Controller extends CI_Controller
2525
* @link http://www.restapitutorial.com/httpstatuscodes.html
2626
*/
2727
const HTTP_METHOD_NOT_ALLOWED = 405;
28-
const STR_METHOD_NOT_ALLOWED = 'HTTP/1.1 405 Method Not Allowed';
2928

3029
/**
3130
* The request cannot be fulfilled due to multiple errors
3231
*/
3332
const HTTP_BAD_REQUEST = 400;
34-
const STR_BAD_REQUEST = 'BAD REQUEST';
3533

3634
/**
3735
* Request Timeout
3836
*/
3937
const HTTP_REQUEST_TIMEOUT = 408;
40-
const STR_REQUEST_TIMEOUT = 'Request Timeout';
4138

4239
/**
4340
* The requested resource could not be found
4441
*/
4542
const HTTP_NOT_FOUND = 404;
46-
const STR_NOT_FOUND = 'NOT FOUND';
4743

4844
/**
4945
* The user is unauthorized to access the requested resource
5046
*/
5147
const HTTP_UNAUTHORIZED = 401;
52-
const STR_UNAUTHORIZED = 'UNAUTHORIZED';
5348

49+
/**
50+
* The request has succeeded
51+
*/
52+
const HTTP_OK = 200;
53+
54+
/**
55+
* HTTP status codes and their respective description
56+
*/
57+
const HEADER_STATUS_STRINGS = [
58+
'405' => 'HTTP/1.1 405 Method Not Allowed',
59+
'400' => 'BAD REQUEST',
60+
'408' => 'Request Timeout',
61+
'404' => 'NOT FOUND',
62+
'401' => 'UNAUTHORIZED',
63+
'200' => 'OK',
64+
];
5465

5566
const API_LIMIT_TABLE_NAME = 'api_limit';
5667
const API_KEYS_TABLE_NAME = 'api_keys';
68+
69+
public $return_other_data = [];
5770

5871
public function __construct() {
5972
parent::__construct();
@@ -65,8 +78,12 @@ public function __construct() {
6578
}
6679

6780

68-
public function _apiConfig($config = [])
81+
public function _APIConfig($config = [])
6982
{
83+
// return other data
84+
if(isset($config['data']))
85+
$this->return_other_data = $config['data'];
86+
7087
// by default method `GET`
7188
if ((isset($config) AND empty($config)) OR empty($config['methods'])) {
7289
$this->_allow_methods(['GET']);
@@ -81,8 +98,6 @@ public function _apiConfig($config = [])
8198
// api key function `_api_key()`
8299
if(isset($config['key']))
83100
$this->_api_key($config['key']);
84-
85-
// print_r($config);
86101
}
87102

88103

@@ -107,10 +122,10 @@ public function _allow_methods(array $methods)
107122
} else
108123
{
109124
// not allow request method
110-
$this->response(['status' => FALSE, 'error' => 'Unknown method'], self::HTTP_METHOD_NOT_ALLOWED, self::STR_METHOD_NOT_ALLOWED);
125+
$this->_response(['status' => FALSE, 'error' => 'Unknown method'], self::HTTP_METHOD_NOT_ALLOWED);
111126
}
112127
} else {
113-
$this->response(['status' => FALSE, 'error' => 'Unknown method'], self::HTTP_METHOD_NOT_ALLOWED, self::STR_METHOD_NOT_ALLOWED);
128+
$this->_response(['status' => FALSE, 'error' => 'Unknown method'], self::HTTP_METHOD_NOT_ALLOWED);
114129
}
115130
}
116131

@@ -132,17 +147,17 @@ public function _limit_method(array $data)
132147
{
133148
// check limit number
134149
if (!isset($data[0])) {
135-
$this->response(['status' => FALSE, 'error' => 'Limit Number Required'], self::HTTP_BAD_REQUEST, self::STR_BAD_REQUEST);
150+
$this->_response(['status' => FALSE, 'error' => 'Limit Number Required'], self::HTTP_BAD_REQUEST);
136151
}
137152

138153
// check limit type
139154
if (!isset($data[1])) {
140-
$this->response(['status' => FALSE, 'error' => 'Limit Type Required'], self::HTTP_BAD_REQUEST, self::STR_BAD_REQUEST);
155+
$this->_response(['status' => FALSE, 'error' => 'Limit Type Required'], self::HTTP_BAD_REQUEST);
141156
}
142157

143158
// check limit database table exists
144159
if (!$this->db->table_exists(self::API_LIMIT_TABLE_NAME)) {
145-
$this->response(['status' => FALSE, 'error' => 'Create Limit Database Table'], self::HTTP_BAD_REQUEST, self::STR_BAD_REQUEST);
160+
$this->_response(['status' => FALSE, 'error' => 'Create Limit Database Table'], self::HTTP_BAD_REQUEST);
146161
}
147162

148163
$limit_num = $data[0]; // limit number
@@ -183,7 +198,7 @@ public function _limit_method(array $data)
183198
// echo $this->CI->db->last_query();
184199
if ($this->db->affected_rows() >= $limit_num)
185200
{
186-
$this->response(['status' => FALSE, 'error' => 'This IP Address has reached the time limit for this method'], self::HTTP_REQUEST_TIMEOUT, self::STR_REQUEST_TIMEOUT);
201+
$this->_response(['status' => FALSE, 'error' => 'This IP Address has reached the time limit for this method'], self::HTTP_REQUEST_TIMEOUT);
187202
} else
188203
{
189204
// insert limit data
@@ -217,7 +232,7 @@ public function _limit_method(array $data)
217232
// echo $this->CI->db->last_query();exit;
218233
if ($this->db->affected_rows() >= $limit_num)
219234
{
220-
$this->response(['status' => FALSE, 'error' => 'This IP Address has reached the time limit for this method'], self::HTTP_REQUEST_TIMEOUT, self::STR_REQUEST_TIMEOUT);
235+
$this->_response(['status' => FALSE, 'error' => 'This IP Address has reached the time limit for this method'], self::HTTP_REQUEST_TIMEOUT);
221236
} else
222237
{
223238
// insert limit data
@@ -226,7 +241,7 @@ public function _limit_method(array $data)
226241
}
227242

228243
} else {
229-
$this->response(['status' => FALSE, 'error' => 'This IP Address has reached limit for this method'], self::HTTP_REQUEST_TIMEOUT, self::STR_REQUEST_TIMEOUT);
244+
$this->_response(['status' => FALSE, 'error' => 'This IP Address has reached limit for this method'], self::HTTP_REQUEST_TIMEOUT);
230245
}
231246

232247
} else
@@ -235,7 +250,7 @@ public function _limit_method(array $data)
235250
$this->limit_data_insert();
236251
}
237252
} else {
238-
$this->response(['status' => FALSE, 'error' => 'Limit Type Invalid'], self::HTTP_BAD_REQUEST, self::STR_BAD_REQUEST);
253+
$this->_response(['status' => FALSE, 'error' => 'Limit Type Invalid'], self::HTTP_BAD_REQUEST);
239254
}
240255
}
241256

@@ -289,14 +304,14 @@ private function _api_key(array $key)
289304
if ($api_key != "table")
290305
{
291306
if ($HEADER_VALUE != $api_key) {
292-
$this->response(['status' => FALSE, 'error' => 'API Key invalid'], self::HTTP_UNAUTHORIZED, self::STR_UNAUTHORIZED);
307+
$this->_response(['status' => FALSE, 'error' => 'API Key invalid'], self::HTTP_UNAUTHORIZED);
293308
}
294309

295310
} else
296311
{
297312
// check api key database table exists
298313
if (!$this->db->table_exists(self::API_KEYS_TABLE_NAME)) {
299-
$this->response(['status' => FALSE, 'error' => 'Create API Key Database Table'], self::HTTP_BAD_REQUEST, self::STR_BAD_REQUEST);
314+
$this->_response(['status' => FALSE, 'error' => 'Create API Key Database Table'], self::HTTP_BAD_REQUEST);
300315
}
301316

302317
$where_key_data = [
@@ -307,13 +322,13 @@ private function _api_key(array $key)
307322
$limit_query = $this->CI->db->get_where(self::API_KEYS_TABLE_NAME, $where_key_data);
308323
if (!$this->db->affected_rows() > 0)
309324
{
310-
$this->response(['status' => FALSE, 'error' => 'API Key invalid'], self::HTTP_NOT_FOUND, self::STR_NOT_FOUND);
325+
$this->_response(['status' => FALSE, 'error' => 'API Key invalid'], self::HTTP_NOT_FOUND);
311326
}
312327
}
313328

314329
} else
315330
{
316-
$this->response(['status' => FALSE, 'error' => 'API Key Header Required'], self::HTTP_NOT_FOUND, self::STR_NOT_FOUND);
331+
$this->_response(['status' => FALSE, 'error' => 'API Key Header Required'], self::HTTP_NOT_FOUND);
317332
}
318333
} else if (strtolower($api_key_type) == 'get') // // api key type `get`
319334
{
@@ -336,14 +351,14 @@ private function _api_key(array $key)
336351
if ($api_key != "table")
337352
{
338353
if ($get_param_value != $api_key) {
339-
$this->response(['status' => FALSE, 'error' => 'API Key invalid'], self::HTTP_UNAUTHORIZED, self::STR_UNAUTHORIZED);
354+
$this->_response(['status' => FALSE, 'error' => 'API Key invalid'], self::HTTP_UNAUTHORIZED);
340355
}
341356

342357
} else
343358
{
344359
// check api key database table exists
345360
if (!$this->db->table_exists(self::API_KEYS_TABLE_NAME)) {
346-
$this->response(['status' => FALSE, 'error' => 'Create API Key Database Table'], self::HTTP_BAD_REQUEST, self::STR_BAD_REQUEST);
361+
$this->_response(['status' => FALSE, 'error' => 'Create API Key Database Table'], self::HTTP_BAD_REQUEST);
347362
}
348363

349364
$where_key_data = [
@@ -354,12 +369,12 @@ private function _api_key(array $key)
354369
$limit_query = $this->CI->db->get_where(self::API_KEYS_TABLE_NAME, $where_key_data);
355370
if (!$this->db->affected_rows() > 0)
356371
{
357-
$this->response(['status' => FALSE, 'error' => 'API Key invalid'], self::HTTP_NOT_FOUND, self::STR_NOT_FOUND);
372+
$this->_response(['status' => FALSE, 'error' => 'API Key invalid'], self::HTTP_NOT_FOUND);
358373
}
359374
}
360375
} else
361376
{
362-
$this->response(['status' => FALSE, 'error' => 'API Key GET Parameter Required'], self::HTTP_NOT_FOUND, self::STR_NOT_FOUND);
377+
$this->_response(['status' => FALSE, 'error' => 'API Key GET Parameter Required'], self::HTTP_NOT_FOUND);
363378
}
364379
} else if (strtolower($api_key_type) == 'post') // // api key type `post`
365380
{
@@ -382,14 +397,14 @@ private function _api_key(array $key)
382397
if ($api_key != "table")
383398
{
384399
if ($get_param_value != $api_key) {
385-
$this->response(['status' => FALSE, 'error' => 'API Key invalid'], self::HTTP_UNAUTHORIZED, self::STR_UNAUTHORIZED);
400+
$this->_response(['status' => FALSE, 'error' => 'API Key invalid'], self::HTTP_UNAUTHORIZED);
386401
}
387402

388403
} else
389404
{
390405
// check api key database table exists
391406
if (!$this->db->table_exists(self::API_KEYS_TABLE_NAME)) {
392-
$this->response(['status' => FALSE, 'error' => 'Create API Key Database Table'], self::HTTP_BAD_REQUEST, self::STR_BAD_REQUEST);
407+
$this->_response(['status' => FALSE, 'error' => 'Create API Key Database Table'], self::HTTP_BAD_REQUEST);
393408
}
394409

395410
$where_key_data = [
@@ -400,16 +415,16 @@ private function _api_key(array $key)
400415
$limit_query = $this->CI->db->get_where(self::API_KEYS_TABLE_NAME, $where_key_data);
401416
if (!$this->db->affected_rows() > 0)
402417
{
403-
$this->response(['status' => FALSE, 'error' => 'API Key invalid'], self::HTTP_NOT_FOUND, self::STR_NOT_FOUND);
418+
$this->_response(['status' => FALSE, 'error' => 'API Key invalid'], self::HTTP_NOT_FOUND);
404419
}
405420
}
406421
} else
407422
{
408-
$this->response(['status' => FALSE, 'error' => 'API Key POST Parameter Required'], self::HTTP_NOT_FOUND, self::STR_NOT_FOUND);
423+
$this->_response(['status' => FALSE, 'error' => 'API Key POST Parameter Required'], self::HTTP_NOT_FOUND);
409424
}
410425

411426
} else {
412-
$this->response(['status' => FALSE, 'error' => 'API Key Parameter Required'], self::HTTP_NOT_FOUND, self::STR_NOT_FOUND);
427+
$this->_response(['status' => FALSE, 'error' => 'API Key Parameter Required'], self::HTTP_NOT_FOUND);
413428
}
414429
}
415430

@@ -428,17 +443,33 @@ private function exists_header($header_name)
428443
}
429444
}
430445

431-
432-
public function response($data = NULL, $http_code = NULL, $http_string = NULL)
446+
/**
447+
* Private Response Function
448+
*/
449+
private function _response($data = NULL, $http_code = NULL)
433450
{
434451
ob_start();
435-
436-
header($http_string, true, $http_code);
437452
header('content-type:application/json; charset=UTF-8');
438-
439-
print_r(json_encode($data));
453+
header(self::HEADER_STATUS_STRINGS[$http_code], true, $http_code);
454+
455+
if (!is_array($this->return_other_data)) {
456+
print_r(json_encode(['status' => false, 'error' => 'Invalid data format']));
457+
} else {
458+
print_r(json_encode(array_merge($data, $this->return_other_data)));
459+
}
460+
ob_end_flush();
440461
die();
462+
}
441463

464+
/**
465+
* Public Response Function
466+
*/
467+
public function api_return($data = NULL, $http_code = NULL)
468+
{
469+
ob_start();
470+
header('content-type:application/json; charset=UTF-8');
471+
header(self::HEADER_STATUS_STRINGS[$http_code], true, $http_code);
472+
print_r(json_encode($data));
442473
ob_end_flush();
443474
}
444475
}

0 commit comments

Comments
 (0)