from falcon.util.uri import parse_query_string
import json
from api.actions import pos_tagging

class ApiResource(object):
    def parse_request_data(self, raw_post_data):
        encoded_raw_post_data = ""
        try:
            encoded_raw_post_data = str(raw_post_data, 'utf-8')
        except UnicodeDecodeError:
            try:
                encoded_raw_post_data = str(raw_post_data, 'latin-1')
            except UnicodeDecodeError:
                pass

        return encoded_raw_post_data

    def on_post(self, request, response):
        body = request.stream.read()
        encoded_raw_post_data = self.parse_request_data(body)

        pretty = request.get_param("pretty")
        if not pretty:
            pretty = parse_query_string(encoded_raw_post_data).get("pretty", False)

        data = request.get_param("data")
        if not data:
            data = parse_query_string(encoded_raw_post_data).get("data", False)
            if not data:
                data = encoded_raw_post_data

        if not data:
            return {"error": "No data posted or data incorrectly encoded"}

        tagged_json = pos_tagging(data)

        json_kwargs = {
            "separators": (',', ':'),
            "ensure_ascii": False,
        }
        if pretty:
            json_kwargs["indent"] = 4
            json_kwargs["separators"] = (', ', ': ')

        response.body = json.dumps(tagged_json, **json_kwargs)

from falcon.util.uri import parse_query_string
import json
from api.actions import pos_tagging

class ApiResource(object):
    def parse_request_data(self, raw_post_data):
        encoded_raw_post_data = ""
        try:
            encoded_raw_post_data = str(raw_post_data, 'utf-8')
        except UnicodeDecodeError:
            try:
                encoded_raw_post_data = str(raw_post_data, 'latin-1')
            except UnicodeDecodeError:
                pass

        return encoded_raw_post_data

    def on_post(self, request, response):
        body = request.stream.read()
        encoded_raw_post_data = self.parse_request_data(body)

        pretty = request.get_param("pretty")
        if not pretty:
            pretty = parse_query_string(encoded_raw_post_data).get("pretty", False)

        data = request.get_param("data")
        if not data:
            data = parse_query_string(encoded_raw_post_data).get("data", False)
            if not data:
                data = encoded_raw_post_data

        if not data:
            return {"error": "No data posted or data incorrectly encoded"}

        tagged_json = pos_tagging(data)

        json_kwargs = {"separators": (',', ':')}
        if pretty:
            json_kwargs = {"indent": 4, "separators": (', ', ': ')}

        response.body = json.dumps(tagged_json, **json_kwargs)

Revert "Return utf-8, not ascii."
Revert "Return utf-8, not ascii."

This reverts commit 86cbefc74471e4c991c96e0385b931a2a20f5d50.


Former-commit-id: 3246e0bfefb806bd2b4d3dda0cb77e91f3481971
  from falcon.util.uri import parse_query_string
  import json
  from api.actions import pos_tagging
  
  class ApiResource(object):
      def parse_request_data(self, raw_post_data):
          encoded_raw_post_data = ""
          try:
              encoded_raw_post_data = str(raw_post_data, 'utf-8')
          except UnicodeDecodeError:
              try:
                  encoded_raw_post_data = str(raw_post_data, 'latin-1')
              except UnicodeDecodeError:
                  pass
  
          return encoded_raw_post_data
  
      def on_post(self, request, response):
          body = request.stream.read()
          encoded_raw_post_data = self.parse_request_data(body)
  
          pretty = request.get_param("pretty")
          if not pretty:
              pretty = parse_query_string(encoded_raw_post_data).get("pretty", False)
  
          data = request.get_param("data")
          if not data:
              data = parse_query_string(encoded_raw_post_data).get("data", False)
              if not data:
                  data = encoded_raw_post_data
  
          if not data:
              return {"error": "No data posted or data incorrectly encoded"}
  
          tagged_json = pos_tagging(data)
  
-         json_kwargs = {
-             "separators": (',', ':'),
+         json_kwargs = {"separators": (',', ':')}
-             "ensure_ascii": False,
-         }
          if pretty:
-             json_kwargs["indent"] = 4
-             json_kwargs["separators"] = (', ', ': ')
+             json_kwargs = {"indent": 4, "separators": (', ', ': ')}
  
          response.body = json.dumps(tagged_json, **json_kwargs)
  
Revert "Return utf-8, not ascii."
## Code Before:
from falcon.util.uri import parse_query_string
import json
from api.actions import pos_tagging

class ApiResource(object):
    def parse_request_data(self, raw_post_data):
        encoded_raw_post_data = ""
        try:
            encoded_raw_post_data = str(raw_post_data, 'utf-8')
        except UnicodeDecodeError:
            try:
                encoded_raw_post_data = str(raw_post_data, 'latin-1')
            except UnicodeDecodeError:
                pass

        return encoded_raw_post_data

    def on_post(self, request, response):
        body = request.stream.read()
        encoded_raw_post_data = self.parse_request_data(body)

        pretty = request.get_param("pretty")
        if not pretty:
            pretty = parse_query_string(encoded_raw_post_data).get("pretty", False)

        data = request.get_param("data")
        if not data:
            data = parse_query_string(encoded_raw_post_data).get("data", False)
            if not data:
                data = encoded_raw_post_data

        if not data:
            return {"error": "No data posted or data incorrectly encoded"}

        tagged_json = pos_tagging(data)

        json_kwargs = {
            "separators": (',', ':'),
            "ensure_ascii": False,
        }
        if pretty:
            json_kwargs["indent"] = 4
            json_kwargs["separators"] = (', ', ': ')

        response.body = json.dumps(tagged_json, **json_kwargs)

## Instruction:
Revert "Return utf-8, not ascii."
## Code After:
from falcon.util.uri import parse_query_string
import json
from api.actions import pos_tagging

class ApiResource(object):
    def parse_request_data(self, raw_post_data):
        encoded_raw_post_data = ""
        try:
            encoded_raw_post_data = str(raw_post_data, 'utf-8')
        except UnicodeDecodeError:
            try:
                encoded_raw_post_data = str(raw_post_data, 'latin-1')
            except UnicodeDecodeError:
                pass

        return encoded_raw_post_data

    def on_post(self, request, response):
        body = request.stream.read()
        encoded_raw_post_data = self.parse_request_data(body)

        pretty = request.get_param("pretty")
        if not pretty:
            pretty = parse_query_string(encoded_raw_post_data).get("pretty", False)

        data = request.get_param("data")
        if not data:
            data = parse_query_string(encoded_raw_post_data).get("data", False)
            if not data:
                data = encoded_raw_post_data

        if not data:
            return {"error": "No data posted or data incorrectly encoded"}

        tagged_json = pos_tagging(data)

        json_kwargs = {"separators": (',', ':')}
        if pretty:
            json_kwargs = {"indent": 4, "separators": (', ', ': ')}

        response.body = json.dumps(tagged_json, **json_kwargs)
