Example #1
0
 def __repr__(self):
     endpoint = self._get_endpoint(
         'collection',
         self._bucket_name,
         self._collection_name
     )
     absolute_endpoint = utils.urljoin(self.session.server_url, endpoint)
     return "<KintoClient %s>" % absolute_endpoint
Example #2
0
    def request(self,
                method,
                endpoint,
                data=None,
                permissions=None,
                payload=None,
                **kwargs):
        parsed = urlparse(endpoint)
        if not parsed.scheme:
            actual_url = utils.urljoin(self.server_url, endpoint)
        else:
            actual_url = endpoint

        if self.auth is not None:
            kwargs.setdefault('auth', self.auth)

        payload = payload or {}
        # if data is not None:
        payload['data'] = data or {}
        if permissions is not None:
            if hasattr(permissions, 'as_dict'):
                permissions = permissions.as_dict()
            payload['permissions'] = permissions
        if payload and method not in ('get', 'head'):
            payload_kwarg = 'data' if 'files' in kwargs else 'json'
            kwargs.setdefault(payload_kwarg, payload)

        retry = self.nb_retry
        while retry >= 0:
            resp = requests.request(method, actual_url, **kwargs)
            retry = retry - 1
            if not (200 <= resp.status_code < 400):
                if resp.status_code >= 500 and retry >= 0:
                    # Wait and try again.
                    # If not forced, use retry-after header and wait.
                    if self.retry_after is None:
                        retry_after = resp.headers.get("Retry-After", 0)
                    else:
                        retry_after = self.retry_after
                    time.sleep(retry_after)
                    continue

                # Retries exhausted, raise expection.
                message = '{0} - {1}'.format(resp.status_code, resp.json())
                exception = KintoException(message)
                exception.request = resp.request
                exception.response = resp
                raise exception

        if resp.status_code == 304:
            body = None
        else:
            body = resp.json()
        # XXX Add the status code.
        return body, resp.headers
Example #3
0
    def request(self, method, endpoint, data=None, permissions=None,
                payload=None, **kwargs):
        parsed = urlparse(endpoint)
        if not parsed.scheme:
            actual_url = utils.urljoin(self.server_url, endpoint)
        else:
            actual_url = endpoint

        if self.auth is not None:
            kwargs.setdefault('auth', self.auth)

        payload = payload or {}
        # if data is not None:
        payload['data'] = data or {}
        if permissions is not None:
            if hasattr(permissions, 'as_dict'):
                permissions = permissions.as_dict()
            payload['permissions'] = permissions
        if payload and method not in ('get', 'head'):
            payload_kwarg = 'data' if 'files' in kwargs else 'json'
            kwargs.setdefault(payload_kwarg, payload)

        retry = self.nb_retry
        while retry >= 0:
            resp = requests.request(method, actual_url, **kwargs)
            retry = retry - 1
            if not (200 <= resp.status_code < 400):
                if resp.status_code >= 500 and retry >= 0:
                    # Wait and try again.
                    # If not forced, use retry-after header and wait.
                    if self.retry_after is None:
                        retry_after = resp.headers.get("Retry-After", 0)
                    else:
                        retry_after = self.retry_after
                    time.sleep(retry_after)
                    continue

                # Retries exhausted, raise expection.
                message = '{0} - {1}'.format(resp.status_code, resp.json())
                exception = KintoException(message)
                exception.request = resp.request
                exception.response = resp
                raise exception

        if resp.status_code == 304:
            body = None
        else:
            body = resp.json()
        # XXX Add the status code.
        return body, resp.headers
Example #4
0
 def __repr__(self):
     endpoint = self._get_endpoint('collection', self._bucket_name,
                                   self._collection_name)
     absolute_endpoint = utils.urljoin(self.session.server_url, endpoint)
     return "<KintoClient %s>" % absolute_endpoint
Example #5
0
 def test_urljoin_can_join_without_trailing_nor_prepend_slash(self):
     url = utils.urljoin("http://localhost", "v1")
     self.assertEquals(url, "http://localhost/v1")
Example #6
0
 def test_urljoin_can_join_prefixed_server_url(self):
     url = utils.urljoin("http://localhost/v1/", "/tests")
     self.assertEquals(url, "http://localhost/v1/tests")
Example #7
0
 def test_urljoin_can_join_with_both_trailing_and_prepend_slash(self):
     url = utils.urljoin("http://localhost/", "/v1")
     self.assertEquals(url, "http://localhost/v1")