当前位置: 首页>>代码示例>>Python>>正文


Python Schema.get_schema方法代码示例

本文整理汇总了Python中schema.Schema.get_schema方法的典型用法代码示例。如果您正苦于以下问题:Python Schema.get_schema方法的具体用法?Python Schema.get_schema怎么用?Python Schema.get_schema使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在schema.Schema的用法示例。


在下文中一共展示了Schema.get_schema方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from schema import Schema [as 别名]
# 或者: from schema.Schema import get_schema [as 别名]
class AniListClient:
    config = {}
    schema = None
    session = None
    auth_expires = 0

    def __init__(self):
        self.schema = Schema()
        self.session = requests.Session()

        self.load_config()
        self.update_auth()

    def load_config(self, config_file='config.yml'):
        with open(config_file) as f:
            self.config = yaml.load(f.read())

    def update_auth(self):
        status, body = self.auth_accessToken({
            'grant_type': 'client_credentials',
            'client_id': self.config['client_id'],
            'client_secret': self.config['client_secret']
        })

        try:
            self.session.headers.update({
                'Authorization': ' '.join(('Bearer', body.get('access_token', '')))
            })
            self.auth_expires = int(body.get('expires', 0))
        except Exception as e:
            # actually do something here
            print(e)

    """
        Proxies to self.send_request

        __getattr__ is called magically when you try to access an attribute not
        explicitly managed by the class. For example, client.anime_search
        will call __getattr__(instance, 'anime_search'), allowing us to build
        an api_call method from the configured schema.
    """
    def __getattr__(self, item):
        return self.send_request(item)

    def send_request(self, signature):
        schema = self.schema.get_schema(signature)

        path = schema.get('path', None)
        method = schema.get('method', 'get').lower()
        params = schema.get('params', [])

        def api_call(data={}):
            if self.auth_expires <= int(time()):
                # self.update_auth()
                pass

            request = self.session.__getattribute__(method)
            if not request:
                # throw bad method in schema
                return False

            # filter out unnecessary pairs in the data dict
            body = {key: data.get(key, False) for key in params}
            res = request(path.format(**data), data=body)
            try:
                return res.status_code, json.loads(res.text)
            except Exception as e:
                return res.status_code, res.text

        return api_call
开发者ID:prototo,项目名称:AniListPythonClient,代码行数:72,代码来源:client.py


注:本文中的schema.Schema.get_schema方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。