Python json.JSONDecodeError() Examples

The following are 30 code examples of json.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 json , or try the search function .
Example #1
Source File: login.py    From maubot with GNU Affero General Public License v3.0 7 votes vote down vote up
def login(server, username, password, alias) -> None:
    data = {
        "username": username,
        "password": password,
    }
    try:
        with urlopen(f"{server}/_matrix/maubot/v1/auth/login",
                     data=json.dumps(data).encode("utf-8")) as resp_data:
            resp = json.load(resp_data)
            config["servers"][server] = resp["token"]
            if not config["default_server"]:
                print(Fore.CYAN, "Setting", server, "as the default server")
                config["default_server"] = server
            if alias:
                config["aliases"][alias] = server
            save_config()
            print(Fore.GREEN + "Logged in successfully")
    except HTTPError as e:
        try:
            err = json.load(e)
        except json.JSONDecodeError:
            err = {}
        print(Fore.RED + err.get("error", str(e)) + Fore.RESET) 
Example #2
Source File: base.py    From tandem with Apache License 2.0 6 votes vote down vote up
def handle_raw_data(self, retrieve_io_data):
        try:
            io_data = retrieve_io_data()
            if io_data is None or io_data.is_empty():
                return

            data_as_dict = json.loads(io_data.get_data())
            handled = self.handle_message(data_as_dict, io_data)

            if not handled:
                logging.info(
                    "Protocol message was not handled because "
                    "no handler was registered.",
                )

        except json.JSONDecodeError:
            logging.info(
                "Protocol message was ignored because it was not valid JSON.",
            )

        except:
            logging.exception("Exception when handling protocol message:")
            raise 
Example #3
Source File: parser.py    From linehaul with Apache License 2.0 6 votes vote down vote up
def Pip6UserAgent(user_agent):
    # We're only concerned about pip user agents.
    if not user_agent.startswith("pip/"):
        raise UnableToParse

    # This format was brand new in pip 6.0, so we'll need to restrict it
    # to only versions of pip newer than that.
    version_str = user_agent.split()[0].split("/", 1)[1]
    version = packaging.version.parse(version_str)
    if version not in SpecifierSet(">=6", prereleases=True):
        raise UnableToParse

    try:
        return json.loads(user_agent.split(maxsplit=1)[1])
    except (json.JSONDecodeError, UnicodeDecodeError, IndexError):
        raise UnableToParse from None 
Example #4
Source File: fcoin_client.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def _onMessage(self, msg):
        try:
            data = json.loads(msg)
        except json.JSONDecodeError:
            # Something wrong with this data, log and discard
            return
        if isinstance(data, dict):
            if 'topics' in data:
                self._response_handler(data['topics'])
            elif 'type' in data:
                type = data.pop('type')
                self._data_handler(type, data)


        else:
            pass 
Example #5
Source File: test_cachito.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_request_sources_error(status_code, error, error_body, caplog):
    responses.add(
        responses.POST,
        '{}/api/v1/requests'.format(CACHITO_URL),
        content_type='application/json',
        body=error_body,
        status=status_code,
    )

    with pytest.raises(error):
        CachitoAPI(CACHITO_URL).request_sources(CACHITO_REQUEST_REPO, CACHITO_REQUEST_REF)

    try:
        response_data = json.loads(error_body)
    except ValueError:  # json.JSONDecodeError in py3
        assert 'Cachito response' not in caplog.text
    else:
        response_json = 'Cachito response:\n{}'.format(json.dumps(response_data, indent=4))
        # Since Python 3.7 logger adds additional whitespaces by default -> checking without them
        assert re.sub(r'\s+', " ", response_json) in re.sub(r'\s+', " ", caplog.text) 
