Exemplo n.º 1
0
class SyncTransport(Transport):
    """A synchronous transport layer for the API client.

    The file provided in cookie_file is used to store and retrieve
    the authentication cookies for the API.

    The optional agent parameter can be used to specify a custom
    User-Agent string for the API. If not provided, the default
    RBTools User-Agent will be used.

    The optional session can be used to specify an 'rbsessionid'
    to use when authenticating with reviewboard.
    """
    def __init__(self,
                 url,
                 cookie_file=None,
                 username=None,
                 password=None,
                 api_token=None,
                 agent=None,
                 session=None,
                 disable_proxy=False,
                 auth_callback=None,
                 otp_token_callback=None,
                 verify_ssl=True,
                 allow_caching=True,
                 cache_location=None,
                 in_memory_cache=False,
                 save_cookies=True,
                 ext_auth_cookies=None,
                 *args,
                 **kwargs):
        super(SyncTransport, self).__init__(url, *args, **kwargs)
        self.allow_caching = allow_caching
        self.cache_location = cache_location
        self.in_memory_cache = in_memory_cache
        self.server = ReviewBoardServer(self.url,
                                        cookie_file=cookie_file,
                                        username=username,
                                        password=password,
                                        api_token=api_token,
                                        session=session,
                                        disable_proxy=disable_proxy,
                                        auth_callback=auth_callback,
                                        otp_token_callback=otp_token_callback,
                                        verify_ssl=verify_ssl,
                                        save_cookies=save_cookies,
                                        ext_auth_cookies=ext_auth_cookies)

    def get_root(self):
        return self._execute_request(HttpRequest(self.server.url))

    def get_path(self, path, *args, **kwargs):
        if not path.endswith('/'):
            path = path + '/'

        if path.startswith('/'):
            path = path[1:]

        return self._execute_request(
            HttpRequest(self.server.url + path, query_args=kwargs))

    def get_url(self, url, *args, **kwargs):
        if not url.endswith('/'):
            url = url + '/'

        return self._execute_request(HttpRequest(url, query_args=kwargs))

    def login(self, username, password):
        self.server.login(username, password)

    def logout(self):
        self.server.logout()

    def execute_request_method(self, method, *args, **kwargs):
        request = method(*args, **kwargs)

        if isinstance(request, HttpRequest):
            return self._execute_request(request)

        return request

    def _execute_request(self, request):
        """Execute an HTTPRequest and construct a resource from the payload"""
        logging.debug('Making HTTP %s request to %s', request.method,
                      request.url)

        rsp = self.server.make_request(request)
        info = rsp.info()
        mime_type = info['Content-Type']
        item_content_type = info.get('Item-Content-Type', None)

        if request.method == 'DELETE':
            # DELETE calls don't return any data. Everything else should.
            return None
        else:
            payload = rsp.read()
            payload = decode_response(payload, mime_type)

            return create_resource(self,
                                   payload,
                                   request.url,
                                   mime_type=mime_type,
                                   item_mime_type=item_content_type)

    def enable_cache(self):
        """Enable caching for all future HTTP requests.

        The cache will be created at the default location if none is provided.

        If the in_memory parameter is True, the cache will be created in memory
        instead of on disk. This overrides the cache_location parameter.
        """
        if self.allow_caching:
            self.server.enable_cache(cache_location=self.cache_location,
                                     in_memory=self.in_memory_cache)

    def __repr__(self):
        return '<%s(url=%r, cookie_file=%r, agent=%r)>' % (
            self.__class__.__name__, self.url, self.server.cookie_file,
            self.server.agent)
