Mercurial > p > roundup > code
comparison roundup/rest.py @ 5573:89ae4ef34efe REST-rebased
Handle response header
committer: Ralf Schlatterbeck <rsc@runtux.com>
| author | Chau Nguyen <dangchau1991@yahoo.com> |
|---|---|
| date | Wed, 30 Jan 2019 10:26:34 +0100 |
| parents | c4c88466da69 |
| children | 55f97de157e7 |
comparison
equal
deleted
inserted
replaced
| 5572:c4c88466da69 | 5573:89ae4ef34efe |
|---|---|
| 137 item_id = class_obj.create(**props) | 137 item_id = class_obj.create(**props) |
| 138 self.db.commit() | 138 self.db.commit() |
| 139 except (TypeError, IndexError, ValueError), message: | 139 except (TypeError, IndexError, ValueError), message: |
| 140 raise UsageError(message) | 140 raise UsageError(message) |
| 141 | 141 |
| 142 # set the header Location | |
| 143 link = self.base_path + class_name + item_id | |
| 144 self.client.setHeader("Location", link) | |
| 145 | |
| 146 # set the response body | |
| 142 result = { | 147 result = { |
| 143 'id': item_id, | 148 'id': item_id, |
| 144 'link': self.base_path + class_name + item_id | 149 'link': link |
| 145 } | 150 } |
| 146 return 201, result | 151 return 201, result |
| 147 | 152 |
| 148 def post_element(self, class_name, item_id, input): | 153 def post_element(self, class_name, item_id, input): |
| 149 raise Reject('Invalid request') | 154 raise Reject('Invalid request') |
| 222 # PATH is split to multiple pieces | 227 # PATH is split to multiple pieces |
| 223 # 0 - rest | 228 # 0 - rest |
| 224 # 1 - resource | 229 # 1 - resource |
| 225 # 2 - attribute | 230 # 2 - attribute |
| 226 resource_uri = uri.split("/")[1] | 231 resource_uri = uri.split("/")[1] |
| 232 | |
| 233 self.client.setHeader("Access-Control-Allow-Methods", | |
| 234 "HEAD, OPTIONS, GET, POST, PUT, DELETE, PATCH") | |
| 235 self.client.setHeader("Access-Control-Allow-Headers", | |
| 236 "Content-Type, Authorization," | |
| 237 "X-HTTP-Method-Override") | |
| 238 self.client.setHeader("Allow", | |
| 239 "HEAD, OPTIONS, GET, POST, PUT, DELETE, PATCH") | |
| 227 | 240 |
| 228 output = None | 241 output = None |
| 229 try: | 242 try: |
| 230 if resource_uri in self.db.classes: | 243 if resource_uri in self.db.classes: |
| 231 response_code, output = getattr(self, "%s_collection" % method.lower())( | 244 response_code, output = getattr(self, "%s_collection" % method.lower())( |
| 262 | 275 |
| 263 # out to the logfile, it would be nice if the server do it for me | 276 # out to the logfile, it would be nice if the server do it for me |
| 264 print 'EXCEPTION AT', time.ctime() | 277 print 'EXCEPTION AT', time.ctime() |
| 265 traceback.print_exc() | 278 traceback.print_exc() |
| 266 finally: | 279 finally: |
| 280 self.client.setHeader("Content-Type", "application/json") | |
| 267 output = RoundupJSONEncoder().encode(output) | 281 output = RoundupJSONEncoder().encode(output) |
| 268 | 282 |
| 269 print "Length: %s - Content(50 char): %s" % (len(output), output[:50]) | |
| 270 return output | 283 return output |
| 271 | 284 |
| 272 | 285 |
| 273 class RoundupJSONEncoder(json.JSONEncoder): | 286 class RoundupJSONEncoder(json.JSONEncoder): |
| 274 def default(self, obj): | 287 def default(self, obj): |
