Exemple #1
0
def test_serialize_model():
    song = SongModel(identifier='1', title='', artists=[], duration=0)
    song_js = serialize('python', song)
    assert song_js['identifier'] == '1'

    song_js = serialize('python', song, brief=True)
    assert song_js['identifier'] == '1'

    song_js = serialize('python', song, fetch=True)
    assert song_js['identifier'] == '1'
Exemple #2
0
def test_serialize_app(mocker):
    app = mocker.Mock(spec=App)
    app.live_lyric = mocker.Mock()
    app.live_lyric.current_sentence = ''
    player = MpvPlayer()
    app.player = player
    app.playlist = player.playlist
    for format in ('plain', 'json'):
        serialize(format, app, brief=False)
        serialize(format, app, fetch=True)
    player.shutdown()
Exemple #3
0
def handle_request(req, app, ctx=None):
    """
    :type req: feeluown.rpc.Request
    """
    if not _REGISTERED:
        register_feeluown_serializers()

    cmd = Cmd(req.cmd, *req.cmd_args, options=req.cmd_options)
    ok, body = exec_cmd(cmd, app=app)
    format = req.options.get('format', 'plain')
    msg = serialize(format, body, brief=False)
    return Response(ok=ok, text=msg, req=req)
Exemple #4
0
def test_serialize_models():
    for cls_name in (
            'Song',
            'Album',
            'Playlist',
            'Artist',
            'User',
    ):
        model = getattr(dummy_provider, cls_name).get(Dummy)
        for format in ('plain', 'json'):
            serialize(format, model)
            serialize(format, model, as_line=True)
            serialize(format, model, as_line=True, fetch=True)
            serialize(format, model, as_line=False, brief=True, fetch=True)
Exemple #5
0
 def publish(self, obj, topic, need_serialize=False):
     # NOTE: use queue? maybe.
     subscribers = self._relations[topic]
     for subscriber in subscribers.copy():
         try:
             if need_serialize is True:
                 msg = serialize('json', obj, brief=False)
             else:
                 msg = obj
             subscriber.write_topic_msg(topic, msg)
         except DeadSubscriber:
             # NOTE: need lock?
             self.remove_subscriber(subscriber)
Exemple #6
0
async def handle_request(
        req: Request,
        app,
        session: Optional[SessionLike] = None,
) -> Response:
    """
    :type req: feeluown.server.rpc.Request
    """
    cmd = Cmd(req.cmd, *req.cmd_args, options=req.cmd_options)

    if session is not None and \
       session.options.rpc_version == '1.0' and \
       cmd.action not in V1_SUPPORTTED_CMDS:
        handler_cls = None
    else:
        handler_cls = cmd_handler_mapping.get(cmd.action)

    ok, body = False, ''
    if handler_cls is None:
        ok, body = False, f"handler for cmd:{req.cmd} not found"
    else:
        try:
            handler: AbstractHandler = handler_cls(app=app, session=session)
            if handler.support_aio_handle is True:
                rv = await handler.a_handle(cmd)
            else:
                # FIXME: handlers do blocking io should implement a_handle method.
                rv = handler.handle(cmd)
        except HandlerException as e:
            ok, body = False, str(e)
        except Exception as e:  # pylint: disable=broad-except
            logger.exception(f'handle cmd({cmd}) error')
            ok, body = False, f'internal server error\n{str(e)}'
        else:
            rv = rv if rv is not None else ''
            ok, body = True, rv
    fmt = req.options.get('format', None)
    fmt = fmt or 'plain'
    msg = serialize(fmt, body, brief=False)
    return Response(ok=ok, text=msg, req=req)
Exemple #7
0
def test_serialize_model_v1():
    song = SongModelV1(identifier='1', title='', artists=[], duration=0)
    song_js = serialize('python', song)
    assert song_js['identifier'] == '1'
Exemple #8
0
def test_serialize_providers():
    for format in ('plain', 'json'):
        serialize(format, [dummy_provider], brief=False)
        serialize(format, [dummy_provider])
Exemple #9
0
def test_serialize_provider():
    for format in ('plain', 'json'):
        serialize(format, dummy_provider, brief=False)
        serialize(format, dummy_provider, as_line=True)