Example #6
Source File: sources_http_client.py    From koku with GNU Affero General Public License v3.0 6 votes vote down vote up
def build_status_header(self):
        """Build org-admin header for internal status delivery."""
        try:
            encoded_auth_header = self._identity_header.get("x-rh-identity")
            identity = json.loads(b64decode(encoded_auth_header))
            account = identity["identity"]["account_number"]

            identity_header = {
                "identity": {
                    "account_number": account,
                    "type": "User",
                    "user": {"username": "cost-mgmt", "email": "cost-mgmt@redhat.com", "is_org_admin": True},
                }
            }
            json_identity = json_dumps(identity_header)
            cost_internal_header = b64encode(json_identity.encode("utf-8"))

            return {"x-rh-identity": cost_internal_header}
        except (binascii.Error, json.JSONDecodeError, TypeError, KeyError) as error:
            LOG.error(f"Unable to build internal status header. Error: {str(error)}") 
Example #7
Source File: manager.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def update_manager_status(request: web.Request, params: Any) -> web.Response:
    log.info('MANAGER.UPDATE_MANAGER_STATUS (status:{}, force_kill:{})',
             params['status'], params['force_kill'])
    try:
        params = await request.json()
        status = params['status']
        force_kill = params['force_kill']
    except json.JSONDecodeError:
        raise InvalidAPIParameters(extra_msg='No request body!')
    except (AssertionError, ValueError) as e:
        raise InvalidAPIParameters(extra_msg=str(e.args[0]))

    if force_kill:
        await request.app['registry'].kill_all_sessions()
    await request.app['config_server'].update_manager_status(status)

    return web.Response(status=204) 
Example #8
Source File: connection.py    From insightconnect-plugins with MIT License 6 votes vote down vote up
def get_alerts_by_key_value(self, key, value):
        self.update_access_token()
        self.update_time_from()

        full_endpoint_url = self.setup_alerts_endpoint()
        self.logger.info("Connecting to: " + full_endpoint_url)

        response = self.session.get(full_endpoint_url)
        self.raise_exception_on_error(response)

        matching_alerts = []

        self.logger.info("Looking for {} matching {}".format(value, key))

        try:
            matching_alerts = list(filter(lambda a: a.get(key) == value, response.json()))
        except json.JSONDecodeError as e:
            self.logger.error("Alerts returned were in an unexpected format!")
            raise e

        return komand.helper.clean(matching_alerts) 
Example #9
Source File: base.py    From tandem with Apache License 2.0 6 votes vote down vote up
def handle_raw_data(self, retrieve_io_data):
        try:
            io_data = retrieve_io_data()
            if io_data is None or io_data.is_empty():
                return

            data_as_dict = json.loads(io_data.get_data())
            handled = self.handle_message(data_as_dict, io_data)

            if not handled:
                logging.info(
                    "Protocol message was not handled because "
                    "no handler was registered.",
                )

        except json.JSONDecodeError:
            logging.info(
                "Protocol message was ignored because it was not valid JSON.",
            )

        except:
            logging.exception("Exception when handling protocol message:")
            raise 
Example #10
Source File: huaban.py    From PickTrue with MIT License 6 votes vote down vote up
def get(self, url, require_json=False, **kwargs):
        """
        :param require_json: If require_json is True and
        the result is not json-encoded, will raise error
        then have a retry.
        :rtype: requests.Response
        """
        if 'timeout' in kwargs:
            kwargs.pop('timeout')
        resp = self.session.get(url, timeout=(2, 30), **kwargs)
        if require_json:
            try:
                resp.json()
            except JSONDecodeError:
                download_logger.error(
                    "Failed to convert resp to json for url {}: {}".format(
                        url,
                        resp.text,
                    )
                )
                raise
        return resp 
Example #11
Source File: client_auth.py    From maubot with GNU Affero General Public License v3.0 6 votes vote down vote up
def read_client_auth_request(request: web.Request) -> Tuple[Optional[AuthRequestInfo],
                                                                  Optional[web.Response]]:
    server_name = request.match_info.get("server", None)
    server = registration_secrets().get(server_name, None)
    if not server:
        return None, resp.server_not_found
    try:
        body = await request.json()
    except JSONDecodeError:
        return None, resp.body_not_json
    try:
        username = body["username"]
        password = body["password"]
    except KeyError:
        return None, resp.username_or_password_missing
    try:
        base_url = server["url"]
        secret = server["secret"]
    except KeyError:
        return None, resp.invalid_server
    api = HTTPAPI(base_url, "", loop=get_loop())
    user_type = body.get("user_type", "bot")
    return AuthRequestInfo(api, secret, username, password, user_type), None 
