python链式调用REST API把参数放到URL中

需求格式:GET /users/:user/repos

程序:

class Chain(object):
    def __init__(self,path=''):
        self._path=path

    def __getattr__(self, path):
        return Chain( '%s/%s' %(self._path,path))

    def __str__(self):
        return self._path

    def __call__(self, user):
        return Chain('%s/%s' %(self._path,user))

    __repr__=__str__

实例:

1 print('GET',Chain().users('michael').repos)
2 print('GET',Chain().status.user.timeline.list)

结果:

1 GET /users/michael/repos
2 GET /status/user/timeline/list

猜你喜欢

转载自blog.csdn.net/qq_38140936/article/details/103577954
今日推荐