Python json.JSONDecodeError() Examples

The following are code examples for showing how to use json.JSONDecodeError(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: python-aiocqhttp   Author: richardchien   File: __init__.py    MIT License 8 votes vote down vote up
def _handle_ws_reverse_universal(self):
        self._add_ws_reverse_api_connection()
        try:
            while True:
                try:
                    payload = json.loads(await websocket.receive())
                except json.JSONDecodeError:
                    payload = None

                if isinstance(payload, dict) and 'post_type' in payload:
                    # is a event
                    asyncio.ensure_future(
                        self._handle_event_payload_with_response(payload))
                elif payload:
                    # is a api result
                    ResultStore.add(payload)
        finally:
            self._remove_ws_reverse_api_connection() 
Example 2
Project: drydock   Author: airshipit   File: base.py    Apache License 2.0 6 votes vote down vote up
def req_json(self, req):
        if req.content_length is None or req.content_length == 0:
            return None

        if req.content_type is not None and req.content_type.lower(
        ) == 'application/json':
            raw_body = req.stream.read(req.content_length or 0)

            if raw_body is None:
                return None

            try:
                json_body = json.loads(raw_body.decode('utf-8'))
                return json_body
            except json.JSONDecodeError as jex:
                print(
                    "Invalid JSON in request: \n%s" % raw_body.decode('utf-8'))
                self.error(
                    req.context,
                    "Invalid JSON in request: \n%s" % raw_body.decode('utf-8'))
                raise errors.InvalidFormat(
                    "%s: Invalid JSON in body: %s" % (req.path, jex))
        else:
            raise errors.InvalidFormat("Requires application/json payload") 
Example 3
Project: pytuber   Author: tefra   File: cmd_add.py    MIT License 6 votes vote down vote up
def parse_jspf(text):
    """
    JSPF parser.

    :param str text:
    :return: A list of tracks
    """

    tracks = []
    with contextlib.suppress(KeyError, json.JSONDecodeError):
        data = json.loads(text)
        for item in data["playlist"]["track"]:
            artist = item.get("creator", "").strip()
            track = item.get("title", "").strip()
            if artist and track and (artist, track) not in tracks:
                tracks.append((artist, track))

    return tracks 
Example 4
Project: ras-frontstage   Author: ONSdigital   File: conversation_controller.py    MIT License 6 votes vote down vote up
def get_conversation(thread_id):
    logger.info('Retrieving conversation thread', thread_id=thread_id)

    headers = _create_get_conversation_headers()
    url = f"{current_app.config['SECURE_MESSAGE_URL']}/threads/{thread_id}"

    with _get_session() as session:
        response = session.get(url, headers=headers)
        try:
            response.raise_for_status()
        except HTTPError as exception:
            if exception.response.status_code == 403:
                raise IncorrectAccountAccessError(message='Access not granted for thread', thread_id=thread_id)
            else:
                logger.error('Thread retrieval failed', thread_id=thread_id)
                raise ApiError(response)

    logger.info('Successfully retrieved conversation thread', thread_id=thread_id)

    try:
        return response.json()
    except JSONDecodeError:
        logger.error('The thread response could not be decoded', thread_id=thread_id)
        raise ApiError(response) 
Example 5
Project: ras-frontstage   Author: ONSdigital   File: conversation_controller.py    MIT License 6 votes vote down vote up
def get_conversation_list(params):
    logger.info('Retrieving threads list')

    headers = _create_get_conversation_headers()
    url = f"{current_app.config['SECURE_MESSAGE_URL']}/threads"

    with _get_session() as session:
        response = session.get(url, headers=headers, params=params)
        try:
            response.raise_for_status()
        except HTTPError:
            logger.error('Threads retrieval failed')
            raise ApiError(response)

    logger.info('Successfully retrieved threads list')

    try:
        return response.json()['messages']
    except JSONDecodeError:
        logger.error('The threads response could not be decoded')
        raise ApiError(response)
    except KeyError:
        logger.error("Request was successful but didn't contain a 'messages' key")
        raise NoMessagesError 
Example 6
Project: python-aiocqhttp   Author: richardchien   File: __init__.py    MIT License 6 votes vote down vote up
def _handle_ws_reverse_event(self):
        try:
            while True:
                try:
                    payload = json.loads(await websocket.receive())
                except json.JSONDecodeError:
                    payload = None

                if not isinstance(payload, dict):
                    # ignore invalid payload
                    continue

                asyncio.ensure_future(
                    self._handle_event_payload_with_response(payload))
        finally:
            pass 
Example 7
Project: cis   Author: mozilla-iam   File: common.py    Mozilla Public License 2.0 6 votes vote down vote up
def _load_publisher_rules(self, rules_url):
        """
        Get the CIS integration rules
        """
        rules = None
        if not self.always_use_local_file:
            if rules_url is not None:
                try:
                    r = requests.get(rules_url)
                    rules = r.json()
                except (json.JSONDecodeError, requests.exceptions.ConnectionError) as e:
                    logger.debug("Failed to load rules data from rules_url {} ({})".format(rules_url, e))
        # Fall-back to built-in copy
        if self.always_use_local_file or rules is None:
            rules_file = "data/well-known/mozilla-iam-publisher-rules"
            if not os.path.isfile(rules_file):
                dirname = os.path.dirname(os.path.realpath(__file__))
                path = dirname + "/" + rules_file
            else:
                path = rules_file

            rules = json.load(open(path))
        return rules 
Example 8
Project: trelliolibs   Author: quikmile   File: decorators.py    MIT License 6 votes vote down vote up
def validate_schema(schema=None, allow_unknown=False):
    def decorator(func):
        @wraps(func)
        async def wrapper(self, *args, **kwargs):
            if schema:
                v = TrellioValidator(schema, allow_unknown=allow_unknown)
                if isinstance(self, HTTPService) or isinstance(self, HTTPView):
                    request = args[0]
                    try:
                        payload = await request.json()
                    except JSONDecodeError:
                        data = await request.text()
                        return json_response({'error': 'invalid json', 'data': data}, status=400)
                    if not v.validate(payload):
                        return json_response({'error': v.errors}, status=400)
                elif isinstance(self, TCPService) or isinstance(self, TCPView):
                    if not v.validate(kwargs):
                        return {'error': v.errors}
            return await func(self, *args, **kwargs)

        return wrapper

    return decorator 
Example 9
Project: trelliolibs   Author: quikmile   File: decorators.py    MIT License 6 votes vote down vote up
def required_params(*params):
    def decorator(func):
        @wraps(func)
        async def wrapper(self, *args, **kwargs):
            if isinstance(self, HTTPService) or isinstance(self, HTTPView):
                request = args[0]
                try:
                    payload = await request.json()
                except JSONDecodeError:
                    data = await request.text()
                    return json_response({'error': 'invalid json', 'data': data}, status=400)
                missing_params = list(filter(lambda x: x not in payload.keys(), params))
                if missing_params:
                    return json_response({'error': 'required params - {} not found'.format(', '.join(missing_params))})
            elif isinstance(self, TCPService) or isinstance(self, TCPView):
                missing_params = list(filter(lambda x: x not in kwargs.keys(), params))
                if missing_params:
                    return {'error': 'required params - {} not found'.format(', '.join(missing_params))}

            return await func(self, *args, **kwargs)

        return wrapper

    return decorator 
Example 10
Project: Lomapy   Author: AlanTaranti   File: manipulador_requisicoes.py    MIT License 6 votes vote down vote up
def erro(resposta: requests.Response):
    """
    Gera uma exceção padronizada

    :param resposta: Resposta
    :type resposta: requests.Response

    :raises Exception: Falha na requisição
    """
    dados = {
        "codigo": resposta.status_code,
        "motivo": resposta.reason,
        "resposta": None
    }
    try:
        dados["resposta"] = resposta.json()
    except JSONDecodeError:
        pass

    if dados["codigo"] == 404:
        raise RespostaVaziaException()
    else:
        raise Exception(dados) 
Example 11
Project: UROV2019   Author: CalPolyUROV   File: client.py    The Unlicense 6 votes vote down vote up
def request_data(self):
        """Main continual entry point for sending data over sockets
        """
        self.create_connection()
        data_bytes = self.receive_data()
        self.close_socket()

        if data_bytes is None:
            # TODO: Throw an exception
            return

        data_str = data_bytes.decode()
        try:
            debug("decode_verbose",
                  "Decoded bytes as {}: {}",
                  [data_str.__class__, data_str])
            data_dict = json.loads(data_str)
            debug("decode_verbose", "Decoded control input: {}", [data_dict])
            self.parent.datastore.store(self.data_name, data_dict)

        except JSONDecodeError as error:
            debug("JSON_Error", "{}", [error])
            # TODO: Throw an exception
            return 
Example 12
Project: maubot   Author: maubot   File: client_auth.py    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", None)
    return AuthRequestInfo(api, secret, username, password, user_type), None 
Example 13
Project: maubot   Author: maubot   File: login.py    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 14
Project: maubot   Author: maubot   File: login.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def login(server, username, password) -> 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"]
            config["default_server"] = 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 15
Project: pyTBA   Author: Thing342   File: api.py    GNU General Public License v3.0 6 votes vote down vote up
def tba_get(self, path):
        """Base method for querying the TBA API. Returns the response JSON as a python dict.
        :param path: (str) Request path, without the API address prefix (https://www.thebluealliance.com/api/v2/)
        :return: A dict parsed from the response from the API.
        """

        if self.app_id['X-TBA-App-Id'] == "":
            raise Exception('An API key is required for TBA. Please use set_api_key() to set one.')

        url_str = 'https://www.thebluealliance.com/api/v2/' + path
        r = self.session.get(url_str, headers=self.app_id)
        # print(r.url)
        tba_txt = r.text
        try:
            return json.loads(tba_txt)
        except json.JSONDecodeError:
            print(url_str)
            print(tba_txt) 
Example 16
Project: backend.ai-manager   Author: lablup   File: manager.py    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 17
Project: errbot-chatopsanything   Author: andrewthetechie   File: chatops-anything.py    GNU General Public License v3.0 6 votes vote down vote up
def _read_json_config(self, file: Path) -> List[Dict]:
        """
        Reads a json config file from the disk and returns it as a dictionary
        Args:
            file (Path): pathlib.Path object to our file

        Returns:
            List[Dict] - list of config objects from the json file
        """
        self.log.debug(f"Opening {file} to read as json config")
        with open(file, 'r') as stream:
            try:
                read_data = json.load(stream)
            except json.JSONDecodeError as exc:
                self.log.error(f"{file} is not a valid Json config file {str(exc)}")
                return list()

        self.log.debug(f"Read in {file} to {read_data}")
        if type(read_data) != list:
            self.log.error(f"{file} is not a valid config file. Please check the config examples. Your file should "
                           f"contain a list of dictionaries")
            return list()

        return read_data 
Example 18
Project: GreenLuma-Reborn-Manager   Author: ImaniiTy   File: core.py    MIT License 6 votes vote down vote up
def load_profiles(self):
        if not os.path.exists(PROFILES_PATH):
            os.makedirs(PROFILES_PATH)
            self.create_profile("default")
        elif len(os.listdir(PROFILES_PATH)) == 0:
            self.create_profile("default")

        for filename in os.listdir(PROFILES_PATH):
            with open("{}/{}".format(PROFILES_PATH,filename), "r") as file:
                try:
                    data = json.load(file)
                    self.register_profile(Profile.from_JSON(data))
                except json.JSONDecodeError as e:
                    logging.exception(e)
                    file.close()
                    os.remove("{}/{}".format(PROFILES_PATH,filename)) 
Example 19
Project: mist.api   Author: mistio   File: actions.py    Apache License 2.0 6 votes vote down vote up
def clean(self):
        if self.json:
            try:
                json.loads(self.json)
            except json.decoder.JSONDecodeError as e:
                raise me.ValidationError(
                    "Invalid JSON payload: %s" % e.args[0]
                )
        if self.headers:
            try:
                json.loads(self.headers)
            except json.decoder.JSONDecodeError as e:
                raise me.ValidationError(
                    "HTTP Headers should be defined as a valid "
                    "JSON dictionary: %s" % e.args[0]
                ) 
Example 20
Project: reinforch   Author: kaixinbaba   File: util.py    MIT License 6 votes vote down vote up
def read_config(config: Union[str, dict, Config]) -> Config:
    _check_config_type(config)
    result = None
    if isinstance(config, str):
        if os.path.exists(config) and os.path.isfile(config):
            # read file
            with open(config, 'r', encoding='utf-8') as f:
                json_str = f.read()
        else:
            # json string
            json_str = config
        try:
            data = json.loads(json_str)
            result = Config(data)
        except JSONDecodeError as e:
            logging.error(e)
    elif isinstance(config, dict):
        result = Config(config)
    elif isinstance(config, Config):
        result = config
    if result is None:
        raise ReinforchException(
            "Can't read config, please check! The argument config is [{}], type is [{}]".format(config, type(config)))
    return result 
Example 21
Project: apistar-msgpack   Author: juancarlospaco   File: apistar_msgpack.py    GNU General Public License v3.0 5 votes vote down vote up
def parse(self, body: http.Body) -> typing.Any:
        if not body:
            raise exceptions.BadRequest(detail=f'Empty MessagePack: {body}.')
        try:
            if has_msgpack:
                data_from_msgpack = loads(msgpack.unpackb(body))
            else:
                data_from_msgpack = loads(umsgpack.unpackb(body))
        except UnicodeEncodeError as error:
            raise exceptions.BadRequest(
                detail=f'Invalid Unicode UTF-8 on MessagePack error: {error}.')
        except ValueError as error:
            raise exceptions.BadRequest(
                detail=f'Invalid Keys or Values on MessagePack error: {error}')
        except JSONDecodeError as error:
            raise exceptions.BadRequest(detail=f'Invalid MessagePack: {error}')
        except Exception as error:
            raise exceptions.BadRequest(
                detail=f'{self} Unknown Exception: {error}, parsing {body}.')
        else:
            return data_from_msgpack 
Example 22
Project: fs_image   Author: facebookincubator   File: subvolume_on_disk.py    MIT License 5 votes vote down vote up
def from_json_file(cls, infile, subvolumes_dir):
        parsed_json = '<NO JSON PARSED>'
        try:
            parsed_json = json.load(infile)
            return cls.from_serializable_dict(
                parsed_json, subvolumes_dir
            )
        except json.JSONDecodeError as ex:
            raise RuntimeError(
                f'Parsing subvolume JSON from {infile}: {ex.doc}'
            ) from ex
        except Exception as ex:
            raise RuntimeError(
                f'Parsed subvolume JSON from {infile}: {parsed_json}'
            ) from ex 
Example 23
Project: jumpserver-python-sdk   Author: jumpserver   File: request.py    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 24
Project: jumpserver-python-sdk   Author: jumpserver   File: test_request.py    GNU General Public License v2.0 5 votes vote down vote up
def test_clean_result_decode_error(self):
        resp_mock = mock.MagicMock()
        resp_mock.status_code = 200
        resp_mock.json.side_effect = json.JSONDecodeError("error", "\n\n", 1)

        with self.assertRaises(ResponseError):
            self.http.clean_result(resp_mock) 
Example 25
Project: backtrader-cn   Author: pandalibin   File: xq_client.py    GNU General Public License v3.0 5 votes vote down vote up
def __get_create_cube_token(self):
        """获取创建组合时需要的token信息
        :return: token
        """
        response = self.session.get(self.config["get_token_url"], params={
            "api": "/cubes/create.json"
        })
        try:
            token_response = json.loads(response.text)
        except JSONDecodeError:
            raise TradeError("解析创建组合的token信息失败: %s" % response.text)
        if 'token' not in token_response:
            raise TradeError("获取创建组合的token信息失败: %s" % response.text)
        return token_response['token'] 
Example 26
Project: backtrader-cn   Author: pandalibin   File: xq_client.py    GNU General Public License v3.0 5 votes vote down vote up
def get_cubes_list(self, type=4):
        """获取组合详情,默认获取自选组合
        :param type: 组合名字的前缀, 1: 全部组合。 4: 我的组合。 5: 只看沪深组合。 6: 只看美股。7: 只看港股
        :return: 组合列表
        """
        response = self.session.get(self.config['get_cubes_list'], params={
            "category": 1,
            "type": type
        })
        try:
            cubes_response = json.loads(response.text)
        except JSONDecodeError:
            log.warning(response.text)
            raise TradeError("解析组合列表失败: %s" % response.text)
        if 'stocks' not in cubes_response:
            log.warning(cubes_response)
            raise TradeError("获取组合信息失败: %s" % response.text)
        cubes_code_list = [cube['code'] for cube in cubes_response['stocks']]

        response = self.session.get(self.config['get_cubes_detail'], params={
            "code": ','.join(cubes_code_list),
            "return_hasexist": False,
        })
        try:
            cubes_detail_response = json.loads(response.text)
        except JSONDecodeError:
            raise TradeError("解析组合详情失败: %s" % response.text)
        if 'stocks' not in cubes_response:
            raise TradeError("获取组合信息失败: %s" % response.text)
        return cubes_detail_response 
Example 27
Project: AutoDL   Author: tanguofu   File: testing.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _local_request(self, method, uri, cookies=None, *args, **kwargs):
        import aiohttp
        if uri.startswith(('http:', 'https:', 'ftp:', 'ftps://' '//')):
            url = uri
        else:
            url = 'http://{host}:{port}{uri}'.format(
                host=HOST, port=self.port, uri=uri)

        logger.info(url)
        conn = aiohttp.TCPConnector(verify_ssl=False)
        async with aiohttp.ClientSession(
                cookies=cookies, connector=conn) as session:
            async with getattr(
                    session, method.lower())(url, *args, **kwargs) as response:
                try:
                    response.text = await response.text()
                except UnicodeDecodeError as e:
                    response.text = None

                try:
                    response.json = await response.json()
                except (JSONDecodeError,
                        UnicodeDecodeError,
                        aiohttp.ClientResponseError):
                    response.json = None

                response.body = await response.read()
                return response 
Example 28
Project: pytuber   Author: tefra   File: storage.py    MIT License 5 votes vote down vote up
def from_file(cls, path: str):
        data: Dict = dict()
        with suppress(FileNotFoundError, JSONDecodeError):
            with open(path, "r") as cfg:
                data = json.load(cfg)
        return cls(data) 
Example 29
Project: jsonrpcclient   Author: bcb   File: test_parse.py    MIT License 5 votes vote down vote up
def test_parse_invalid_json():
    with pytest.raises(JSONDecodeError):
        parse("{dodgy}", batch=False) 
Example 30
Project: django-notifs   Author: danidee10   File: fields.py    MIT License 5 votes vote down vote up
def from_db_value(self, value, expression, connection, *args, **kwargs):
        """Convert the JSON back to a dictionary."""
        try:
            return json.loads(value)
        except json.JSONDecodeError:
            return value 
Example 31
Project: helloworld   Author: pip-uninstaller-python   File: 爬腾讯新闻2_把Redis存的新闻内容抓到本地文件.py    GNU General Public License v2.0 5 votes vote down vote up
def get_page_info(url):
    t = ''
    # 获取页面信息
    re = requests.get(url)
    # 使用pyqyery解析得到标题和内容
    info = q(re.text)
    # 获取标题
    title = info('body > div.qq_conent.clearfix > div.LEFT > h1').text()
    t += title + '\n\n'+url +'\n'
    # 我发现页面js中含有一个json串,含有新闻的基本信息,切片获取后用json解析
    # 获取js语句,切片后得到json串
    aaa = info('head > script:last').text()[14:]
    try:
        j = json.loads(aaa, encoding='UTF-8')
    except JSONDecodeError as e :
        print(e)
    # 发表媒体
    media = j['media']
    t += '作者:' + media + '\n'
    # 发布时间
    pubtime = j['pubtime']
    t += '发布时间:' + pubtime + '\n'
    # 新闻标签
    tags = j['tags']
    t += '关键字:' + tags + '\n\n'
    # 新闻内容
    ps = info('body > div.qq_conent.clearfix > div.LEFT > div.content.clearfix > div.content-article > p')
    for p in ps:
        # 每次一段
        t +=q(p).text()
        t +='\n\n'
    return t


# 把数据序列化到文件中 
Example 32
Project: trio-chrome-devtools-protocol   Author: HyperionGray   File: __init__.py    MIT License 5 votes vote down vote up
def _reader_task(self):
        '''
        Runs in the background and handles incoming messages: dispatching
        responses to commands and events to listeners.
        '''
        while True:
            message = await self.ws.get_message()
            try:
                data = json.loads(message)
            except json.JSONDecodeError:
                raise BrowserError({
                    'code': -32700,
                    'message': 'Client received invalid JSON',
                    'data': message
                })
            logger.debug('Received message %r', data)
            if 'sessionId' in data:
                session_id = cdp.target.SessionID(data['sessionId'])
                try:
                    session = self.sessions[session_id]
                except KeyError:
                    raise BrowserError('Browser sent a message for an invalid '
                        'session: {!r}'.format(session_id))
                session._handle_data(data)
            else:
                self._handle_data(data) 
Example 33
Project: controller   Author: deis   File: config.py    MIT License 5 votes vote down vote up
def set_tags(self, previous_config):
        """verify the tags exist on any nodes as labels"""
        if not self.tags:
            if settings.DEIS_DEFAULT_CONFIG_TAGS:
                try:
                    tags = json.loads(settings.DEIS_DEFAULT_CONFIG_TAGS)
                    self.tags = tags
                except json.JSONDecodeError as e:
                    return
            else:
                return

        # Get all nodes with label selectors
        nodes = self._scheduler.node.get(labels=self.tags).json()
        if nodes['items']:
            return

        labels = ['{}={}'.format(key, value) for key, value in self.tags.items()]
        message = 'No nodes matched the provided labels: {}'.format(', '.join(labels))

        # Find out if there are any other tags around
        old_tags = getattr(previous_config, 'tags')
        if old_tags:
            old = ['{}={}'.format(key, value) for key, value in old_tags.items()]
            new = set(labels) - set(old)
            if new:
                message += ' - Addition of {} is the cause'.format(', '.join(new))

        raise DeisException(message) 
Example 34
Project: python-aiocqhttp   Author: richardchien   File: __init__.py    MIT License 5 votes vote down vote up
def _handle_ws_reverse_api(self):
        self._add_ws_reverse_api_connection()
        try:
            while True:
                try:
                    ResultStore.add(json.loads(await websocket.receive()))
                except json.JSONDecodeError:
                    pass
        finally:
            self._remove_ws_reverse_api_connection() 
Example 35
Project: cis   Author: mozilla-iam   File: mozilliansorg_groups.py    Mozilla Public License 2.0 5 votes vote down vote up
def from_record(record):
        """
        Constructs a MozilliansGroupUpdate from a raw sqs record.

        @record: a raw record from an SQS event
        @return: MozilliansGroupUpdate or None to error gracefully.
        """
        body = get_nested(record, "body")
        if not body:
            logger.error("Event without body: {}".format(record))
            return None
        try:
            body = json.loads(body)
        except json.JSONDecodeError as e:
            logger.error("Invalid JSON in body: {}".format(e))
            return None
        user_id = get_nested(body, "dynamodb", "Keys", "user_id", "S")
        new_image = get_nested(body, "dynamodb", "NewImage")
        typ = get_nested(body, "eventName")
        payload_user_id = get_nested(new_image, "user_id", "S")
        if user_id != payload_user_id:
            logger.error("mismatching user_ids {} {}".format(user_id, payload_user_id))
            return None
        groups = unpack_string_list(get_nested(new_image, "groups", "L", default=[]))
        if user_id and typ and groups is not None:
            return MozilliansorgGroupUpdate(typ, user_id, groups)
        return None 
Example 36
Project: cis   Author: mozilla-iam   File: common.py    Mozilla Public License 2.0 5 votes vote down vote up
def _load_well_known(self):
        """
        Gets the discovery url's data ("well-known")
        Return dict,None the well-known JSON data copy
        """
        # Memory cache
        if self._well_known_json is not None:
            return self._well_known_json

        if not self.always_use_local_file:
            try:
                r = requests.get(self.discovery_url)
                self._well_known_json = r.json()
            except (json.JSONDecodeError, requests.exceptions.ConnectionError) as e:
                logger.debug("Failed to fetch schema url from discovery {} ({})".format(self.discovery_url, e))
                logger.debug("Using builtin copy")

        if self._well_known_json is None or self.always_use_local_file:
            well_known_file = "data/well-known/mozilla-iam"  # Local fall-back
            if not os.path.isfile(well_known_file):
                dirname = os.path.dirname(os.path.realpath(__file__))
                path = dirname + "/" + well_known_file
            else:
                path = well_known_file
            self._well_known_json = json.load(open(path))

        return self._well_known_json 
Example 37
Project: cis   Author: mozilla-iam   File: common.py    Mozilla Public License 2.0 5 votes vote down vote up
def _load_schema(self, schema_url, stype="data/profile.schema"):
        """
        Loads JSON Schema from an URL
        @schema_url: str,None the schema URL
        @stype: str, type of schema to load. This is also the name of the library builtin, local copy.
        Return dict JSON object which is the CIS Profile Schema
        """
        schema = None
        if not self.always_use_local_file:
            if schema_url is not None:
                try:
                    r = requests.get(schema_url)
                    schema = r.json()
                except (json.JSONDecodeError, requests.exceptions.ConnectionError) as e:
                    logger.debug("Failed to load schema from schema_url {} ({})".format(schema_url, e))

        # That did not work, fall-back to local, built-in copy
        if schema is None or self.always_use_local_file:
            # Builtin, hardcoded schema from library
            schema_file = stype
            if not os.path.isfile(schema_file):
                dirname = os.path.dirname(os.path.realpath(__file__))
                path = dirname + "/" + schema_file
            else:
                path = schema_file

            schema = json.load(open(path))
        return schema 
Example 38
Project: spotlight   Author: mdoesburg   File: rules.py    MIT License 5 votes vote down vote up
def valid_json(value) -> bool:
        try:
            json.loads(value)
            return True
        except (TypeError, JSONDecodeError):
            return False 
Example 39
Project: botovod   Author: OlegYurchik   File: gino.py    MIT License 5 votes vote down vote up
def a_get_values(self) -> dict:

        try:
            return json.loads(self.data)
        except JSONDecodeError:
            logger.error("Cannot get values for follower %s %s - incorrect json", self.bot,
                         self.chat) 
Example 40
Project: botovod   Author: OlegYurchik   File: gino.py    MIT License 5 votes vote down vote up
def a_get_value(self, name: str, default=None):

        try:
            return json.loads(self.data)[name]
        except KeyError:
            return default
        except JSONDecodeError:
            logger.error("Cannot get value '%s' for follower %s %s - incorrect json", name,
                         self.bot, self.chat) 
Example 41
Project: botovod   Author: OlegYurchik   File: gino.py    MIT License 5 votes vote down vote up
def a_set_value(self, name: str, value: str):

        try:
            data = json.loads(self.data)
        except JSONDecodeError:
            logger.error("Incorrect json structure for follower %s %s", self.bot, self.chat)
            data = dict()
        data[name] = value
        await self.update(data=json.dumps(data)).apply() 
Example 42
Project: botovod   Author: OlegYurchik   File: sqlalchemy.py    MIT License 5 votes vote down vote up
def get_values(self) -> dict:

        try:
            return json.loads(self.data)
        except JSONDecodeError:
            logger.error("Cannot get values for follower %s %s - incorrect json", self.bot,
                         self.chat) 
Example 43
Project: botovod   Author: OlegYurchik   File: sqlalchemy.py    MIT License 5 votes vote down vote up
def get_value(self, name: str, default=None):

        try:
            return json.loads(self.data)[name]
        except KeyError:
            return default
        except JSONDecodeError:
            logger.error("Cannot get value '%s' for follower %s %s - incorrect json", name,
                         self.bot, self.chat) 
Example 44
Project: clashroyale   Author: cgrok   File: client.py    MIT License 5 votes vote down vote up
def _raise_for_status(self, resp, text, *, method=None):
        try:
            data = json.loads(text)
        except json.JSONDecodeError:
            data = text
        code = getattr(resp, 'status', None) or getattr(resp, 'status_code')
        log.debug(self.REQUEST_LOG.format(method=method or resp.request_info.method, url=resp.url, text=text, status=code))
        if self.error_debug:
            raise ServerError(resp, data)
        if 300 > code >= 200:  # Request was successful
            if self.using_cache:
                cached_data = {
                    'c_timestamp': datetime.utcnow().timestamp(),
                    'data': data
                }
                self.cache[str(resp.url)] = cached_data
            return data, False, datetime.utcnow(), resp  # value, cached, last_updated, response
        if code == 400:
            raise BadRequest(resp, data)
        if code in (401, 403):  # Unauthorized request - Invalid token
            raise Unauthorized(resp, data)
        if code == 404:  # Tag not found
            raise NotFoundError(resp, data)
        if code == 429:
            raise RatelimitError(resp, data)
        if code == 503:  # Maintainence
            raise ServerError(resp, data)

        raise UnexpectedError(resp, data) 
Example 45
Project: clashroyale   Author: cgrok   File: client.py    MIT License 5 votes vote down vote up
def _raise_for_status(self, resp, text, *, method=None):
        try:
            data = json.loads(text)
        except json.JSONDecodeError:
            data = text
        code = getattr(resp, 'status', None) or getattr(resp, 'status_code')
        log.debug(self.REQUEST_LOG.format(method=method or resp.request_info.method, url=resp.url, text=text, status=code))
        if self.error_debug:
            raise ServerError(resp, data)
        if 300 > code >= 200:  # Request was successful
            if self.using_cache:
                cached_data = {
                    'c_timestamp': datetime.utcnow().timestamp(),
                    'data': data
                }
                self.cache[str(resp.url)] = cached_data
            if resp.headers.get('x-ratelimit-limit'):
                self.ratelimit = [
                    int(resp.headers['x-ratelimit-limit']),
                    int(resp.headers['x-ratelimit-remaining']),
                    int(resp.headers.get('x-ratelimit-reset', 0))
                ]
            return data, False, datetime.utcnow(), resp  # value, cached, last_updated, response
        if code == 401:  # Unauthorized request - Invalid token
            raise Unauthorized(resp, data)
        if code in (400, 404):  # Tag not found
            raise NotFoundError(resp, data)
        if code == 417:
            raise NotTrackedError(resp, data)
        if code == 429:
            raise RatelimitError(resp, data)
        if code >= 500:  # Something wrong with the api servers :(
            raise ServerError(resp, data)

        raise UnexpectedError(resp, data) 
Example 46
Project: sanic   Author: huge-success   File: test_keep_alive_timeout.py    MIT License 5 votes vote down vote up
def _local_request(self, method, url, *args, **kwargs):
        raw_cookies = kwargs.pop("raw_cookies", None)
        request_keepalive = kwargs.pop(
            "request_keepalive", CONFIG_FOR_TESTS["KEEP_ALIVE_TIMEOUT"]
        )
        if not self._session:
            self._session = self.get_new_session()
        try:
            response = await getattr(self._session, method.lower())(
                url, verify=False, timeout=request_keepalive, *args, **kwargs
            )
        except NameError:
            raise Exception(response.status_code)

        try:
            response.json = response.json()
        except (JSONDecodeError, UnicodeDecodeError):
            response.json = None

        response.body = await response.read()
        response.status = response.status_code
        response.content_type = response.headers.get("content-type")

        if raw_cookies:
            response.raw_cookies = {}
            for cookie in response.cookies:
                response.raw_cookies[cookie.name] = cookie

        return response 
Example 47
Project: sanic   Author: huge-success   File: testing.py    MIT License 5 votes vote down vote up
def _local_request(self, method, url, *args, **kwargs):
        logger.info(url)
        raw_cookies = kwargs.pop("raw_cookies", None)

        if method == "websocket":
            async with websockets.connect(url, *args, **kwargs) as websocket:
                websocket.opened = websocket.open
                return websocket
        else:
            async with self.get_new_session() as session:

                try:
                    response = await getattr(session, method.lower())(
                        url, verify=False, *args, **kwargs
                    )
                except NameError:
                    raise Exception(response.status_code)

                try:
                    response.json = response.json()
                except (JSONDecodeError, UnicodeDecodeError):
                    response.json = None

                response.body = await response.read()
                response.status = response.status_code
                response.content_type = response.headers.get("content-type")

                if raw_cookies:
                    response.raw_cookies = {}

                    for cookie in response.cookies.jar:
                        response.raw_cookies[cookie.name] = cookie

                return response 
Example 48
Project: sproutlib   Author: LabMouseTeam   File: Sproutlib.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, *args, **kw):
        # Init the dict with nothing in it.
        dict.__init__(self, (), **kw)

        self.__iter_i = 0
        self.__iter_l = []

        self.__hash = self.__generate_hash()
        SproutSchema.__add_identity(self.__hash)

        # If we were loaded with a dict, process each name by class.
        if len(args) == 0:
            return

        # If we were seeded with a string, presume it's either YAML or JSON,
        # and parse it into an object.
        if len(args) == 1 and isinstance(args[0], str):
            try:
                a = json.loads(args[0])
            except json.JSONDecodeError:
                # If JSON didn't work, presume its YAML.
                try:
                    a = yaml_safe_load(args[0])
                except YAMLParserError as E:
                    raise E
        else:
            a = args[0]

        self.__do_update(a) 