Example #12
Source File: login.py    From maubot with GNU Affero General Public License v3.0 6 votes vote down vote up
def login(request: web.Request) -> web.Response:
    try:
        data = await request.json()
    except json.JSONDecodeError:
        return resp.body_not_json
    secret = data.get("secret")
    if secret and get_config()["server.unshared_secret"] == secret:
        user = data.get("user") or "root"
        return resp.logged_in(create_token(user))

    username = data.get("username")
    password = data.get("password")
    if get_config().check_password(username, password):
        return resp.logged_in(create_token(username))

    return resp.bad_auth 
Example #13
Source File: repo_entries.py    From wit with Apache License 2.0 6 votes vote down vote up
def parse(text: str, path: Path, rev: str) -> List[RepoEntry]:
        try:
            fromtext = json.loads(text)
        except json.JSONDecodeError as e:
            print("Failed to parse json in {}:{}: {}".format(path, rev, e.msg))
            sys.exit(1)

        entries = []
        fmt = Format.from_path(path)
        if fmt is Format.Manifest:
            for entry in fromtext:
                entries.append(OriginalEntry.from_dict(entry))
        if fmt is Format.Lock:
            for _, entry in fromtext.items():
                entries.append(OriginalEntry.from_dict(entry))

        # Check for duplicates
        names = [entry.checkout_path for entry in entries]
        if len(names) != len(set(names)):
            dup = set([x for x in names if names.count(x) > 1])
            print("Two repositories have same checkout path in {}:{}: {}".format(path, rev, dup))
            sys.exit(1)

        return entries 
Example #14
Source File: base.py    From tandem with Apache License 2.0 6 votes vote down vote up
def handle_raw_data(self, retrieve_io_data):
        try:
            io_data = retrieve_io_data()
            if io_data is None or io_data.is_empty():
                return

            data_as_dict = json.loads(io_data.get_data())
            handled = self.handle_message(data_as_dict, io_data)

            if not handled:
                logging.info(
                    "Protocol message was not handled because "
                    "no handler was registered.",
                )

        except json.JSONDecodeError:
            logging.info(
                "Protocol message was ignored because it was not valid JSON.",
            )

        except:
            logging.exception("Exception when handling protocol message:")
            raise 
Example #15
Source File: serializers.py    From desec-stack with MIT License 5 votes vote down vote up
def _unpack_code(cls, code):
        try:
            payload_enc = urlsafe_b64decode(code.encode())
            payload = crypto.decrypt(payload_enc, context='desecapi.serializers.AuthenticatedActionSerializer',
                                     ttl=settings.VALIDITY_PERIOD_VERIFICATION_SIGNATURE.total_seconds())
            return json.loads(payload.decode())
        except (TypeError, UnicodeDecodeError, UnicodeEncodeError, json.JSONDecodeError, binascii.Error):
            raise ValueError 
Example #16
Source File: tools_helper.py    From cellphonedb with MIT License 5 votes vote down vote up
def add_to_meta(file: str, meta_path: str):
    if not os.path.exists(meta_path):
        meta = {}
    else:
        try:
            with open(meta_path, 'r') as metafile:
                meta = json.load(metafile)
        except JSONDecodeError:
            meta = {}

    with open(meta_path, 'w') as metafile:
        meta[file] = {'date': datetime.now().strftime('%Y%m%d')}
        json.dump(meta, metafile, indent=2) 
Example #17
Source File: caches.py    From pdm with MIT License 5 votes vote down vote up
def _read_cache(self) -> None:
        if not self.cache_file.exists():
            self._cache = {}
            return
        with self.cache_file.open() as fp:
            try:
                self._cache = json.load(fp)
            except json.JSONDecodeError:
                raise CorruptedCacheError("The dependencies cache seems to be broken.") 
Example #18
Source File: notificationlog.py    From eventsourcing with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def json_loads(self, value: str) -> object:
        try:
            return self.json_decoder.decode(value)
        except JSONDecodeError:
            raise ValueError("Couldn't load JSON string: {}".format(value)) 