Exemplo n.º 2
0
class SyncTransport(Transport):
    """A synchronous transport layer for the API client.

    the url, cookie_file, username, and password parameters are
    mandatory when using this resource. The file provided in
    cookie_file is used to store and retrieive the authentication
    cookies for the API.

    The optional agent parameter can be used to specify a custom
    User-Agent string for the API. If not provided, the default
    RBTools User-Agent will be used.

    The optional session can be used to specify an 'rbsessionid'
    to use when authenticating with reviewboard.

    """
    def __init__(self, url, cookie_file, username=None, password=None,
                 agent=None, session=None, disable_proxy=False,
                 auth_callback=None, *args, **kwargs):
        super(SyncTransport, self).__init__(url, *args, **kwargs)
        self.server = ReviewBoardServer(self.url, cookie_file,
                                        username=username,
                                        password=password,
                                        session=session,
                                        disable_proxy=disable_proxy,
                                        auth_callback=auth_callback)

        self.get_root = SyncTransportMethod(self, self._root_request)

    def _root_request(self):
        return HttpRequest(self.server.url)

    def login(self, username, password):
        self.server.login(username, password)

    def wrap(self, value):
        """Wrap any values returned to the user

        All values returned from the transport should be wrapped with
        this method, unless the specific type is known and handled as
        a special case. This wrapping allows for nested dictionaries
        and fields to be accessed as attributes, instead of using the
        '[]' operation.

        This wrapping is also necessary to have control over updates to
        nested fields inside the resource.
        """
        if isinstance(value, ResourceItem):
            return SyncTransportItemResource(self, value)
        elif isinstance(value, ResourceList):
            return SyncTransportListResource(self, value)
        elif isinstance(value, list):
            return ResourceListField(self, value)
        elif isinstance(value, dict):
            dict_keys = set(value.keys())
            if ('href' in dict_keys and
                len(dict_keys.difference(LINK_KEYS)) == 0):
                return SyncTransportResourceLink(self, **value)
            else:
                return ResourceDictField(self, value)
        else:
            return value

    def __repr__(self):
        return ('<SyncTransport(url=%r, cookie_file=%r, username=%r, '
                'password=%r, agent=%r)>') % (
            self.url,
            self.server.cookie_file,
            self.server.preset_auth_handler.password_mgr.rb_user,
            # Actual password not shown for security reasons.
            '*********',
            self.server.agent)
Exemplo n.º 3
0
class SyncTransport(Transport):
    """A synchronous transport layer for the API client.

    The file provided in cookie_file is used to store and retrieve
    the authentication cookies for the API.

    The optional agent parameter can be used to specify a custom
    User-Agent string for the API. If not provided, the default
    RBTools User-Agent will be used.

    The optional session can be used to specify an 'rbsessionid'
    to use when authenticating with reviewboard.
    """
    def __init__(self,
                 url,
                 cookie_file=None,
                 username=None,
                 password=None,
                 agent=None,
                 session=None,
                 disable_proxy=False,
                 auth_callback=None,
                 otp_token_callback=None,
                 *args,
                 **kwargs):
        super(SyncTransport, self).__init__(url, *args, **kwargs)
        self.server = ReviewBoardServer(self.url,
                                        cookie_file=cookie_file,
                                        username=username,
                                        password=password,
                                        session=session,
                                        disable_proxy=disable_proxy,
                                        auth_callback=auth_callback,
                                        otp_token_callback=otp_token_callback)

    def get_root(self):
        return self._execute_request(HttpRequest(self.server.url))

    def get_path(self, path, *args, **kwargs):
        if not path.endswith('/'):
            path = path + '/'

        if path.startswith('/'):
            path = path[1:]

        return self._execute_request(
            HttpRequest(self.server.url + path, query_args=kwargs))

    def get_url(self, url, *args, **kwargs):
        if not url.endswith('/'):
            url = url + '/'

        return self._execute_request(HttpRequest(url, query_args=kwargs))

    def login(self, username, password):
        self.server.login(username, password)

    def execute_request_method(self, method, *args, **kwargs):
        request = method(*args, **kwargs)

        if isinstance(request, HttpRequest):
            return self._execute_request(request)

        return request

    def _execute_request(self, request):
        """Execute an HTTPRequest and construct a resource from the payload"""
        logging.debug('Making HTTP %s request to %s' %
                      (request.method, request.url))

        rsp = self.server.make_request(request)
        info = rsp.info()
        mime_type = info['Content-Type']
        item_content_type = info.get('Item-Content-Type', None)
        payload = rsp.read()
        payload = decode_response(payload, mime_type)

        return create_resource(self,
                               payload,
                               request.url,
                               mime_type=mime_type,
                               item_mime_type=item_content_type)

    def __repr__(self):
        return '<%s(url=%r, cookie_file=%r, agent=%r)>' % (
            self.__class__.__name__, self.url, self.server.cookie_file,
            self.server.agent)