Example 49
Project: Twitch-Chat-Downloader   Author: PetterKraabol   File: settings.py    MIT License 5 votes vote down vote up
def load(self, filepath: str) -> Dict[str, Any]:
        """
        Load dictionary from json file
        :param filepath: filepath to load from
        :return: Configuration dictionary
        """

        # Create settings file from reference file if necessary
        if not self.filepath.exists():
            self.directory.mkdir(parents=True, exist_ok=True)

            # Missing reference file
            if not self.reference_filepath.exists():
                Logger().log(f'Could not find {self.reference_filepath}', Log.CRITICAL)
                exit(1)

            # Load config from reference settings
            with open(self.reference_filepath, 'r') as file:
                config = json.load(file)

            Settings.write(self.filepath, data=config)

            return config

        # Load from settings file
        try:
            with open(filepath, 'r') as file:
                return json.load(file)
        except json.JSONDecodeError:
            print('Invalid settings format')
            exit(1) 
Example 50
Project: waspy   Author: wasp   File: parser.py    Apache License 2.0 5 votes vote down vote up
def decode(self, data: bytes):
        if data:
            try:
                return json.loads(data)
            except json.JSONDecodeError:
                raise ParseError("Invalid JSON") 
Example 51
Project: contentful.py   Author: contentful   File: utils.py    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 52
Project: custodia   Author: latchset   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def error_message(args, exc):
    out = StringIO()
    parts = urlparse(args.server)

    if args.debug:
        traceback.print_exc(file=out)
        out.write('\n')

    out.write("ERROR: Custodia command '{args.sub} {args.name}' failed.\n")
    if args.verbose:
        out.write("Custodia server '{args.server}'.\n")

    if isinstance(exc, requests.exceptions.HTTPError):
        errcode = E_HTTP_ERROR
        out.write("{exc.__class__.__name__}: {exc}\n")
    elif isinstance(exc, requests.exceptions.ConnectionError):
        errcode = E_CONNECTION_ERROR
        if parts.scheme == 'http+unix':
            out.write("Failed to connect to Unix socket '{unix_path}':\n")
        else:
            out.write("Failed to connect to '{parts.netloc}' "
                      "({parts.scheme}):\n")
        # ConnectionError always contains an inner exception
        out.write("    {exc.args[0]}\n")
    elif isinstance(exc, JSONDecodeError):
        errcode = E_JSON_ERROR
        out.write("Server returned invalid JSON response:\n")
        out.write("    {exc}\n")
    else:
        errcode = E_OTHER
        out.write("{exc.__class__.__name__}: {exc}\n")

    msg = out.getvalue()
    if not msg.endswith('\n'):
        msg += '\n'
    return errcode, msg.format(args=args, exc=exc, parts=parts,
                               unix_path=unquote(parts.netloc)) 