Example #19
Source File: base.py    From desec-stack with MIT License 5 votes vote down vote up
def setUp(self):
        def request_callback(r, _, response_headers):
            try:
                request = json.loads(r.parsed_body)
            except JSONDecodeError:
                request = r.parsed_body

            return [
                599,
                response_headers,
                json.dumps(
                    {
                        'MockPDNSTestCase': 'This response was generated upon an unexpected request.',
                        'request': request,
                        'method': str(r.method),
                        'requestline': str(r.raw_requestline),
                        'host': str(r.headers['Host']) if 'Host' in r.headers else None,
                        'headers': {str(key): str(value) for key, value in r.headers.items()},
                    },
                    indent=4
                )
            ]

        super().setUp()
        httpretty.reset()
        hr_core.POTENTIAL_HTTP_PORTS.add(8081)  # FIXME should depend on self.expected_requests
        for method in [
            httpretty.GET, httpretty.PUT, httpretty.POST, httpretty.DELETE, httpretty.HEAD, httpretty.PATCH,
            httpretty.OPTIONS, httpretty.CONNECT
        ]:
            for ns in ['LORD', 'MASTER']:
                httpretty.register_uri(
                    method,
                    self.get_full_pdns_url('.*', ns),
                    body=request_callback,
                    status=599,
                    priority=-100,
                ) 
Example #20
Source File: lookups.py    From foremast with Apache License 2.0 5 votes vote down vote up
def json(self, branch='master', filename=''):
        """Retrieve _filename_ from GitLab.

        Args:
            branch (str): Git Branch to find file.
            filename (str): Name of file to retrieve.

        Returns:
            dict: Decoded JSON.

        Raises:
            SystemExit: Invalid JSON provided.

        """
        file_contents = self.get(branch=branch, filename=filename)

        try:
            json_dict = json.loads(file_contents)
        # TODO: Use json.JSONDecodeError when Python 3.4 has been deprecated
        except ValueError as error:
            msg = ('"{filename}" appears to be invalid json. '
                   'Please validate it with http://jsonlint.com. '
                   'JSON decoder error:\n'
                   '{error}').format(
                       filename=filename, error=error)
            raise SystemExit(msg)

        LOG.debug('JSON object:\n%s', json_dict)
        return json_dict 
Example #21
Source File: test_last_sent_pp_store_helper.py    From indy-plenum with Apache License 2.0 5 votes vote down vote up
def test_cannot_load_last_sent_pre_preapre_key_if_empty_value(
        txnPoolNodeSet, view_no_set, setup):
    node = txnPoolNodeSet[0]
    node.nodeStatusDB.put(LAST_SENT_PRE_PREPARE, b'')

    with pytest.raises(JSONDecodeError):
        pp_key = node.last_sent_pp_store_helper._load_last_sent_pp_key() 
Example #22
Source File: test_last_sent_pp_store_helper.py    From indy-plenum with Apache License 2.0 5 votes vote down vote up
def test_cannot_load_last_sent_pre_preapre_key_if_not_valid_dict(
        txnPoolNodeSet, view_no_set, setup):
    node = txnPoolNodeSet[0]
    node.nodeStatusDB.put(LAST_SENT_PRE_PREPARE,
                          node_status_db_serializer.serialize({1: [2, 5]})[:-1])

    with pytest.raises(JSONDecodeError):
        pp_key = node.last_sent_pp_store_helper._load_last_sent_pp_key() 
Example #23
Source File: communication.py    From pyDcop with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def do_POST(self):
        sender, dest = None, None
        type = MSG_ALGO
        if "sender-agent" in self.headers:
            sender = self.headers["sender-agent"]
        if "dest-agent" in self.headers:
            dest = self.headers["dest-agent"]
        src_comp, dest_comp = None, None
        if "sender-comp" in self.headers:
            src_comp = self.headers["sender-comp"]
        if "dest-comp" in self.headers:
            dest_comp = self.headers["dest-comp"]
        if "type" in self.headers:
            type = self.headers["type"]

        content_length = int(self.headers["Content-Length"])
        post_data = self.rfile.read(content_length)
        try:
            content = json.loads(str(post_data, "utf-8"))
        except JSONDecodeError as jde:
            print(jde)
            print(post_data)
            raise jde

        comp_msg = ComputationMessage(
            src_comp, dest_comp, from_repr(content), int(type)
        )
        try:
            self.server.comm.on_post_message(self.path, sender, dest, comp_msg)

            # Always answer 200, as the actual message is not processed yet by
            # the target computation.
            self.send_response(200)
            self.send_header("Content-type", "text/plain")
            self.end_headers()

        except UnknownComputation as e:
            # if the requested computation is not hosted here
            self.send_response(404, str(e))
            self.send_header("Content-type", "text/plain")
            self.end_headers() 
