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


Python FuturesSession.options方法代码示例

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


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

示例1: CrestAPI

# 需要导入模块: from requests_futures.sessions import FuturesSession [as 别名]
# 或者: from requests_futures.sessions.FuturesSession import options [as 别名]
class CrestAPI():

    RATE_LIMIT = 28

    def __init__(self, base_url, secret, client_id, refresh_token = None, user_agent = 'SHHLD | CRESTv0.1 | [email protected]'):

        self.base_url = base_url
        self.secret = secret
        self.client_id = client_id
        self.refresh_token = refresh_token
        self.access_token = ''
        self.expiration = datetime.now()
        self.user_agent = user_agent
        self.endpoints = None

        # Setup the async session
        self.session = FuturesSession(max_workers=30)
        self.session.headers.update({'User-Agent': self.user_agent})

        # Fetch the base endpoints
        self.endpoints = self.get_endpoints()

    @rate_limited(RATE_LIMIT)
    def get_endpoints(self):

        if self.endpoints:
            return self.endpoints

        # Load all root endpoints
        future = self.session.get(self.base_url)

        # Wait for the result and return the data
        return future.result().json()
        return data

    @rate_limited(RATE_LIMIT)
    def get_endpoint(self, resource, query=None, accept=None, callback=None):

        headers = {}
        if accept:
            headers['Accept'] = accept

        future = self.session.get(resource, params=query, headers=headers, auth=CrestAuth(self.get_access_token()), background_callback=callback)

        if callback is None:
            return future.result()

        return future


    def walk_endpoint(self, resource, query=None, accept=None):

        items = []
        while True:

            response = get_endpoint(resource, query, accept).json()

            if 'items' not in response:
                return response

            items += response['items']

            if 'next' not in response:
                break

            resource = response['next']['href']

        return items


    @rate_limited(RATE_LIMIT)
    def get_endpoint_docs(self, resource):

        future = self.session.options(resource)
        result = future.result()

        if result.ok:

            def structureJson():

                json_result = json.loads(result.text)
                for representation in json_result['representations']:
                    if 'contentType' in representation:
                        representation['contentType']['jsonDumpOfStructure'] = json.loads(representation['contentType']['jsonDumpOfStructure'])
                    if 'acceptType' in representation:
                        representation['acceptType']['jsonDumpOfStructure'] = json.loads(representation['acceptType']['jsonDumpOfStructure'])

                return json_result

            result.json = structureJson

        return result


    @rate_limited(RATE_LIMIT)
    def get_access_token(self):

        if datetime.now() < self.expiration:
            return self.access_token

#.........这里部分代码省略.........
开发者ID:tysongg,项目名称:crest-downloader,代码行数:103,代码来源:crestapi_async.py


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