Example 53
Project: ssrspeed_backup   Author: mazhenting   File: config_parser.py    GNU General Public License v3.0 5 votes vote down vote up
def read_gui_config(self, filename: str):
		raw_data = ""
		with open(filename, "r", encoding="utf-8") as f:
			raw_data = f.read()
			f.close()
		try:
			#Try Load as Json
			data = json.loads(raw_data)
			#Identification of proxy type
			#Shadowsocks(D)
			if (
				"subscriptions" in data
				or
				(
					"subscriptions" not in data
					and "serverSubscribe" not in data
					and "vmess" not in data
				)
			):
				pssb = ParserShadowsocksBasic(self.__get_ss_base_config())
				for cfg in pssb.parse_gui_data(data):
					self.__nodes.append(NodeShadowsocks(cfg))
			#ShadowsocksR
			elif "serverSubscribe" in data:
				pssr = ParserShadowsocksR(self.__get_ss_base_config())
				for cfg in pssr.parse_gui_data(data):
					self.__nodes.append(NodeShadowsocksR(cfg))
			#V2RayN
			elif "vmess" in data:
				pv2n = ParserV2RayN()
				cfgs = pv2n.parse_gui_data(data)
				for cfg in cfgs:
					self.__nodes.append(
						NodeV2Ray(
							V2RayBaseConfigs.generate_config(cfg, LOCAL_ADDRESS, LOCAL_PORT)
						)
					)
		except json.JSONDecodeError:
			#Try Load as Yaml
			self.__nodes = self.__parse_clash(raw_data) 