Example #24
Source File: state.py    From resolwe with Apache License 2.0 5 votes vote down vote up
def get(self):
            """Parse the value from the internal JSON representation.

            :return: The deserialized python object represented by the
                JSON in this variable, or ``None``.
            """
            val = self.redis.get(self.item_name)
            if not val:
                return None
            try:
                val = json.loads(val.decode("utf-8"))
            except json.JSONDecodeError:
                val = None
            return val 
Example #25
Source File: request.py    From jumpserver-python-sdk with GNU General Public License v2.0 5 votes vote down vote up
def clean_result(resp):
        if resp.status_code >= 500:
            msg = "Response code is {0.status_code}: {0.text}".format(resp)
            logger.error(msg)
            raise ResponseError(msg)

        try:
            _ = resp.json()
        except (json.JSONDecodeError, simplejson.scanner.JSONDecodeError):
            msg = "Response json couldn't be decode: {0.text}".format(resp)
            logger.error(msg)
            raise ResponseError(msg)
        else:
            return resp 
Example #26
Source File: sequenceditemmapper.py    From eventsourcing with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def json_loads(self, s: str) -> Dict:
        try:
            return self.json_decoder.decode(s)
        except JSONDecodeError:
            raise ValueError("Couldn't load JSON string: {}".format(s)) 
Example #27
Source File: test_data_processing.py    From peony-twitter with MIT License 5 votes vote down vote up
def test_read_json_decode_error():
    response = MockResponse(data='{', content_type="application/json")
    try:
        await data_processing.read(response, encoding='utf-8')
    except exceptions.PeonyDecodeError as exc:
        assert exc.data == b'{'
        assert isinstance(exc.exception, json.JSONDecodeError)
    else:
        pytest.fail("Did not raise PeonyDecoderError") 
Example #28
Source File: data_processing.py    From peony-twitter with MIT License 5 votes vote down vote up
def read(response, loads=loads, encoding=None):
    """
        read the data of the response

    Parameters
    ----------
    response : aiohttp.ClientResponse
        response
    loads : callable
        json loads function
    encoding : :obj:`str`, optional
        character encoding of the response, if set to None
        aiohttp should guess the right encoding

    Returns
    -------
    :obj:`bytes`, :obj:`str`, :obj:`dict` or :obj:`list`
        the data returned depends on the response
    """
    ctype = response.headers.get('Content-Type', "").lower()

    try:
        if "application/json" in ctype:
            logger.info("decoding data as json")
            return await response.json(encoding=encoding, loads=loads)

        if "text" in ctype:
            logger.info("decoding data as text")
            return await response.text(encoding=encoding)

    except (UnicodeDecodeError, json.JSONDecodeError) as exc:
        data = await response.read()
        raise exceptions.PeonyDecodeError(response=response,
                                          data=data,
                                          exception=exc)

    return await response.read() 
Example #29
Source File: utils.py    From contentful-management.py with MIT License 5 votes vote down vote up
def json_error_class():
    """Returns the class for JSON decode errors
    depends on the Python version."""
    if sys.version_info[0] >= 3 and sys.version_info[1] >= 5:
        return json.JSONDecodeError
    return ValueError 
Example #30
Source File: learn.py    From learn2018-autodown with MIT License 5 votes vote down vote up
def get_json(uri, values={}):
    for i in range(10):
        try:
            page = get_page(uri, values)
            result = json.loads(page)
            return result
        except json.JSONDecodeError:
            print('\rJSON Decode Error, reconnecting (%d) ...' % (i + 1), end='')
            time.sleep(5)
    return {}