|
21 | 21 | /** Web Api Controller */
|
22 | 22 | class ApiController extends REST_Controller
|
23 | 23 | {
|
24 |
| - |
25 | 24 | private $httpSuccessCode = array(
|
26 | 25 | 'index' => 200, // 200 OK
|
27 | 26 | 'get' => 200,
|
@@ -73,7 +72,7 @@ protected function _exceptionHandler(Exception $e)
|
73 | 72 | default:
|
74 | 73 | $httpCode = 400; // 400 Bad Request
|
75 | 74 | }
|
76 |
| - return array($errorInfo, $httpCode); |
| 75 | + return array($errorInfo, $httpCode); |
77 | 76 | }
|
78 | 77 |
|
79 | 78 | /**
|
@@ -104,49 +103,49 @@ protected function _exceptionHandler(Exception $e)
|
104 | 103 | * for given url: {base_path}/rest/item/duplicate/2, Midas will call
|
105 | 104 | * 'itemDuplicate' in ApiComponent (in core module) to do the api;
|
106 | 105 | */
|
107 |
| - |
108 | 106 | protected function _genericAction($args, $resource, $restAction, $apiFunctions, $moduleName = null)
|
109 | 107 | {
|
110 |
| - $ApiComponent = MidasLoader::loadComponent('Api'.$resource, $moduleName); |
111 |
| - $httpCode = $this->httpSuccessCode[strtolower($restAction)]; |
112 |
| - $calledFunction = $apiFunctions['default']; |
113 |
| - $apiResults = array(); |
114 |
| - try |
| 108 | + $ApiComponent = MidasLoader::loadComponent('Api'.$resource, $moduleName); |
| 109 | + $httpCode = $this->httpSuccessCode[strtolower($restAction)]; |
| 110 | + $calledFunction = $apiFunctions['default']; |
| 111 | + $apiResults = array(); |
| 112 | + try |
| 113 | + { |
| 114 | + $userDao = $this->_getUser($args); |
| 115 | + if(isset($args['method'])) |
115 | 116 | {
|
116 |
| - $userDao = $this->_getUser($args); |
117 |
| - if(isset($args['method'])) |
| 117 | + $method = strtolower($args['method']); |
| 118 | + if(array_key_exists($method, $apiFunctions)) |
118 | 119 | {
|
119 |
| - $method = strtolower($args['method']); |
120 |
| - if(array_key_exists($method, $apiFunctions)) |
121 |
| - { |
122 |
| - $calledFunction = $apiFunctions[$method]; |
123 |
| - } |
124 |
| - else |
125 |
| - { |
126 |
| - throw new Exception('Server error. Operation ' . $args['method'] . ' is not supported.', -100); |
127 |
| - } |
128 |
| - } |
129 |
| - if(method_exists($ApiComponent, $calledFunction . 'Wrapper')) { |
130 |
| - $calledFunction = $calledFunction . 'Wrapper'; |
131 |
| - } |
132 |
| - $resultsArray = $ApiComponent->$calledFunction($args, $userDao); |
133 |
| - if(isset($resultsArray)) |
134 |
| - { |
135 |
| - $apiResults['data'] = $resultsArray; |
| 120 | + $calledFunction = $apiFunctions[$method]; |
136 | 121 | }
|
137 |
| - else // if the api function doesn't provide an return value |
| 122 | + else |
138 | 123 | {
|
139 |
| - $apiResults['msg'] = "succeed!"; // there is no exception if code reaches here |
| 124 | + throw new Exception('Server error. Operation ' . $args['method'] . ' is not supported.', -100); |
140 | 125 | }
|
141 | 126 | }
|
142 |
| - catch(Exception $e) |
| 127 | + if(method_exists($ApiComponent, $calledFunction . 'Wrapper')) |
143 | 128 | {
|
144 |
| - list($apiResults['error'], $httpCode) = $this->_exceptionHandler($e); |
| 129 | + $calledFunction = $calledFunction . 'Wrapper'; |
145 | 130 | }
|
146 |
| - $this->_response->setHttpResponseCode($httpCode); |
147 |
| - // only the data assigned to '$this->view->apiresults' will be serilized |
148 |
| - // in requested format (json, xml, etc) and filled in response body |
149 |
| - $this->view->apiresults = $apiResults; |
| 131 | + $resultsArray = $ApiComponent->$calledFunction($args, $userDao); |
| 132 | + if(isset($resultsArray)) |
| 133 | + { |
| 134 | + $apiResults['data'] = $resultsArray; |
| 135 | + } |
| 136 | + else // if the api function doesn't provide an return value |
| 137 | + { |
| 138 | + $apiResults['msg'] = "succeed!"; // there is no exception if code reaches here |
| 139 | + } |
| 140 | + } |
| 141 | + catch(Exception $e) |
| 142 | + { |
| 143 | + list($apiResults['error'], $httpCode) = $this->_exceptionHandler($e); |
| 144 | + } |
| 145 | + $this->_response->setHttpResponseCode($httpCode); |
| 146 | + // only the data assigned to '$this->view->apiresults' will be serilized |
| 147 | + // in requested format (json, xml, etc) and filled in response body |
| 148 | + $this->view->apiresults = $apiResults; |
150 | 149 | }
|
151 | 150 |
|
152 | 151 | /**
|
@@ -215,4 +214,4 @@ public function optionsAction()
|
215 | 214 | {
|
216 | 215 | $this->_response->setHeader('Allow', 'OPTIONS');
|
217 | 216 | }
|
218 |
| - } // end class |
| 217 | + } |
0 commit comments