Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 921a243

Browse files
author
Jamie Snape
committed
Add restful API for tracker module
1 parent ce13e7b commit 921a243

File tree

7 files changed

+1067
-5
lines changed

7 files changed

+1067
-5
lines changed

core/ApiController.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ protected function _exceptionHandler(Exception $exception)
104104
* => function name in the corresponding ApiComponent. This array must have
105105
* 'default' in its keys.
106106
* @param null|string $moduleName module from which to get the ApiComponent
107+
* @param bool $rpcStyle wrap data in RPC-style "data" and "msg" fields
107108
*
108109
* {@example
109110
* _genericAction(array('id' => 2), 'get', $apiFunctionArray) is called and
@@ -118,7 +119,7 @@ protected function _exceptionHandler(Exception $exception)
118119
* 'itemDuplicate' in ApiComponent (in core module) to do the API work.
119120
* }
120121
*/
121-
protected function _genericAction($args, $resource, $restAction, $apiFunctions, $moduleName = null)
122+
protected function _genericAction($args, $resource, $restAction, $apiFunctions, $moduleName = null, $rpcStyle = true)
122123
{
123124
$ApiComponent = MidasLoader::loadComponent('Api'.$resource, $moduleName);
124125
$httpCode = $this->httpSuccessCode[strtolower($restAction)];
@@ -138,10 +139,14 @@ protected function _genericAction($args, $resource, $restAction, $apiFunctions,
138139
$calledFunction = $calledFunction.'Wrapper';
139140
}
140141
$resultsArray = $ApiComponent->$calledFunction($args, $userDao);
141-
if (isset($resultsArray)) {
142-
$apiResults['data'] = $resultsArray;
143-
} else { // if the api function doesn't provide an return value
144-
$apiResults['msg'] = "succeed!"; // there is no exception if code reaches here
142+
if ($rpcStyle) {
143+
if (isset($resultsArray)) {
144+
$apiResults['data'] = $resultsArray;
145+
} else { // if the api function doesn't provide an return value
146+
$apiResults['msg'] = "succeed!"; // there is no exception if code reaches here
147+
}
148+
} else {
149+
$apiResults = $resultsArray;
145150
}
146151
} catch (Exception $e) {
147152
list($apiResults['error'], $httpCode) = $this->_exceptionHandler($e);
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
/**
22+
* API producer controller for the tracker module.
23+
*
24+
* @package Modules\Tracker\Controller
25+
*/
26+
class Apitracker_ProducerController extends ApiController
27+
{
28+
/** @var string */
29+
public $moduleName = 'tracker';
30+
31+
/** Handle HTTP DELETE requests. Requires an id parameter. */
32+
public function deleteAction()
33+
{
34+
$this->_genericAction(
35+
$this->_request->getParams(),
36+
$this->_request->getControllerName(),
37+
$this->_request->getActionName(),
38+
array('default' => $this->_request->getActionName()),
39+
$this->moduleName,
40+
false
41+
);
42+
}
43+
44+
/** Handle HTTP GET requests. Requires an id parameter. */
45+
public function getAction()
46+
{
47+
$this->_genericAction(
48+
$this->_request->getParams(),
49+
$this->_request->getControllerName(),
50+
$this->_request->getActionName(),
51+
array('default' => $this->_request->getActionName()),
52+
$this->moduleName,
53+
false
54+
);
55+
}
56+
57+
/** Handle HTTP HEAD requests. */
58+
public function headAction()
59+
{
60+
$this->_response->setHttpResponseCode(200); // 200 OK
61+
}
62+
63+
/** Handle HTTP GET index or list requests. */
64+
public function indexAction()
65+
{
66+
$this->_genericAction(
67+
$this->_request->getParams(),
68+
$this->_request->getControllerName(),
69+
$this->_request->getActionName(),
70+
array('default' => $this->_request->getActionName()),
71+
$this->moduleName,
72+
false
73+
);
74+
}
75+
76+
/** Handle HTTP OPTIONS requests. */
77+
public function optionsAction()
78+
{
79+
$this->_response->setHeader('Allow', 'DELETE, GET, HEAD, OPTIONS, POST, PUT');
80+
}
81+
82+
/** Handle HTTP POST requests. */
83+
public function postAction()
84+
{
85+
$this->_genericAction(
86+
$this->_request->getParams(),
87+
$this->_request->getControllerName(),
88+
$this->_request->getActionName(),
89+
array('default' => $this->_request->getActionName()),
90+
$this->moduleName,
91+
false
92+
);
93+
}
94+
95+
/** Handle HTTP PUT requests. Requires an id parameter. */
96+
public function putAction()
97+
{
98+
$this->_genericAction(
99+
$this->_request->getParams(),
100+
$this->_request->getControllerName(),
101+
$this->_request->getActionName(),
102+
array('default' => $this->_request->getActionName()),
103+
$this->moduleName,
104+
false
105+
);
106+
}
107+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
/**
22+
* API scalar controller for the tracker module.
23+
*
24+
* @package Modules\Tracker\Controller
25+
*/
26+
class Apitracker_ScalarController extends ApiController
27+
{
28+
/** @var string */
29+
public $moduleName = 'tracker';
30+
31+
/** Handle HTTP DELETE requests. Requires an id parameter. */
32+
public function deleteAction()
33+
{
34+
$this->_genericAction(
35+
$this->_request->getParams(),
36+
$this->_request->getControllerName(),
37+
$this->_request->getActionName(),
38+
array('default' => $this->_request->getActionName()),
39+
$this->moduleName,
40+
false
41+
);
42+
}
43+
44+
/** Handle HTTP GET requests. Requires an id parameter. */
45+
public function getAction()
46+
{
47+
$this->_genericAction(
48+
$this->_request->getParams(),
49+
$this->_request->getControllerName(),
50+
$this->_request->getActionName(),
51+
array('default' => $this->_request->getActionName()),
52+
$this->moduleName,
53+
false
54+
);
55+
}
56+
57+
/** Handle HTTP HEAD requests. */
58+
public function headAction()
59+
{
60+
$this->_response->setHttpResponseCode(200); // 200 OK
61+
}
62+
63+
/** Handle HTTP GET index or list requests. */
64+
public function indexAction()
65+
{
66+
$this->_genericAction(
67+
$this->_request->getParams(),
68+
$this->_request->getControllerName(),
69+
$this->_request->getActionName(),
70+
array('default' => $this->_request->getActionName()),
71+
$this->moduleName,
72+
false
73+
);
74+
}
75+
76+
/** Handle HTTP OPTIONS requests. */
77+
public function optionsAction()
78+
{
79+
$this->_response->setHeader('Allow', 'DELETE, GET, HEAD, OPTIONS, POST, PUT');
80+
}
81+
82+
/** Handle HTTP POST requests. */
83+
public function postAction()
84+
{
85+
$this->_genericAction(
86+
$this->_request->getParams(),
87+
$this->_request->getControllerName(),
88+
$this->_request->getActionName(),
89+
array('default' => $this->_request->getActionName()),
90+
$this->moduleName,
91+
false
92+
);
93+
}
94+
95+
/** Handle HTTP PUT requests. Requires an id parameter. */
96+
public function putAction()
97+
{
98+
$this->_genericAction(
99+
$this->_request->getParams(),
100+
$this->_request->getControllerName(),
101+
$this->_request->getActionName(),
102+
array('default' => $this->_request->getActionName()),
103+
$this->moduleName,
104+
false
105+
);
106+
}
107+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
/**
22+
* API trend controller for the tracker module.
23+
*
24+
* @package Modules\Tracker\Controller
25+
*/
26+
class Apitracker_TrendController extends ApiController
27+
{
28+
/** @var string */
29+
public $moduleName = 'tracker';
30+
31+
/** Handle HTTP DELETE requests. Requires an id parameter. */
32+
public function deleteAction()
33+
{
34+
$this->_genericAction(
35+
$this->_request->getParams(),
36+
$this->_request->getControllerName(),
37+
$this->_request->getActionName(),
38+
array('default' => $this->_request->getActionName()),
39+
$this->moduleName,
40+
false
41+
);
42+
}
43+
44+
/** Handle HTTP GET requests. Requires an id parameter. */
45+
public function getAction()
46+
{
47+
$this->_genericAction(
48+
$this->_request->getParams(),
49+
$this->_request->getControllerName(),
50+
$this->_request->getActionName(),
51+
array('default' => $this->_request->getActionName()),
52+
$this->moduleName,
53+
false
54+
);
55+
}
56+
57+
/** Handle HTTP HEAD requests. */
58+
public function headAction()
59+
{
60+
$this->_response->setHttpResponseCode(200); // 200 OK
61+
}
62+
63+
/** Handle HTTP GET index or list requests. */
64+
public function indexAction()
65+
{
66+
$this->_genericAction(
67+
$this->_request->getParams(),
68+
$this->_request->getControllerName(),
69+
$this->_request->getActionName(),
70+
array('default' => $this->_request->getActionName()),
71+
$this->moduleName,
72+
false
73+
);
74+
}
75+
76+
/** Handle HTTP OPTIONS requests. */
77+
public function optionsAction()
78+
{
79+
$this->_response->setHeader('Allow', 'DELETE, GET, HEAD, OPTIONS, POST, PUT');
80+
}
81+
82+
/** Handle HTTP POST requests. */
83+
public function postAction()
84+
{
85+
$this->_genericAction(
86+
$this->_request->getParams(),
87+
$this->_request->getControllerName(),
88+
$this->_request->getActionName(),
89+
array('default' => $this->_request->getActionName()),
90+
$this->moduleName,
91+
false
92+
);
93+
}
94+
95+
/** Handle HTTP PUT requests. Requires an id parameter. */
96+
public function putAction()
97+
{
98+
$this->_genericAction(
99+
$this->_request->getParams(),
100+
$this->_request->getControllerName(),
101+
$this->_request->getActionName(),
102+
array('default' => $this->_request->getActionName()),
103+
$this->moduleName,
104+
false
105+
);
106+
}
107+
}

0 commit comments

Comments
 (0)