Example 54
Project: statsbomb-parser   Author: imrankhan17   File: base.py    MIT License 5 votes vote down vote up
def __init__(self, event_id: str = None, season_id: str = None):
        self.event_id = event_id
        self.season_id = season_id
        self.id = self._construct_id
        try:
            self.data = requests.get(self._construct_url()).json()
        except JSONDecodeError:
            raise Exception(f'No data found for {self.__class__.__name__.lower()} ID: {self.id}, '
                            f'URL: {self._construct_url()}') 
Example 55
Project: gphotos-sync   Author: gilesknap   File: authorize.py    MIT License 5 votes vote down vote up
def __init__(
            self, scope: List[str], token_file: Path,
            secrets_file: Path, max_retries: int = 5
    ):
        """ A very simple class to handle Google API authorization flow
        for the requests library. Includes saving the token and automatic
        token refresh.

        Args:
            scope: list of the scopes for which permission will be granted
            token_file: full path of a file in which the user token will be
            placed. After first use the previous token will also be read in from
            this file
            secrets_file: full path of the client secrets file obtained from
            Google Api Console
        """
        self.max_retries = max_retries
        self.scope: List[str] = scope
        self.token_file: Path = token_file
        self.session = None
        self.token = None
        try:
            with secrets_file.open('r') as stream:
                all_json = load(stream)
            secrets = all_json['installed']
            self.client_id = secrets['client_id']
            self.client_secret = secrets['client_secret']
            self.redirect_uri = secrets['redirect_uris'][0]
            self.token_uri = secrets['token_uri']
            self.extra = {
                'client_id': self.client_id,
                'client_secret': self.client_secret}

        except (JSONDecodeError, IOError):
            print('missing or bad secrets file: {}'.format(secrets_file))
            exit(1) 
