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


Python URLObject.add_path_segment方法代码示例

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


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

示例1: Connection

# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import add_path_segment [as 别名]
class Connection(object):

    def __init__(self, url, email, token, name='default', version='v1',
                 cache=None):
        self.session = Connection._get_session(email, token)
        self.email = email
        self.base_url = URLObject(url)
        self.api_url = self.base_url.add_path_segment(version)
        self.cache = InMemoryCache() if cache is None else cache
        self.name = name

    def http_method(self, method, url, *args, **kwargs):
        """
        Send HTTP request with `method` to `url`.
        """
        method_fn = getattr(self.session, method)
        return method_fn(url, *args, **kwargs)

    def build_absolute_url(self, path):
        """
        Resolve relative `path` against this connection's API url.
        """
        return url_join(self.api_url, path)

    @staticmethod
    def _get_session(email, token):
        session = requests.Session()
        defaults = {
            'X-PW-Application': 'developer_api',
            'X-PW-AccessToken': token,
            'X-PW-UserEmail': email,
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        }
        session.headers.update(defaults)
        return session

    def __getattr__(self, name):
        """
        Turn HTTP verbs into http_method calls so e.g. conn.get(...) works.

        Note that 'get' and 'delete' are special-cased to handle caching
        """
        methods = 'post', 'put', 'patch', 'options'
        if name in methods:
            return functools.partial(self.http_method, name)
        return super(Connection, self).__getattr__(name)

    def get(self, url, *args, **kwargs):
        cached = self.cache.get(url)
        if cached is None:
            cached = self.http_method('get', url, *args, **kwargs)
            self.cache.set(url, cached, max_age=seconds(minutes=5))
        return cached

    def delete(self, url, *args, **kwargs):
        resp = self.http_method('delete', url, *args, **kwargs)
        if resp.ok:
            self.cache.clear(url)
        return resp
开发者ID:markhellewell,项目名称:prospyr,代码行数:62,代码来源:connection.py

示例2: test_add_path_segment_adds_a_path_segment

# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import add_path_segment [as 别名]
 def test_add_path_segment_adds_a_path_segment(self):
     url = URLObject('https://github.com/zacharyvoase/urlobject')
     assert (url.add_path_segment('tree') ==
             'https://github.com/zacharyvoase/urlobject/tree')
     assert (url.add_path_segment('tree/master') ==
             'https://github.com/zacharyvoase/urlobject/tree%2Fmaster')
开发者ID:vmalloc,项目名称:urlobject,代码行数:8,代码来源:urlobject_test.py


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