コード例 #1
0
ファイル: client.py プロジェクト: yx1819351140/jingping
    def _recv(self, _api):
        fname, mtype, rseqid = yield from self._iprot.read_message_begin()
        if mtype == TMessageType.EXCEPTION:
            x = TApplicationException()
            yield from self._iprot.read_struct(x)
            yield from self._iprot.read_message_end()
            raise x
        result = getattr(self._service, _api + "_result")()
        yield from self._iprot.read_struct(result)
        yield from self._iprot.read_message_end()

        if hasattr(result, "success") and result.success is not None:
            return result.success

        # void api without throws
        if len(result.thrift_spec) == 0:
            return

        # check throws
        for k, v in result.__dict__.items():
            if k != "success" and v:
                raise v

        # no throws & not void api
        if hasattr(result, "success"):
            raise TApplicationException(TApplicationException.MISSING_RESULT)
コード例 #2
0
    async def _call(self, method: str, args: Sequence[Any], kwargs):
        reader_trans = AsyncBytesIO()
        writer_trans = AsyncBytesIO()
        iproto = self._protocol_cls(reader_trans)
        oproto = self._protocol_cls(writer_trans)
        try:
            self._seqid += 1

            # Write to the output buffer with Thrift.
            kw = args_to_kwargs(
                getattr(self._service, method + "_args").thrift_spec, *args)
            kwargs.update(kw)
            oproto.write_message_begin(method, TMessageType.CALL, self._seqid)
            api_args = getattr(self._service, method + '_args')()
            for k, v in kwargs.items():
                setattr(api_args, k, v)
            api_args.write(oproto)
            oproto.write_message_end()

            # Switch over the control to Callosum so that it can perform send/recv
            # with its own lower transport layer.
            raw_request_body = writer_trans.getvalue()
            raw_response_body = yield raw_request_body
            if raw_response_body is None:
                yield None
                return
            reader_trans.write(raw_response_body)
            reader_trans.seek(0, io.SEEK_SET)

            # Now read the response buffer with Thrift.
            fname, mtype, rseqid = await iproto.read_message_begin()
            if rseqid != self._seqid:
                raise TApplicationException(
                    TApplicationException.BAD_SEQUENCE_ID,
                    fname + ' failed: out of sequence response')

            if mtype == TMessageType.EXCEPTION:
                x = TApplicationException()
                await iproto.read_struct(x)
                await iproto.read_message_end()
                raise x
            result = getattr(self._service, method + '_result')()
            await iproto.read_struct(result)
            await iproto.read_message_end()

            if hasattr(result, "success") and result.success is not None:
                yield result.success
            if len(result.thrift_spec) == 0:
                yield None
            for k, v in result.__dict__.items():
                if k != 'success' and v:
                    raise v
        finally:
            writer_trans.close()
            reader_trans.close()
コード例 #3
0
    async def process_in(self, iprot):
        api, type, seqid = await iprot.read_message_begin()
        if api not in self._service.thrift_services:
            await iprot.skip(TType.STRUCT)
            await iprot.read_message_end()
            return (
                api,
                seqid,
                TApplicationException(TApplicationException.UNKNOWN_METHOD),
                None,
            )

        args = getattr(self._service, api + "_args")()
        await iprot.read_struct(args)
        await iprot.read_message_end()
        result = getattr(self._service, api + "_result")()

        # convert kwargs to args
        api_args = [args.thrift_spec[k][1] for k in sorted(args.thrift_spec)]

        async def call():
            f = getattr(self._handler, api)
            arguments = (args.__dict__[k] for k in api_args)
            if asyncio.iscoroutinefunction(f):
                rv = await f(*arguments)
                return rv
            return f(*arguments)

        return api, seqid, result, call
コード例 #4
0
ファイル: hahaHba.py プロジェクト: oimq/hahaHba
 def error(self, e, msg="", ex=False, cry=False):
     if type(e) == type(TApplicationException()):
         print("\nERROR {} : {}\n".format(msg, "TApplicationException"))
     else:
         print("\nERROR {} : {}\n".format(msg, e))
     if cry:
         traceback.print_exc()
         print("\n")
     if ex: exit()
コード例 #5
0
    def _negotiation(self):
        self._oprot.write_message_begin(track_method, TMessageType.CALL,
                                        self._seqid)
        args = track_thrift.UpgradeArgs()
        self.tracker.init_handshake_info(args)
        args.write(self._oprot)
        self._oprot.write_message_end()
        self._oprot.trans.flush()

        api, msg_type, seqid = self._iprot.read_message_begin()
        if msg_type == TMessageType.EXCEPTION:
            x = TApplicationException()
            x.read(self._iprot)
            self._iprot.read_message_end()
            raise x
        else:
            result = track_thrift.UpgradeReply()
            result.read(self._iprot)
            self._iprot.read_message_end()
コード例 #6
0
ファイル: client.py プロジェクト: yx1819351140/jingping
    def _req(self, _api, *args, **kwargs):
        try:
            kwargs = args_to_kwargs(
                getattr(self._service, _api + "_args").thrift_spec, *args,
                **kwargs)
        except ValueError as e:
            raise TApplicationException(
                TApplicationException.UNKNOWN_METHOD,
                'missing required argument {arg} for {service}.{api}'.format(
                    arg=e.args[0], service=self._service.__name__, api=_api))
        result_cls = getattr(self._service, _api + "_result")

        yield from self._send(_api, **kwargs)
        # wait result only if non-oneway
        if not getattr(result_cls, "oneway"):
            return (yield from self._recv(_api))
コード例 #7
0
    def _process_in(self, api, iprot):
        if api not in self._service.thrift_services:
            iprot.skip(TType.STRUCT)
            iprot.read_message_end()
            return TApplicationException(
                TApplicationException.UNKNOWN_METHOD), None

        args = getattr(self._service, api + "_args")()
        args.read(iprot)
        iprot.read_message_end()
        result = getattr(self._service, api + "_result")()

        # convert kwargs to args
        api_args = [args.thrift_spec[k][1] for k in sorted(args.thrift_spec)]

        def call():
            return getattr(self._handler,
                           api)(*(args.__dict__[k] for k in api_args))

        return result, call
コード例 #8
0
ファイル: processor.py プロジェクト: zjx-ERROR/thriftpy2
    def process_in(self, iprot):
        api, type, seqid = yield from iprot.read_message_begin()
        if api not in self._service.thrift_services:
            yield from iprot.skip(TType.STRUCT)
            yield from iprot.read_message_end()
            return api, seqid, TApplicationException(
                TApplicationException.UNKNOWN_METHOD), None  # noqa

        args = getattr(self._service, api + "_args")()
        yield from iprot.read_struct(args)
        yield from iprot.read_message_end()
        result = getattr(self._service, api + "_result")()

        # convert kwargs to args
        api_args = [args.thrift_spec[k][1] for k in sorted(args.thrift_spec)]

        @asyncio.coroutine
        def call():
            f = getattr(self._handler, api)
            return (yield from f(*(args.__dict__[k] for k in api_args)))

        return api, seqid, result, call
コード例 #9
0
ファイル: test_rpc.py プロジェクト: aiudirog/thriftpy2
 def raises(self, msg):
     raise TApplicationException(message=msg)