Example 56
Project: gphotos-sync   Author: gilesknap   File: authorize.py    MIT License 5 votes vote down vote up
def load_token(self) -> Optional[str]:
        try:
            with self.token_file.open('r') as stream:
                token = load(stream)
        except (JSONDecodeError, IOError):
            return None
        return token 
Example 57
Project: Recession-Predictor   Author: tzhangwps   File: make_dataset.py    MIT License 5 votes vote down vote up
def get_fred_data(self):
        """
        Cycles through "fred_series"ids" to get data from the FRED API.
        """
        import time
        
        now = datetime.now()
        month = now.strftime('%m')
        year = now.year        
        most_recent_date = '{}-{}-07'.format(year, month)
        print('\nGetting data from FRED API as of {}...'.format(most_recent_date))
        
        for series_name in list(self.fred_series_ids.keys()):
            series_data = DataSeries()
            series_id = self.fred_series_ids[series_name]
            print('\t|--Getting data for {}({}).'.format(series_name, series_id))
            params = {'series_id': series_id,
                      'api_key': path.fred_api_key,
                      'file_type': 'json',
                      'sort_order': 'desc',
                      'realtime_start': most_recent_date,
                      'realtime_end': most_recent_date}
            success = False
            while success == False:
                try:
                    series_data.fred_response(params)
                except json.JSONDecodeError:
                    delay = 5
                    print('\t --CONNECTION ERROR--',
                          '\n\t Sleeping for {} seconds.'.format(delay))
                    time.sleep(delay) 
                else:
                    success = True
            self.primary_dictionary_output[series_name] = series_data
        print('Finished getting data from FRED API!') 
