Grammalecte  Check-in [bed3dc07b3]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:[server] add Content-Type informations for JSON results
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | server
Files: files | file ages | folders
SHA3-256:bed3dc07b3a7515bb58834cfcf219dbff095abb2698116d9c62f0df9bf2b8383
User & Date: olr 2019-08-02 16:10:12
Context
2019-08-03
08:45
[fr] tests et ajustements check-in: 3def2b0590 user: olr tags: fr, trunk
2019-08-02
16:10
[server] add Content-Type informations for JSON results check-in: bed3dc07b3 user: olr tags: server, trunk
16:00
[fr] tests et ajustements check-in: e61e44ae87 user: olr tags: fr, trunk
Changes

Changes to grammalecte-server.py.

   180    180                  I'm doomed, but you are not. You can get out of here. """
   181    181   
   182    182   @app.route("/get_options/fr")
   183    183   def listOptions ():
   184    184       "returns grammar options in a text JSON format"
   185    185       sUserId = request.cookies.user_id
   186    186       dOptions = dUser[sUserId]["gc_options"]  if sUserId and sUserId in dUser  else oGCE.getOptions()
          187  +    response.set_header("Content-Type", "application/json; charset=UTF-8")
   187    188       return '{ "values": ' + json.dumps(dOptions, ensure_ascii=False) + ', "labels": ' + json.dumps(oGCE.getOptionsLabels("fr"), ensure_ascii=False) + ' }'
   188    189   
   189    190   @app.route("/suggest/fr/<token>")
   190    191   def suggestGet (token):
          192  +    response.set_header("Content-Type", "application/json; charset=UTF-8")
   191    193       try:
   192    194           xFuture = xProcessPoolExecutor.submit(suggest, token)
   193    195           return xFuture.result()
   194    196       except (concurrent.futures.TimeoutError, concurrent.futures.CancelledError):
   195    197           return '{"error": "Analysis aborted (time out or cancelled)"}'
   196    198       except concurrent.futures.BrokenExecutor:
   197    199           return '{"error": "Executor broken. The server failed."}'
................................................................................
   213    215               response.delete_cookie("user_id", path="/")
   214    216       if request.forms.options:
   215    217           try:
   216    218               dUserOptions = dict(oGCE.getOptions())  if not dUserOptions  else dict(dUserOptions)
   217    219               dUserOptions.update(json.loads(request.forms.options))
   218    220           except (TypeError, json.JSONDecodeError):
   219    221               sError = "Request options not used."
          222  +    response.set_header("Content-Type", "application/json; charset=UTF-8")
   220    223       try:
   221    224           xFuture = xProcessPoolExecutor.submit(parseText, request.forms.text, dUserOptions, bool(request.forms.tf), sError)
   222    225           return xFuture.result()
   223    226       except (concurrent.futures.TimeoutError, concurrent.futures.CancelledError):
   224    227           return '{"error": "Analysis aborted (time out or cancelled)"}'
   225    228       except concurrent.futures.BrokenExecutor:
   226    229           return '{"error": "Executor broken. The server failed."}'
   227    230       return '{"error": "Fatal error. The server failed."}'
   228    231   
   229    232   @app.route("/set_options/fr", method="POST")
   230    233   def setOptions ():
   231    234       "set grammar options for current user"
          235  +    response.set_header("Content-Type", "application/json; charset=UTF-8")
   232    236       if request.forms.options:
   233    237           sUserId = request.cookies.user_id  if request.cookies.user_id  else next(userGenerator)
   234    238           dOptions = dUser[sUserId]["gc_options"]  if sUserId in dUser  else dict(oGCE.getOptions())
   235    239           try:
   236    240               dOptions.update(json.loads(request.forms.options))
   237    241               dUser[sUserId] = { "time": int(time.time()), "gc_options": dOptions }
   238    242               response.set_cookie("user_id", sUserId, path="/", max_age=86400) # 24h
................................................................................
   241    245               traceback.print_exc()
   242    246               return '{"error": "Options not registered."}'
   243    247       return '{"error": "No options received."}'
   244    248   
   245    249   @app.route("/reset_options/fr", method="POST")
   246    250   def resetOptions ():
   247    251       "default grammar options"
          252  +    response.set_header("Content-Type", "application/json; charset=UTF-8")
   248    253       if request.cookies.user_id and request.cookies.user_id in dUser:
   249    254           try:
   250    255               del dUser[request.cookies.user_id]
   251    256           except KeyError:
   252    257               return '{"error" : "Unknown user."}'
   253    258       return '{"message" : "Done."}'
   254    259   
................................................................................
   259    264   
   260    265   #@app.route('/static/<filepath:path>')
   261    266   #def server_static (filepath):
   262    267   #    return static_file(filepath, root='./views/static')
   263    268   
   264    269   @app.route("/suggest/fr", method="POST")
   265    270   def suggestPost ():
          271  +    response.set_header("Content-Type", "application/json; charset=UTF-8")
   266    272       try:
   267    273           xFuture = xProcessPoolExecutor.submit(suggest, request.forms.token)
   268    274           return xFuture.result()
   269    275       except (concurrent.futures.TimeoutError, concurrent.futures.CancelledError):
   270    276           return '{"error": "Analysis aborted (time out or cancelled)"}'
   271    277       except concurrent.futures.BrokenExecutor:
   272    278           return '{"error": "Executor broken. The server failed."}'