def _send_request(self, method, url, *args, **kwargs): if not url.startswith('http'): url = urljoin(self.server, url) headers = kwargs.get('headers', {}) headers.setdefault('Authorization', 'Token ' + self._token) kwargs['headers'] = headers expected = kwargs.pop('expected', 200) if not hasattr(expected, '__iter__'): expected = (expected, ) kwargs['auth'] = self._rewrite_request( *args, **kwargs) # hack to rewrite post body resp = requests.request(method, url, *args, verify=self.verify_ssl, **kwargs) if resp.status_code not in expected: msg = 'Expected %s, but get %s' % \ (' or '.join(map(str, expected)), resp.status_code) raise ClientHttpError(resp.status_code, msg) return resp
def _send_request(self, method, url, *args, **kwargs): if not url.startswith('http'): url = urljoin(self.server, url) headers = kwargs.get('headers', {}) headers.setdefault('Authorization', 'Token ' + self._token) kwargs['headers'] = headers expected = kwargs.pop('expected', 200) if not hasattr(expected, '__iter__'): expected = (expected, ) if 'files' in kwargs and getsize(kwargs['files']['file'][1].name) > MAX_SIZE: # see https://github.com/psf/requests/issues/2717#issuecomment-724725392 m = MultipartEncoder( fields={'file': (kwargs['files']['file'][1].name, open(kwargs['files']['file'][1].name, 'rb'), 'text/plain'), 'parent_dir': kwargs['files']['parent_dir']} ) del kwargs['files'] kwargs['data'] = m kwargs['headers']['Content-Type'] = m.content_type resp = requests.request(method, url, *args, **kwargs) if resp.status_code not in expected: msg = 'Expected %s, but get %s' % \ (' or '.join(map(str, expected)), resp.status_code) raise ClientHttpError(resp.status_code, msg) return resp
def _get_token(self): data = { 'username': self.username, 'password': self.password, } url = urljoin(self.server, '/api2/auth-token/') res = requests.post(url, data=data) if res.status_code != 200: if res.status_code == 400: # Possible auth error try: resp_json = res.json() if 'non_field_errors' in resp_json: raise AuthenticationError(res.status_code, res.content) except (TypeError, ValueError), e: # fallback raise ClientHttpError(res.status_code, res.content) else: raise ClientHttpError(res.status_code, res.content)
def _get_token(self): data = { 'username': self.username, 'password': self.password, } url = urljoin(self.server, '/api2/auth-token/') res = requests.post(url, data=data, verify=self.verify_ssl) if res.status_code != 200: raise ClientHttpError(res.status_code, res.content) token = res.json()['token'] assert len( token) == 40, 'The length of seahub api auth token should be 40' self._token = token
def _send_request(self, method, url, *args, **kwargs): if not (url.startswith('http') or url.startswith('https')): url = urljoin(self.server, url) headers = kwargs.get('headers', {}) headers.setdefault('Authorization', 'Token {}'.format(self._token)) kwargs['headers'] = headers expected = kwargs.pop('expected', 200) if not hasattr(expected, '__iter__'): expected = (expected, ) resp = requests.request(method, url, **kwargs) if resp.status_code not in expected: msg = 'Expected {}, but get {}'.format( ' or '.join(map(str, expected)), resp.status_code) raise ClientHttpError(resp.status_code, msg) return resp
def _send_request(self, method, url, *args, **kwargs): if not url.startswith('http'): url = urljoin(self.server, url) headers = kwargs.get('headers', {}) headers.setdefault('Authorization', 'Token ' + self._token) kwargs['headers'] = headers expected = kwargs.pop('expected', 200) if not hasattr(expected, '__iter__'): expected = (expected, ) resp = requests.request(method, url, *args, **kwargs) if resp.status_code not in expected: msg = 'Expected %s, but get %s' % \ (' or '.join(map(str, expected)), resp.status_code) msg += '\n' + resp.content.decode('utf-8') raise ClientHttpError(resp.status_code, msg) return resp