Example 58
Project: TGC-Designer-Tools   Author: chadrockey   File: usgs_lidar_parser.py    Apache License 2.0 5 votes vote down vote up
def wkt_to_epsg(wkt, printf=print):
    '''
    Function borrowed from https://github.com/cmollet/sridentify, don't want or need the local database aspects
    Copyright 2018 Cory Mollet

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    '''
    url = 'http://prj2epsg.org/search.json?'
    params = {
        'mode': 'wkt',
        'terms': wkt
    }
    req = urlopen(url + urlencode(params), timeout=5.0)
    raw_resp = req.read()
    try:
        resp = json.loads(raw_resp.decode('utf-8'))
    except json.JSONDecodeError:
        printf('API call succeeded but response\
                is not JSON: %s' % raw_resp)
        return None
    except:
        printf("Problem with prj2epsg.org, try running the tool later.")
        raise

    return int(resp['codes'][0]['code']) 
Example 59
Project: statsy   Author: fourjr   File: fortnite.py    GNU General Public License v3.0 5 votes vote down vote up
def post(self, endpoint, payload, *, reason='command'):
        headers = {
            'Authorization': os.getenv('fortnite'),
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        speed = time.time()
        async with self.session.post(
            'https://fortnite-public-api.theapinetwork.com/prod09' + endpoint,
            data=urlencode(payload), headers=headers
        ) as resp:
            speed = speed - time.time()
            datadog.statsd.increment('statsy.api_latency', 1, [
                'game:fortnite', f'speed:{speed}', f'method:{endpoint}'
            ])
            datadog.statsd.increment('statsy.requests', 1, [
                'game:fortnite', f'code:{resp.status}', f'method:{endpoint}', f'reason:{reason}'
            ])
            if resp.status != 200:
                raise utils.APIError
            try:
                data = await resp.json()
                if not data:
                    raise utils.APIError
            except (json.JSONDecodeError, aiohttp.client_exceptions.ContentTypeError):
                raise utils.APIError
            else:
                return data 
Example 60
Project: statsy   Author: fourjr   File: clashroyale.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, bot):
        self.bot = bot
        self.conv = TagCheck()
        self.cache = TTLCache(500, 180)
        scopes = [
            "https://www.googleapis.com/auth/userinfo.email",
            "https://www.googleapis.com/auth/firebase.database"
        ]
        self.firebase = ServiceAccountCredentials.from_json_keyfile_dict(json.loads(b64decode(os.getenv('firebase')).decode()), scopes=scopes)

        try:
            constants = json.loads(requests.get('https://fourjr.herokuapp.com/cr/constants').text)
        except json.JSONDecodeError:
            constants = None
        self.cr = clashroyale.OfficialAPI(
            os.getenv('clashroyale'),
            session=self.bot.session,
            is_async=True,
            timeout=20,
            constants=constants,
            url=f"http://{os.getenv('spike')}/redirect?url=https://api.clashroyale.com/v1"
        )
        self.royaleapi = clashroyale.RoyaleAPI(
            os.getenv('royaleapi'),
            session=self.bot.session,
            is_async=True,
            timeout=20
        )

        if not self.bot.dev_mode:
            self.bot.clan_update = self.bot.loop.create_task(self.clan_update_loop()) 
Example 61
Project: normandy   Author: mozilla   File: validators.py    Mozilla Public License 2.0 5 votes vote down vote up
def validate_json(value):
    """
    Validate that a given value can be successfully parsed as JSON.
    """
    try:
        json.loads(value)
    except json.JSONDecodeError as err:
        raise ValidationError("%s is not valid JSON: %s", params=(value, err.msg)) 
Example 62
Project: django-boost   Author: ChanTsune   File: mixins.py    MIT License 5 votes vote down vote up
def __json(self):
        try:
            encoding = self.get_encoding()
            return json.loads(self.request.body.decode(encoding))
        except json.JSONDecodeError:
            return {} 
Example 63
Project: pymovie   Author: bob-anderson-ok   File: astrometry_client.py    MIT License 5 votes vote down vote up
def json2python(data):
    try:
        return json.loads(data)
    except json.JSONDecodeError:
        pass
    return None 
Example 64
Project: insights_connexion   Author: RedHatInsights   File: app.py    MIT License 5 votes vote down vote up
def log_middleware(request, handler):
    try:
        req_body = await request.json() if request.has_body else None
    except JSONDecodeError as e:
        log.error(e)
        return responses.invalid_request_parameters('Malformed JSON in request body.')

    req_id = str(uuid.uuid4())

    log.info({'message': req_id,
              'type': 'request',
              'req_id': req_id,
              'body': req_body,
              'cookies': dict(request.cookies),
              'content-type': request.content_type,
              'content-length': request.content_length,
              'headers': dict(request.headers),
              'method': request.method,
              'query': dict(request.query),
              'url': str(request.url)})

    response = await handler(request)

    log.info({'message': req_id,
              'type': 'response',
              'req_id': req_id,
              'body': response.text,
              'cookies': dict(response.cookies),
              'content_type': response.content_type,
              'content_length': response.content_length,
              'headers': dict(response.headers),
              'status_code': response.status})

    return response 
Example 65
Project: maubot   Author: maubot   File: client.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def create_client(request: web.Request) -> web.Response:
    try:
        data = await request.json()
    except JSONDecodeError:
        return resp.body_not_json
    return await _create_client(None, data) 
Example 66
Project: maubot   Author: maubot   File: client.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def update_client(request: web.Request) -> web.Response:
    user_id = request.match_info.get("id", None)
    client = Client.get(user_id, None)
    try:
        data = await request.json()
    except JSONDecodeError:
        return resp.body_not_json
    if not client:
        return await _create_client(user_id, data)
    else:
        return await _update_client(client, data) 
Example 67
Project: maubot   Author: maubot   File: instance.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def update_instance(request: web.Request) -> web.Response:
    instance_id = request.match_info.get("id", "").lower()
    instance = PluginInstance.get(instance_id, None)
    try:
        data = await request.json()
    except JSONDecodeError:
        return resp.body_not_json
    if not instance:
        return await _create_instance(instance_id, data)
    else:
        return await _update_instance(instance, data) 
