Mercurial > p > roundup > code
comparison doc/rest.txt @ 5688:1b9ef04b9528
Add docs on how to add new rest endpoints.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 01 Apr 2019 21:53:30 -0400 |
| parents | b8e8b1b3ec77 |
| children | 3e1b66c4e1e2 |
comparison
equal
deleted
inserted
replaced
| 5687:83037aaf3b9d | 5688:1b9ef04b9528 |
|---|---|
| 190 >>> print(r.json()) | 190 >>> print(r.json()) |
| 191 >>> d = {'@op:'action', '@action_name':'restore'} | 191 >>> d = {'@op:'action', '@action_name':'restore'} |
| 192 >>> r = s.patch(u + 'issue/42', data = d, headers = h) | 192 >>> r = s.patch(u + 'issue/42', data = d, headers = h) |
| 193 >>> print(r.json()) | 193 >>> print(r.json()) |
| 194 | 194 |
| 195 Adding new rest endpoints | |
| 196 ========================= | |
| 197 | |
| 198 Add or edit the file interfaces.py at the root of the tracker | |
| 199 directory. | |
| 200 | |
| 201 In that file add (remove indentation): | |
| 202 | |
| 203 from roundup.rest import Routing, RestfulInstance, _data_decorator | |
| 204 | |
| 205 class RestfulInstance: | |
| 206 | |
| 207 @Routing.route("/summary2") | |
| 208 @_data_decorator | |
| 209 def summary2(self, input): | |
| 210 result = { "hello": "world" } | |
| 211 return 200, result | |
| 212 | |
| 213 will make a new endpoint .../rest/summary2 that you can test with: | |
| 214 | |
| 215 $ curl -X GET .../rest/summary2 | |
| 216 { | |
| 217 "data": { | |
| 218 "hello": "world" | |
| 219 } | |
| 220 } | |
| 221 | |
| 222 Similarly appending this to interfaces.py after summary2: | |
| 223 | |
| 224 @Routing.route("/data/<:class_name>/@schema", 'GET') | |
| 225 def get_element_schema(self, class_name, input): | |
| 226 result = { "schema": {} } | |
| 227 uid = self.db.getuid () | |
| 228 if not self.db.security.hasPermission('View', uid, class_name) : | |
| 229 raise Unauthorised('Permission to view %s denied' % class_name) | |
| 230 | |
| 231 class_obj = self.db.getclass(class_name) | |
| 232 props = class_obj.getprops(protected=False) | |
| 233 schema = result['schema'] | |
| 234 | |
| 235 for prop in props: | |
| 236 schema[prop] = { "type": repr(class_obj.properties[prop]) } | |
| 237 | |
| 238 return result | |
| 239 | |
| 240 returns some data about the class | |
| 241 | |
| 242 $ curl -X GET .../rest/data/issue/@schema | |
| 243 { | |
| 244 "schema": { | |
| 245 "keyword": { | |
| 246 "type": "<roundup.hyperdb.Multilink to \"keyword\">" | |
| 247 }, | |
| 248 "title": { | |
| 249 "type": "<roundup.hyperdb.String>" | |
| 250 }, | |
| 251 "files": { | |
| 252 "type": "<roundup.hyperdb.Multilink to \"file\">" | |
| 253 }, | |
| 254 "status": { | |
| 255 "type": "<roundup.hyperdb.Link to \"status\">" | |
| 256 }, ... | |
| 257 } | |
| 258 } | |
| 259 | |
| 260 | |
| 195 Searches and selection | 261 Searches and selection |
| 196 ====================== | 262 ====================== |
| 197 | 263 |
| 198 One difficult interface issue is selection of items from a long list. | 264 One difficult interface issue is selection of items from a long list. |
| 199 Using multi-item selects requires loading a lot of data (e.g. consider | 265 Using multi-item selects requires loading a lot of data (e.g. consider |
