コード例 #1
0
ファイル: client.py プロジェクト: prnake/Thu-Toolbox
    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
コード例 #2
0
    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
コード例 #3
0
ファイル: client.py プロジェクト: haiwen/python-seafile
 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:
         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
コード例 #4
0
ファイル: client.py プロジェクト: Willyfrog/python-seafile
 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:
         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
コード例 #5
0
 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
コード例 #6
0
ファイル: client.py プロジェクト: haiwen/python-seafile
    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)
            raise ClientHttpError(resp.status_code, msg)

        return resp
コード例 #7
0
ファイル: client.py プロジェクト: Willyfrog/python-seafile
    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)
            raise ClientHttpError(resp.status_code, msg)

        return resp
コード例 #8
0
ファイル: client.py プロジェクト: ldreissi/python-seafile-api
    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
コード例 #9
0
ファイル: client.py プロジェクト: vincentrou/python-seafile
 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)
コード例 #10
0
    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
コード例 #11
0
ファイル: account.py プロジェクト: axonxorz/python-seafile
 def client_login_url(self):
     token = self.client_login_token()
     if token:
         return urljoin(self.client.server, '/client-login/?token={}'.format(token))
     else:
         return None