Mercurial > p > roundup > code
comparison roundup/rest.py @ 5577:11e75fbb9edc REST-rebased
Added pretty print
committer: Ralf Schlatterbeck <rsc@runtux.com>
| author | Chau Nguyen <dangchau1991@yahoo.com> |
|---|---|
| date | Wed, 30 Jan 2019 10:26:34 +0100 |
| parents | 77d11a24f718 |
| children | c2214d0c9df8 |
comparison
equal
deleted
inserted
replaced
| 5576:77d11a24f718 | 5577:11e75fbb9edc |
|---|---|
| 252 | 252 |
| 253 # format_header need a priority parser | 253 # format_header need a priority parser |
| 254 format_ext = os.path.splitext(urlparse.urlparse(uri).path)[1][1:] | 254 format_ext = os.path.splitext(urlparse.urlparse(uri).path)[1][1:] |
| 255 format_header = self.client.request.headers.getheader('Accept')[12:] | 255 format_header = self.client.request.headers.getheader('Accept')[12:] |
| 256 format_output = format_ext or format_header or "json" | 256 format_output = format_ext or format_header or "json" |
| 257 | |
| 258 # check for pretty print | |
| 259 try: | |
| 260 pretty_output = input['pretty'].value.lower() == "true" | |
| 261 except KeyError: | |
| 262 pretty_output = False | |
| 257 | 263 |
| 258 self.client.setHeader("Access-Control-Allow-Origin", "*") | 264 self.client.setHeader("Access-Control-Allow-Origin", "*") |
| 259 self.client.setHeader("Access-Control-Allow-Headers", | 265 self.client.setHeader("Access-Control-Allow-Headers", |
| 260 "Content-Type, Authorization, " | 266 "Content-Type, Authorization, " |
| 261 "X-HTTP-Method-Override") | 267 "X-HTTP-Method-Override") |
| 310 print 'EXCEPTION AT', time.ctime() | 316 print 'EXCEPTION AT', time.ctime() |
| 311 traceback.print_exc() | 317 traceback.print_exc() |
| 312 finally: | 318 finally: |
| 313 if format_output.lower() == "json": | 319 if format_output.lower() == "json": |
| 314 self.client.setHeader("Content-Type", "application/json") | 320 self.client.setHeader("Content-Type", "application/json") |
| 315 output = RoundupJSONEncoder().encode(output) | 321 if pretty_output: |
| 322 indent = 4 | |
| 323 else: | |
| 324 indent = None | |
| 325 output = RoundupJSONEncoder(indent=indent).encode(output) | |
| 316 else: | 326 else: |
| 317 self.client.response_code = 406 | 327 self.client.response_code = 406 |
| 318 output = "" | 328 output = "" |
| 319 | 329 |
| 320 return output | 330 return output |
