Python simplejson.scanner.JSONDecodeError() Examples

The following are 8 code examples of simplejson.scanner.JSONDecodeError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module simplejson.scanner , or try the search function .
Example #1
Source File: swagger_aggregator.py    From swagger-aggregator with MIT License 6 votes vote down vote up
def get_aggregate_swagger(self):
        """Get swagger files associated with the aggregates.

        Returns:
            A dict of swagger spec.
        """
        if 'apis' in self.yaml_file:  # Check if apis is in the config file
            for api_name, api_url in self.yaml_file['apis'].items():
                if api_name not in self.swagger_apis:
                    # Get the swagger.json
                    try:
                        self.swagger_apis[api_name] = {'spec': self.get_swagger_from_url(api_url),
                                                       'url': self.parse_value(api_url)}
                        self.errors.remove(api_url)
                    except (JSONDecodeError, RequestException) as exc:
                        if api_url not in self.errors:
                            self.errors.append(api_url)
                        logger.warning(u'Cannot get swagger from {0}: {1}'.format(api_url, repr(exc)))
                    except ValueError:
                        logger.info(u'Cannot remove {0} from errors'.format(api_url))
        return self.swagger_apis 
Example #2
Source File: api_cache.py    From threat_intel with MIT License 5 votes vote down vote up
def _read_cache_from_file(self):
        """Read the contents of the cache from a file on disk."""
        cache = {}
        try:
            with(open(self._cache_file_name, 'r')) as fp:
                contents = fp.read()
                cache = simplejson.loads(contents)
        except (IOError, JSONDecodeError):
            # The file could not be read. This is not a problem if the file does not exist.
            pass

        return cache 
Example #3
Source File: client.py    From guacapy with GNU General Public License v3.0 5 votes vote down vote up
def __auth_request(
        self, method, url, payload=None, url_params=None, json_response=True
    ):
        params = [("token", self.token)]
        if url_params:
            params += url_params
        logger.debug(
            "{method} {url} - Params: {params}- Payload: {payload}".format(
                method=method, url=url, params=params, payload=payload
            )
        )
        r = requests.request(
            method=method,
            url=url,
            params=params,
            json=payload,
            verify=self.verify,
            allow_redirects=True,
        )
        if not r.ok:
            logger.error(r.content)
        r.raise_for_status()
        if json_response:
            try:
                return r.json()
            except JSONDecodeError:
                logger.error("Could not decode JSON response")
                return r
        else:
            return r 
Example #4
Source File: save_article_content.py    From web_develop with GNU General Public License v3.0 5 votes vote down vote up
def fetch(url):
    s = requests.Session()
    s.headers.update({'user-agent': get_user_agent()})
    proxies = {
        'http': Proxy.get_random()['address'],
    }
    html_text = s.get(url, timeout=TIMEOUT, proxies=proxies).text
    js_url = gen_js_url(url)
    try:
        js_data = s.get(js_url, timeout=TIMEOUT, proxies=proxies).json()
    except JSONDecodeError:
        raise RequestException()
    return html_text, js_data 
Example #5
Source File: save_article_content.py    From web_develop with GNU General Public License v3.0 5 votes vote down vote up
def fetch(url):
    s = requests.Session()
    s.headers.update({'user-agent': get_user_agent()})
    proxies = {
        'http': Proxy.get_random()['address'],
    }
    html_text = s.get(url, timeout=TIMEOUT, proxies=proxies).text
    js_url = gen_js_url(url)
    try:
        js_data = s.get(js_url, timeout=TIMEOUT, proxies=proxies).json()
    except JSONDecodeError:
        raise RequestException()
    return html_text, js_data 
Example #6
Source File: restclient_test.py    From treadmill with Apache License 2.0 5 votes vote down vote up
def test_get_bad_json(self, resp_mock):
        """Test treadmill.restclient.get bad JSON"""
        resp_mock.return_value.status_code = http_client.INTERNAL_SERVER_ERROR
        resp_mock.return_value.text = '{"bad json"'
        resp_mock.return_value.json.side_effect = sjs.JSONDecodeError(
            'Foo', '{"bad json"', 1
        )

        self.assertRaises(
            restclient.MaxRequestRetriesError,
            restclient.get, 'http://foo.com', '/', retries=1) 
Example #7
Source File: gh.py    From papr with MIT License 5 votes vote down vote up
def _update_status(repo, commit, token, data):
    "Sends the status update's data using the GitHub API."

    header = {'Authorization': 'token ' + token}
    api_url = ("https://api.github.com/repos/%s/statuses/%s" %
               (repo, commit))

    if __name__ == '__main__':
        eprint("Updating status of commit", commit, "with data", data)

    try:
        # use data= instead of json= in case we're running on an older requests
        resp = requests.post(api_url, data=json.dumps(data), headers=header)
        _print_ratelimit_info(resp)
        body = resp.json()
    except JSONDecodeError:
        eprint("Expected JSON, but received:")
        eprint("---")
        eprint(resp.content)
        eprint("---")
        eprint("Retrying...")
        resp = requests.post(api_url, data=json.dumps(data), headers=header)
        body = resp.json()

    # pylint: disable=no-member
    if resp.status_code != requests.codes.created:
        if (resp.status_code == requests.codes.unprocessable
                and body is not None and 'message' in body
                and "No commit found for SHA" in body['message']):
            raise CommitNotFoundException()

        # Some other error happened.
        errmsg = "Failed to update commit status [HTTP %d]" % resp.status_code
        errmsg += "\n" + str(resp.headers)
        if body is not None:
            errmsg += "\n" + str(body)
        raise Exception(errmsg)


# XXX: add CLI support and deduplicate with status() 
Example #8
Source File: swagger_aggregator.py    From swagger-aggregator with MIT License 4 votes vote down vote up
def generate_operation_id_function(self, spec, uri, path, action, func_name):
        """Generate a function to handle the current path.

        Args:
            spec: spec of the action the generated function should handle.
            uri: uri of the microservice corresponding to the spec.
            func_name: name the generated function should have.

        Returns:
            A function with func_name as name.
        """
        @retry_http
        def func(*args, **kwargs):
            """Handle a flask request for the current action.

            """
            # Get url from spec and flask query
            url = u'{0}{1}?{2}'.format(uri[func.__name__], path[func.__name__], flask.request.query_string)
            p = re.compile('{(.+)}')
            for path_param in re.findall(p, url):
                for k, v in kwargs.items():
                    if k == path_param:
                        url = url.replace('{{{0}}}'.format(k), str(v))

            requests_meth = getattr(requests, action[func.__name__])

            headers = {k: v for k, v in dict(flask.request.headers).items() if v}

            if not flask.request.headers.get('Content-Type', '').startswith('multipart/form-data'):
                req = requests_meth(url, data=flask.request.data, headers=headers)
            else:
                # Remove Content-Length because it cause error on nginx side
                if 'Content-Length' in headers:
                    headers['X-Content-Length'] = headers['Content-Length']
                    del headers['Content-Length']

                req = requests_meth(url, data=flask.request.stream, headers=headers)

            try:
                return (self.filter_definition(req.json()), req.status_code)
            except JSONDecodeError:
                return (req.text, req.status_code)
        func.__name__ = func_name
        return func