Exemplo n.º 4
0
Arquivo: sync.py Projeto: drbr/rbtools
class SyncTransport(Transport):
    """A synchronous transport layer for the API client.

    The file provided in cookie_file is used to store and retrieve
    the authentication cookies for the API.

    The optional agent parameter can be used to specify a custom
    User-Agent string for the API. If not provided, the default
    RBTools User-Agent will be used.

    The optional session can be used to specify an 'rbsessionid'
    to use when authenticating with reviewboard.
    """
    def __init__(self, url, cookie_file=None, username=None, password=None,
                 api_token=None, agent=None, session=None, disable_proxy=False,
                 auth_callback=None, otp_token_callback=None,
                 disable_ssl_verification=False, allow_caching=True,
                 cache_location=None, in_memory_cache=False,
                 save_cookies=True, *args, **kwargs):
        super(SyncTransport, self).__init__(url, *args, **kwargs)
        self.allow_caching = allow_caching
        self.cache_location = cache_location
        self.in_memory_cache = in_memory_cache
        self.server = ReviewBoardServer(
            self.url,
            cookie_file=cookie_file,
            username=username,
            password=password,
            api_token=api_token,
            session=session,
            disable_proxy=disable_proxy,
            auth_callback=auth_callback,
            otp_token_callback=otp_token_callback,
            disable_ssl_verification=disable_ssl_verification,
            save_cookies=save_cookies)

    def get_root(self):
        return self._execute_request(HttpRequest(self.server.url))

    def get_path(self, path, *args, **kwargs):
        if not path.endswith('/'):
            path = path + '/'

        if path.startswith('/'):
            path = path[1:]

        return self._execute_request(
            HttpRequest(self.server.url + path, query_args=kwargs))

    def get_url(self, url, *args, **kwargs):
        if not url.endswith('/'):
            url = url + '/'

        return self._execute_request(HttpRequest(url, query_args=kwargs))

    def login(self, username, password):
        self.server.login(username, password)

    def logout(self):
        self.server.logout()

    def execute_request_method(self, method, *args, **kwargs):
        request = method(*args, **kwargs)

        if isinstance(request, HttpRequest):
            return self._execute_request(request)

        return request

    def _execute_request(self, request):
        """Execute an HTTPRequest and construct a resource from the payload"""
        logging.debug('Making HTTP %s request to %s' % (request.method,
                                                        request.url))

        rsp = self.server.make_request(request)
        info = rsp.info()
        mime_type = info['Content-Type']
        item_content_type = info.get('Item-Content-Type', None)

        if request.method == 'DELETE':
            # DELETE calls don't return any data. Everything else should.
            return None
        else:
            payload = rsp.read()
            payload = decode_response(payload, mime_type)

            return create_resource(self, payload, request.url,
                                   mime_type=mime_type,
                                   item_mime_type=item_content_type)

    def enable_cache(self):
        """Enable caching for all future HTTP requests.

        The cache will be created at the default location if none is provided.

        If the in_memory parameter is True, the cache will be created in memory
        instead of on disk. This overrides the cache_location parameter.
        """
        if self.allow_caching:
            self.server.enable_cache(cache_location=self.cache_location,
                                     in_memory=self.in_memory_cache)

    def __repr__(self):
        return '<%s(url=%r, cookie_file=%r, agent=%r)>' % (
            self.__class__.__name__,
            self.url,
            self.server.cookie_file,
            self.server.agent)
Exemplo n.º 5
0
class SyncTransport(Transport):
    """A synchronous transport layer for the API client.

    The file provided in cookie_file is used to store and retrieve
    the authentication cookies for the API.

    The optional agent parameter can be used to specify a custom
    User-Agent string for the API. If not provided, the default
    RBTools User-Agent will be used.

    The optional session can be used to specify an 'rbsessionid'
    to use when authenticating with reviewboard.
    """
    def __init__(self, url, cookie_file=None, username=None, password=None,
                 agent=None, session=None, disable_proxy=False,
                 auth_callback=None, *args, **kwargs):
        super(SyncTransport, self).__init__(url, *args, **kwargs)
        self.server = ReviewBoardServer(self.url,
                                        cookie_file=cookie_file,
                                        username=username,
                                        password=password,
                                        session=session,
                                        disable_proxy=disable_proxy,
                                        auth_callback=auth_callback)

    def get_root(self):
        return self._execute_request(HttpRequest(self.server.url))

    def get_path(self, path, *args, **kwargs):
        if path[-1] != '/':
            path = path + '/'

        if path[0] == '/':
            path = path[1:]

        return self._execute_request(
            HttpRequest(self.server.url + path, query_args=kwargs))

    def login(self, username, password):
        self.server.login(username, password)

    def execute_request_method(self, method, *args, **kwargs):
        request = method(*args, **kwargs)

        if isinstance(request, HttpRequest):
            return self._execute_request(request)

        return request

    def _execute_request(self, request):
        """Execute an HTTPRequest and construct a resource from the payload"""
        logging.debug('Making HTTP %s request to %s' % (request.method,
                                                        request.url))

        rsp = self.server.make_request(request)
        info = rsp.info()
        mime_type = info['Content-Type']
        item_content_type = info.get('Item-Content-Type', None)
        payload = rsp.read()
        payload = decode_response(payload, mime_type)

        return create_resource(self, payload, request.url, mime_type=mime_type,
                               item_mime_type=item_content_type)

    def __repr__(self):
        return '<%s(url=%r, cookie_file=%r, agent=%r)>' % (
            self.__class__.__name__,
            self.url,
            self.server.cookie_file,
            self.server.agent)