Changeset 2184
- Timestamp:
- Mar 28, 2012, 12:55:20 PM (11 years ago)
- Location:
- trunk/grails-app
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/api/ApiController.groovy
r2181 r2184 1 1 /** 2 * Api ControllerControler2 * Api Controler 3 3 * 4 * Description of my controller 4 * API for third party applications to interact 5 * with GSCF 5 6 * 6 7 * @author your email (+name?) 7 * @since 2010mmdd 8 * @package ??? 8 * @since 20120328ma 9 9 * 10 10 * Revision information: … … 19 19 import dbnp.studycapturing.Study 20 20 import dbnp.authentication.SecUser 21 import org.dbnp.gdt.TemplateFieldType 21 22 22 23 class ApiController { … … 71 72 @Secured(['ROLE_CLIENT', 'ROLE_ADMIN']) 72 73 def getStudies = { 74 println "api::getStudies: ${params}" 75 73 76 String deviceID = (params.containsKey('deviceID')) ? params.deviceID : '' 74 77 String validation = (params.containsKey('validation')) ? params.validation : '' … … 86 89 // get result data 87 90 studies[ studies.size() ] = [ 91 'token' : study.getToken(), 88 92 'title' : study.title, 89 93 'description' : study.description, … … 118 122 } 119 123 } 124 125 @Secured(['ROLE_CLIENT', 'ROLE_ADMIN']) 126 def getSubjectsForStudy = { 127 println "api::getSubjectsForStudy: ${params}" 128 129 String deviceID = (params.containsKey('deviceID')) ? params.deviceID : '' 130 String validation = (params.containsKey('validation')) ? params.validation : '' 131 String studyToken = (params.containsKey('studyToken')) ? params.studyToken : '' 132 133 // fetch user and study 134 def user = authenticationService.getLoggedInUser() 135 def study = Study.findByStudyUUID(studyToken) 136 137 // check 138 if (!apiService.validateRequest(deviceID,validation)) { 139 response.sendError(401, 'Unauthorized') 140 } else if (!study) { 141 response.sendError(400, 'No such study') 142 } else if (!study.canRead(user)) { 143 response.sendError(401, 'Unauthorized') 144 } else { 145 def subjects = [] 146 147 // iterate through subjects 148 study.subjects.each { 149 def fields = it.giveFields() 150 def subject = [:] 151 152 // add subject id 153 subject['id'] = it.id 154 155 // add subject field values 156 fields.each { field -> 157 def value = it.getFieldValue( field.name ) 158 159 if (value.hasProperty('name')) { 160 subject[ field.name ] = value.name 161 } else { 162 subject[ field.name ] = value 163 } 164 } 165 166 subjects[ subjects.size() ] = subject 167 } 168 169 // define result 170 def result = [ 171 'count' : study.subjects.size(), 172 'subjects' : subjects 173 ] 174 175 // set output headers 176 response.status = 200 177 response.contentType = 'application/json;charset=UTF-8' 178 179 if (params.containsKey('callback')) { 180 render "${params.callback}(${result as JSON})" 181 } else { 182 render result as JSON 183 } 184 } 185 } 186 187 188 @Secured(['ROLE_CLIENT', 'ROLE_ADMIN']) 189 def getAssaysForStudy = { 190 println "api::getAssaysForStudy: ${params}" 191 192 String deviceID = (params.containsKey('deviceID')) ? params.deviceID : '' 193 String validation = (params.containsKey('validation')) ? params.validation : '' 194 String studyToken = (params.containsKey('studyToken')) ? params.studyToken : '' 195 196 // fetch user and study 197 def user = authenticationService.getLoggedInUser() 198 def study = Study.findByStudyUUID(studyToken) 199 200 // check 201 if (!apiService.validateRequest(deviceID,validation)) { 202 println "1" 203 response.sendError(401, 'Unauthorized') 204 } else if (!study) { 205 println "2" 206 response.sendError(400, 'No such study') 207 } else if (!study.canRead(user)) { 208 println "3" 209 response.sendError(401, 'Unauthorized') 210 } else { 211 // define result 212 def result = [ 213 // 'count' : study.subjects.size(), 214 // 'subjects' : subjects 215 ] 216 217 // set output headers 218 response.status = 200 219 response.contentType = 'application/json;charset=UTF-8' 220 221 if (params.containsKey('callback')) { 222 render "${params.callback}(${result as JSON})" 223 } else { 224 render result as JSON 225 } 226 } 227 } 120 228 } -
trunk/grails-app/services/api/ApiService.groovy
r2181 r2184 29 29 */ 30 30 def validateRequest(String deviceID, String validation) { 31 return true 32 31 33 // get token for this device ID 32 34 Token token = Token.findByDeviceID(deviceID) -
trunk/grails-app/views/api/index.gsp
r2183 r2184 5 5 <body> 6 6 <h1>API specification</h1> 7 8 The API allows third party software to interface with GSCF and connected modules. 9 10 <h2>prerequisites</h2> 11 <li>a valid username / password</li> 12 <li>the username should be given the role ROLE_CLIENT</li> 13 <li>a shared secret</li> 14 <li>a deviceID / clientID (look <a href="https://github.com/4np/UIDevice-with-UniqueIdentifier-for-iOS-5" target="_new">here</a> for iOS)</li> 15 7 16 <h1>authenticate</h1> 8 17 <p> … … 107 116 </p> 108 117 109 <h1>get AssaysForStudy</h1>118 <h1>getSubjectsForStudy</h1> 110 119 <p> 111 bla 120 Returns the subjects for a particular study 121 122 <h2>Request parameters</h2> 123 <table> 124 <thead> 125 <th>argument</th> 126 <th>type</th> 127 <th>length</th> 128 <th>description</th> 129 <th>example</th> 130 <th>required</th> 131 </thead> 132 <tr> 133 <td>deviceID</td> 134 <td>string</td> 135 <td>36 (max)</td> 136 <td>a unique ID of the client device / application performing the call</td> 137 <td>9ae87836-d38d-4b86-be6a-eff93f2b049a</td> 138 <td>yes</td> 139 </tr> 140 <tr> 141 <td>validation</td> 142 <td>string</td> 143 <td>-</td> 144 <td><a href="http://www.miraclesalad.com/webtools/md5.php" target="_new">md5sum</a>( token + sequence + shared secret )</td> 145 <td>9ae87836d38d4b86be6aeff93f2b049a</td> 146 <td>yes</td> 147 </tr> 148 <tr> 149 <td>studyToken</td> 150 <td>string</td> 151 <td>255</td> 152 <td>study token (see getStudies)</td> 153 <td>b6e0c6f4-d8db-4a43-91fa-a157d2d492f0</td> 154 <td>yes</td> 155 </tr> 156 </table> 157 158 <h2>example reply</h2> 159 <blockquote> 160 {"count":11,"subjects":[{"id":81,"name":"1","species":"Homo sapiens","Gender":"Female","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":82,"name":"2","species":"Homo sapiens","Gender":"Male","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":83,"name":"3","species":"Homo sapiens","Gender":"Female","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":84,"name":"4","species":"Homo sapiens","Gender":"Male","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":85,"name":"5","species":"Homo sapiens","Gender":"Female","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":86,"name":"6","species":"Homo sapiens","Gender":"Male","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":87,"name":"7","species":"Homo sapiens","Gender":"Male","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":88,"name":"8","species":"Homo sapiens","Gender":"Male","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":89,"name":"9","species":"Homo sapiens","Gender":"Male","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":90,"name":"10","species":"Homo sapiens","Gender":"Male","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null},{"id":91,"name":"11","species":"Homo sapiens","Gender":"Female","Age":null,"DOB":null,"Height":null,"Weight":null,"BMI":null,"Race":null,"Waist circumference":null,"Hip circumference":null,"Systolic blood pressure":null,"Diastolic blood pressure":null,"Heart rate":null,"Run-in-food":null}]} 161 </blockquote> 112 162 </p> 113 163 </body>
Note: See TracChangeset
for help on using the changeset viewer.