Esempio n. 1
0
 def _decode_result(self, res):
     try:
         result = json_loads(res.content.decode('utf-8', 'ignore'), strict=False)
     except (TypeError, ValueError):
         # Return origin response object if we can not decode it as JSON
         logger.debug('Can not decode response as JSON', exc_info=True)
         return res
     return result
Esempio n. 2
0
 def parse_message(self, msg, signature, timestamp, nonce):
     message = self.crypto.decrypt_message(msg, signature, timestamp, nonce)
     try:
         message = json_loads(to_text(message))
         self.proc_message(message)
     except Exception as e:
         logger.error("proc_message error %s %s", message, e)
     return message
Esempio n. 3
0
    def list(self, offset=0, size=100):
        """
        外部联系人列表

        :param offset: 偏移位置
        :param size: 分页大小,最大100
        :return:
        """
        return self._top_request('dingtalk.corp.ext.list', {
            'offset': offset,
            'size': size
        },
                                 result_processor=lambda x: json_loads(x)
                                 if isinstance(x, six.string_types) else x)
Esempio n. 4
0
    def _handle_result(self,
                       res,
                       method=None,
                       url=None,
                       result_processor=None,
                       top_response_key=None,
                       **kwargs):
        if not isinstance(res, dict):
            # Dirty hack around asyncio based AsyncWeChatClient
            result = self._decode_result(res)
        else:
            result = res

        if not isinstance(result, dict):
            return result
        if top_response_key:
            if 'error_response' in result:
                error_response = result['error_response']
                logger.error("\n【请求地址】: %s\n【请求参数】:%s \n%s\n【错误信息】:%s", url,
                             kwargs.get('params', ''), kwargs.get('data', ''),
                             result)
                raise DingTalkClientException(
                    error_response.get('code', -1),
                    error_response.get('sub_msg',
                                       error_response.get('msg', '')),
                    client=self,
                    request=res.request,
                    response=res)
            top_result = result
            if top_response_key in top_result:
                top_result = result[top_response_key]
                if 'result' in top_result:
                    top_result = top_result['result']
                    if isinstance(top_result, six.string_types):
                        try:
                            top_result = json_loads(top_result)
                        except Exception:
                            pass
            if isinstance(top_result, dict):
                if ('success' in top_result and not top_result['success']) or (
                        'is_success' in top_result
                        and not top_result['is_success']):
                    logger.error("\n【请求地址】: %s\n【请求参数】:%s \n%s\n【错误信息】:%s",
                                 url, kwargs.get('params', ''),
                                 kwargs.get('data', ''), result)
                    raise DingTalkClientException(
                        top_result.get('ding_open_errcode', -1),
                        top_result.get('error_msg', ''),
                        client=self,
                        request=res.request,
                        response=res)
            result = top_result
        if not isinstance(result, dict):
            return result
        if 'errcode' in result:
            result['errcode'] = int(result['errcode'])

        if 'errcode' in result and result['errcode'] != 0:
            errcode = result['errcode']
            errmsg = result.get('errmsg', errcode)

            logger.error("\n【请求地址】: %s\n【请求参数】:%s \n%s\n【错误信息】:%s", url,
                         kwargs.get('params', ''), kwargs.get('data', ''),
                         result)
            raise DingTalkClientException(errcode,
                                          errmsg,
                                          client=self,
                                          request=res.request,
                                          response=res)

        return result if not result_processor else result_processor(result)