Example 68
Project: maubot   Author: maubot   File: upload.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def upload_file(file: IO, server: str, token: str) -> None:
    req = Request(f"{server}/_matrix/maubot/v1/plugins/upload?allow_override=true", data=file,
                  headers={"Authorization": f"Bearer {token}", "Content-Type": "application/zip"})
    try:
        with urlopen(req) as resp_data:
            resp = json.load(resp_data)
            print(f"{Fore.GREEN}Plugin {Fore.CYAN}{resp['id']} v{resp['version']}{Fore.GREEN} "
                  f"uploaded to {Fore.CYAN}{server}{Fore.GREEN} successfully.{Fore.RESET}")
    except HTTPError as e:
        try:
            err = json.load(e)
        except json.JSONDecodeError:
            err = {}
        print(err.get("stacktrace", ""))
        print(Fore.RED + "Failed to upload plugin: " + err.get("error", str(e)) + Fore.RESET) 
Example 69
Project: dove   Author: vishalkuo   File: dove.py    MIT License 5 votes vote down vote up
def _load_config(config: str) -> Dict[any, any]:
    if not path.exists(config):
        click.secho(f"Config not found at {config}", fg="red")
        sys.exit(1)

    parsed_config = None
    try:
        with open(config, "r") as f:
            data = f.read()
            parsed_config = json.loads(data)
    except json.JSONDecodeError as ex:
        click.secho(f"Could not decode file: {ex}", fg="red")
        sys.exit(1)

    return parsed_config 
Example 70
Project: mirror-gitlab-gitea   Author: JeanRibes   File: helpers.py    GNU General Public License v3.0 5 votes vote down vote up
def load_config(args):
    try:
        with open(args.config_file, 'r') as json_config:
            try:
                data = json.load(json_config)
                args.__dict__.update(data)
            except (json.JSONDecodeError):
                logger.exception("Error readind file {}".format(args.config_file))
    except FileNotFoundError:
        pass 
Example 71
Project: DiscordEmojiPy   Author: Algueem   File: requestmanager.py    MIT License 5 votes vote down vote up
def request(self, method, url):
        await self.client.save_cache()
        running = os.getcwd()
        cache = json.load(open(running + '/.demojicache/cache.json', 'r'))
        met = cache.get(method)
        if met:
            if method in ('all', 'stats'):
                if datetime.utcnow() <= datetime.strptime(cache[method]['request_after'], "%Y-%m-%d %H:%M:%S.%f"):
                    return cache[method]['info']
                else:
                    pass
            else:
                return cache[method]['info']

        async with aiohttp.ClientSession() as session:
            async with session.request(method, url) as response:
                if response.status == 200:
                    try:
                        res = await response.json()
                        cache[method] = {}
                        cache[method]['info'] = res
                        if method in ('all', 'stats'):
                            cache[method]['request_after'] = f'{datetime.utcnow() + timedelta(minutes=30)}'
                        with open(running + '/.demojicache/cache.json', 'w') as fp:
                            json.dump(cache, fp, indent=4)
                        return res
                    except json.JSONDecodeError:
                        raise RequestFailed("Can't make request. Try again later.")
                elif response.status == 429:
                    raise RequestFailed("Can't make request. Try again later.") 
Example 72
Project: dephell   Author: dephell   File: cache.py    MIT License 5 votes vote down vote up
def load(self):
        if not self.path.exists():
            return None
        with self.path.open('r') as stream:
            try:
                return json.load(stream)
            except json.JSONDecodeError:
                return None
        return None 
Example 73
Project: qpid-bow   Author: Bynder   File: message_send.py    MIT License 5 votes vote down vote up
def message_send(args):
    properties = None
    if args.properties_file:
        try:
            properties = json.load(args.properties_file)
        except json.JSONDecodeError:
            print("Invalid JSON properties file\n")
            args.parser.print_help()
            exit(EX_DATAERR)

    message = create_message(args.message.encode(), properties)
    sender = Sender(args.address, args.broker_url)
    sender.queue(repeat(message, args.repeat))
    sender.send() 
Example 74
Project: rail-line-animation-toolkit   Author: ZHZHH-Arezrh5100   File: query_schedule_12306.py    GNU General Public License v3.0 4 votes vote down vote up
def query_single_train(train_name='G1', date=None, default_params=None):

    # search train No. by train name (根据车次查车次编号)
    # query JSON data from 12306 (下载JSON数据)
    url_train = r'https://search.12306.cn/search/v1/train/search'  # the url may be changed, check the latest on 12306.cn
    parameter_dict = {'keyword': train_name,
                      'date': (datetime.date.today() + datetime.timedelta(days=1)).strftime(
        '%Y%m%d') if date is None else date.replace('-', '')}
    req = request.Request(url='%s%s%s' % (url_train, '?', parse.urlencode(parameter_dict)))
    try:
        train_res = request.urlopen(req)
    except (HTTPError, URLError):
        print('Failed to get train code from 12306!')
        return None
    else:
        train_str = train_res.read()
    # decode JSON to Object (JSON转为对象)
    try:
        train_json = json.loads(train_str)
    except json.JSONDecodeError:
        print('Failed to decode train information from 12306!')
        # print(train_str.decode('utf-8'))
        return None
    # validate train name (验证查到的车次)
    train_no = None
    if isinstance(train_json, dict):
        if 'data' in train_json:
            if isinstance(train_json['data'], list):
                for train_data in train_json['data']:
                    if isinstance(train_data, dict):
                        if ('station_train_code' in train_data) and ('train_no' in train_data):
                            if train_data['station_train_code'] == train_name:
                                train_no = train_data['train_no']
                                break
    if train_no is None:
        print('Train {0} not found!'.format(train_name))
        # print(train_json)
        return None
    
    # train info (整理列车信息)
    train = {'train_no': train_no, 'train_name': train_name, 'from': 'XXX', 'to': 'XXX', 'destination': 'XXX'}
    train['train_class'] = train_name[0] if train_name[0].isalpha() else '0'

    # set other params (设置其他参数)
    if default_params is None:
        default_params = ['1', '1', 'D']
    train['icon_id'] = default_params[0]
    train['class_color'] = train['train_class']
    train['destination_color'] = 'TO_OTHER_LINES'

    # query stop information (查询停站时刻信息)
    print('Querying stops info...')
    csv_df = query_stops_of(train, date, default_params[1], default_params[2], 1)
    
    return csv_df


# query the information of trains between two stations (查询站间列车信息) 
Example 75
Project: trelliolibs   Author: quikmile   File: parsers.py    MIT License 4 votes vote down vote up
def multipart_parser(request, file_handler=default_file_handler):
    """
    :param file_handler: callable to save file, this should always return the file path
    :return: dictionary containing files and data
    """
    multipart_data = {
        'files': {},
        'data': {}
    }
    if request.content_type == 'multipart/form-data':
        reader = MultipartReader.from_response(request)

        while True:
            part = await reader.next()
            if part is None:
                break
            if isinstance(part, BodyPartReader):
                if part.filename:
                    # if body is binary file
                    if file_handler:
                        # in case we just want to parse data and not save file actually e.g. in validator
                        file_data = await part.read(decode=True)
                        file_data = part.decode(file_data)
                        file_path = await file_handler(part.filename, file_data, part.headers[CONTENT_TYPE])
                    else:
                        file_path = part.filename
                    multipart_data['files'][part.name] = file_path
                elif part.text():
                    # if body is text
                    text = await part.text()
                    multipart_data['data'][part.name] = text
                else:
                    # if body is json or form (not text), not handling this
                    continue
            else:
                # if part is recursive multipart , not handling this right now
                # TODO: do recursive call to handle this
                continue
    else:
        try:
            multipart_data['data'] = await request.json()
        except json.JSONDecodeError:
            pass
    return multipart_data 
