def generate_error_response(self, filename, solved_url):
     # We got an error, so we'll copy an error message image to the GCS file
     # indicated to contain the result.
     utils.copy_error_image(filename)
     # respond to the client, indicating the URL to which the result has
     # been written.
     resp = {'status': 'ERROR', 'solved_url' : solved_url}
     return json.dumps(resp)
 def generate_error_response(self, filename, solved_url):
     # We got an error, so we'll copy an error message image to the GCS file
     # indicated to contain the result.
     utils.copy_error_image(filename)
     # respond to the client, indicating the URL to which the result has
     # been written.
     resp = {'status': 'ERROR', 'solved_url': solved_url}
     return json.dumps(resp)
    def post(self):
        """Parse and solve the given sudoku puzzle image, and write the result to a known GCS
        file.
        """

        image_url = self.request.get('image_url')
        filename = self.request.get('filename')
        image_data = None
        try:
            if image_url:
                logging.info("image url: %s", image_url)
                result = urlfetch.fetch(image_url)
                if result.status_code == 200:
                    image_data = result.content
            else:
                logging.info('did not get image url...')
        except:
            logging.exception("issue fetching url data")
        if not image_data:
            logging.info("no image data")
            utils.copy_error_image(filename)
            return

        self.parser = sudoku_image_parser.SudokuImageParser()
        stringified_puzzle = ''
        try:
            stringified_puzzle = self.parser.parse(image_data)
            logging.info("stringified puzzle: %s", stringified_puzzle)
        except (IndexError, sudoku_image_parser.ImageError) as e:
            logging.debug(e)
            utils.copy_error_image(filename)
            return
        try:
            image_solution = self._solved_puzzle_image(stringified_puzzle)
            gcs_file = utils.create_jpg_file(filename,
                                             image_solution.tostring())
            logging.debug("url: %s%s", self.api_url, gcs_file)
            return
        except (sudoku_solver.ContradictionError, ValueError) as e:
            logging.debug(e)
            utils.copy_error_image(filename)
            return
    def post(self):
        """Parse and solve the given sudoku puzzle image, and write the result to a known GCS
        file.
        """

        image_url = self.request.get('image_url')
        filename = self.request.get('filename')
        image_data = None
        try:
            if image_url:
                logging.info("image url: %s", image_url)
                result = urlfetch.fetch(image_url)
                if result.status_code == 200:
                    image_data = result.content
            else:
                logging.info('did not get image url...')
        except:
            logging.exception("issue fetching url data")
        if not image_data:
            logging.info("no image data")
            utils.copy_error_image(filename)
            return

        self.parser = sudoku_image_parser.SudokuImageParser()
        stringified_puzzle = ''
        try:
            stringified_puzzle = self.parser.parse(image_data)
            logging.info("stringified puzzle: %s", stringified_puzzle)
        except (IndexError, sudoku_image_parser.ImageError) as e:
            logging.debug(e)
            utils.copy_error_image(filename)
            return
        try:
            image_solution = self._solved_puzzle_image(stringified_puzzle)
            gcs_file = utils.create_jpg_file(filename, image_solution.tostring())
            logging.debug("url: %s%s", self.api_url, gcs_file)
            return
        except (sudoku_solver.ContradictionError, ValueError) as e:
            logging.debug(e)
            utils.copy_error_image(filename)
            return