Example 76
Project: ghi   Author: gkrizek   File: github.py    MIT License 4 votes vote down vote up
def getPool(payload, pools):

    try:
        payload = json.loads(payload)
    except json.JSONDecodeError as e:
        message = "There was a problem parsing payload: %s" % e
        logging.error(message)
        return {
            "statusCode": 400,
            "body": json.dumps({
                "success": False,
                "message": message
            })
        }
    ownerPool = None
    repo = payload["repository"]["full_name"]

    for pool in pools:
        if pool.containsRepo(repo):
            ownerPool = pool
            break

    if ownerPool is None:
        message = "Received repository '%s', but no pool is configured for it." % repo
        logging.info(message)
        return {
            "statusCode": 202,
            "body": json.dumps({
                "success": True,
                "message": message
            })
        }
    else:
        # get the secret for this repo
        for requestedRepo in ownerPool.repos:
            if repo == requestedRepo["name"]:
                repoName = requestedRepo["name"]
                repoSecret = requestedRepo["secret"]
                repoVerify = requestedRepo["verify"]
        logging.info("Matched repo '{}' to pool '{}'".format(repoName,ownerPool.name))
        return {
            "statusCode": 200,
            "pool": ownerPool,
            "name": repoName,
            "secret": repoSecret,
            "verify": repoVerify
        } 
Example 77
Project: mist.api   Author: mistio   File: actions.py    Apache License 2.0 4 votes vote down vote up
def run(self, resource, *args, **kwargs):
        from mist.api.config import CORE_URI
        resource_type = getattr(self._instance, 'resource_model_name', 'org')
        if resource_type == 'org':
            resource_url = CORE_URI
        else:
            resource_url = '%s/%ss/%s' % (CORE_URI, resource_type, resource.id)
        if hasattr(resource, "name"):
            resource_name = resource.name
        elif hasattr(resource, "title"):
            resource_name = resource.title
        else:
            resource_name = 'unknown'
        if self.json:
            json_body = self.json.replace(
                "{resource_id}", getattr(resource, 'id', '')).replace(
                    "{resource_url}", resource_url).replace(
                        "{resource_name}", resource_name)
            json_body = json.loads(json_body)
        else:
            json_body = None
        if self.data:
            data = self.data.replace(
                "{resource_id}", getattr(resource, 'id', '')).replace(
                    "{resource_url}", resource_url).replace(
                        "{resource_name}", resource.name)
        else:
            data = None
        headers = json.loads(self.headers) if self.headers else None
        response = requests.request(
            self.method, self.url, params=self.params, data=data,
            json=json_body, headers=headers)

        # Notify user & admin if response indicates an error
        if not response.ok:
            title = "Webhook for rule `%s` responded with http code `%d`" % (
                self._instance.title, response.status_code)
            try:
                body = "URL: %s\n Response body: %s\n" % (
                    self.url, str(response.json()))
            except json.JSONDecodeError:
                body = "URL: %s\n Response body: %s\n" % (
                    self.url, response.text)
            log.info("%s - %s - %s", title, self._instance.id, body)
            from mist.api.methods import notify_user, notify_admin
            notify_user(self._instance.owner, title, message=body)
            notify_admin(title + ' ' + self._instance.id, body)
        return {'status_code': response.status_code} 
Example 78
Project: catapult   Author: Tessian   File: release.py    MIT License 4 votes vote down vote up
def _get_release(client, bucket, key, version_id=None) -> Release:
    """
    Fetches a release from a S3 object.

    Arguments:
        client (botocore.client.S3): client for AWS S3.
        bucket (str): bucket's name.
        key (str): object's key.
        version_id (str or None): version ID of the S3 object.
            If the `version_id` is `None`, it will return the latest release.

    Returns:
        Release or None: the release stored in the object.
    """
    extras = {}

    if version_id is not None:
        extras["VersionId"] = version_id

    try:
        resp = client.get_object(Bucket=bucket, Key=key, **extras)

    except client.exceptions.NoSuchKey:
        raise InvalidRelease(f"Key not found: {key}")

    try:
        body = json.load(resp["Body"])

    except json.JSONDecodeError as exc:
        raise InvalidRelease("Invalid JSON data") from exc

    try:
        version = body["version"]
        commit = body["commit"]
        image = body["image"]
        author = body["author"]
        rollback = body.get("rollback", False)

    except KeyError as exc:
        raise InvalidRelease(f"Missing property in JSON: {exc}")

    if "VersionId" not in resp:
        # files created when the bucket had the versioning disabled
        raise InvalidRelease("Object has no S3 VersionId")

    return Release(
        version=version,
        commit=commit,
        changelog=body.get("changelog", "<changelog unavailable>"),
        version_id=resp["VersionId"],
        image=image,
        timestamp=resp["LastModified"],
        author=author,
        rollback=rollback,
    ) 
Example 79
Project: LibCloud   Author: stewnorriss   File: pricing.py    Apache License 2.0 4 votes vote down vote up
def download_pricing_file(file_url=DEFAULT_FILE_URL,
                          file_path=CUSTOM_PRICING_FILE_PATH):
    """
    Download pricing file from the file_url and save it to file_path.

    :type file_url: ``str``
    :param file_url: URL pointing to the pricing file.

    :type file_path: ``str``
    :param file_path: Path where a download pricing file will be saved.
    """
    dir_name = os.path.dirname(file_path)

    if not os.path.exists(dir_name):
        # Verify a valid path is provided
        msg = ('Can\'t write to %s, directory %s, doesn\'t exist' %
               (file_path, dir_name))
        raise ValueError(msg)

    if os.path.exists(file_path) and os.path.isdir(file_path):
        msg = ('Can\'t write to %s file path because it\'s a'
               ' directory' % (file_path))
        raise ValueError(msg)

    response = get_response_object(file_url)
    body = response.body

    # Verify pricing file is valid
    try:
        data = json.loads(body)
    except JSONDecodeError:
        msg = 'Provided URL doesn\'t contain valid pricing data'
        raise Exception(msg)

    # pylint: disable=maybe-no-member
    if not data.get('updated', None):
        msg = 'Provided URL doesn\'t contain valid pricing data'
        raise Exception(msg)

    # No need to stream it since file is small
    with open(file_path, 'w') as file_handle:
        file_handle.write(body) 
Example 80
Project: NEMCore   Author: nnnewb   File: api.py    MIT License 4 votes vote down vote up
def request(self,
                method: str,
                path: str,
                params: Mapping = None,
                default: Mapping = None,
                custom_cookies: Mapping = None) -> Mapping:
        """ 发送请求

        这个函数会准备好csrf token、将请求的api路径转换为合法的url、处理并保存cookies。

        如果服务器返回的响应提示错误,会抛出``NetEaseError``。

        :param method: 请求的HTTP方法
        :param path: 请求的API路径
        :param params: 请求的参数,当方法是GET时使用Query String传递,使用POST时使用form传递。
        :param default: 响应内容为空时或无效时使用的默认响应内容,注意``message``字段会在无法解析响应json时替换成``unable to decode response``。
        :param custom_cookies: 自定义的cookies,注意,这些cookies会在本次会话中一直存在,直到你退出登录或被服务器丢弃。
        :return: 返回服务端的响应内容
        """
        if not params:
            params = {}

        if not default:
            default = {'code': -1}

        if not custom_cookies:
            custom_cookies = {}

        endpoint = '{}{}'.format(c.BASE_URL, path)
        csrf_token = ''
        for cookie in self.session.cookies:
            if cookie.name == '__csrf':
                csrf_token = cookie.value
                break
        params.update({'csrf_token': csrf_token})
        data = default

        for key, value in custom_cookies.items():
            cookie = make_cookie(key, value)
            self.session.cookies.set_cookie(cookie)

        params = encrypted_request(params)
        try:
            resp = self._raw_request(method, endpoint, params)
            data = resp.json()
        except (json.JSONDecodeError, ValueError) as e:  # noqa: F841
            self.logger.error('response:\n{}'.format(path, resp.text[:200]))
            data['message'] = 'unable to decode response'
            return data

        raise_for_code(data)
        return data