コード例 #1
0
    def upload(self, url_or_file, **options):
        '''
        Upload and convert a file referenced by URL or uploaded via a POST
        request.
        '''
        options['token'] = self.API_TOKEN

        #Check if we have a url or file
        if multipart.is_file(url_or_file):
            options['file'] = url_or_file
            return self.conn.post(
                    urlparse.urljoin(self.API_URL, 'document/upload'), 
                    body = options)

        #Otherwise, we just have a url
        options['url'] = url_or_file
        return self.conn.get(
                urlparse.urljoin(self.API_URL, 'document/upload'), 
                body = options
                )
コード例 #2
0
ファイル: test_multipart.py プロジェクト: jucacrispim/bolacha
def test_is_file_attr_read_not_callable():
    class FakeFile(object):
        read = 'not a callable'

    assert not multipart.is_file(FakeFile())
コード例 #3
0
ファイル: test_multipart.py プロジェクト: jucacrispim/bolacha
def test_is_file_no_attr_read():
    class FakeFile(object):
        pass

    assert not multipart.is_file(FakeFile())
コード例 #4
0
ファイル: test_multipart.py プロジェクト: jucacrispim/bolacha
def test_is_file_true():
    class FakeFile(object):
        def read(self):
            pass

    assert multipart.is_file(FakeFile())
コード例 #5
0
ファイル: __init__.py プロジェクト: jucacrispim/bolacha
    def request(self, url, method, body=None, headers=None):
        if not isinstance(url, basestring):
            raise TypeError, 'Bolacha.request, parameter url must be ' \
                  'a string. Got %s' % repr(url)

        if not isinstance(method, basestring):
            raise TypeError, 'Bolacha.request, parameter method must be ' \
                  'a string. Got %s' % repr(method)

        if method not in HTTP_METHODS:
            raise TypeError, 'Bolacha.request, parameter method must be ' \
                  'a valid HTTP method. Got %s. %s' % (method,
                                                        RFC_LOCATION)

        if body is None:
            body = ''

        if not isinstance(body, (basestring, dict)):
            raise TypeError, 'Bolacha.request, parameter body must be ' \
                  'a string or dict. Got %s.' % (repr(body))

        is_urlencoded = False

        body_has_file = isinstance(body, dict) and any([is_file(fobj)
                                                        for fobj in body.values()])

        if isinstance(body, dict):
            if body_has_file:
                rbody = encode_multipart(BOUNDARY, body)
            else:
                rbody = urlencode(body)
                is_urlencoded = True
        else:
            rbody = body

        if headers is None:
            headers = {}

        if not isinstance(headers, dict):
            raise TypeError, 'Bolacha.request, parameter headers must be ' \
                  'a dict or NoneType. Got %s' % repr(headers)

        rheaders = self.headers.copy()
        rheaders.update(headers)

        if self.persistent:
            if 'set-cookie' in self.headers:
                rheaders['Cookie'] = self.headers['set-cookie']

        if 'set-cookie' in rheaders:
            del rheaders['set-cookie']

        if is_urlencoded and not 'Content-type' in rheaders:
            rheaders['Content-type'] = 'application/x-www-form-urlencoded'
        elif body_has_file:
            rheaders['Content-type'] = 'multipart/form-data; boundary=%s' % BOUNDARY
            rheaders['content-length'] = '%d' % len(rbody)

        response, content = self.http.request(url, method, rbody, rheaders)

        if self.persistent and 'set-cookie' in response:
            self.headers['set-cookie'] = response['set-cookie']

        if not self.persistent:
            if 'connection' in response:
                self.headers['connection'] = response['connection']

        return response, content
コード例 #6
0
ファイル: __init__.py プロジェクト: mikexstudios/bolacha
    def request(self, url, method, body=None, headers=None):
        if not isinstance(url, basestring):
            raise TypeError, 'Bolacha.request, parameter url must be ' \
                  'a string. Got %s' % repr(url)

        if not isinstance(method, basestring):
            raise TypeError, 'Bolacha.request, parameter method must be ' \
                  'a string. Got %s' % repr(method)

        if method not in HTTP_METHODS:
            raise TypeError, 'Bolacha.request, parameter method must be ' \
                  'a valid HTTP method. Got %s. %s' % (method,
                                                        RFC_LOCATION)

        if body is None:
            body = ''

        if not isinstance(body, (basestring, dict)):
            raise TypeError, 'Bolacha.request, parameter body must be ' \
                  'a string or dict. Got %s.' % (repr(body))

        is_urlencoded = False

        body_has_file = isinstance(body, dict) and any([is_file(value)
                                                       for key,value
                                                       in expand_items(body)])

        #We can only send files with POST or PUT
        if body_has_file and method not in ('POST', 'PUT'):
            raise Exception('Bolacha.request, can only send files with POST or PUT!')

        if isinstance(body, dict):
            if body_has_file:
                rbody = encode_multipart(BOUNDARY, body)
            else:
                rbody = urlencode(body, doseq=True)
                is_urlencoded = True
        else:
            rbody = body

        if headers is None:
            headers = {}

        if not isinstance(headers, dict):
            raise TypeError, 'Bolacha.request, parameter headers must be ' \
                  'a dict or NoneType. Got %s' % repr(headers)

        rheaders = self.headers.copy()
        rheaders.update(headers)

        if self.persistent:
            if 'set-cookie' in self.headers:
                rheaders['Cookie'] = self.headers['set-cookie']

        if 'set-cookie' in rheaders:
            del rheaders['set-cookie']

        if is_urlencoded and not 'Content-type' in rheaders:
            rheaders['Content-type'] = 'application/x-www-form-urlencoded'
        elif body_has_file:
            rheaders['Content-type'] = 'multipart/form-data; boundary=%s' % BOUNDARY
            rheaders['content-length'] = '%d' % len(rbody)

        #For httplib2, if the request is either GET or HEAD, need to append
        #body to url.
        if method in ('GET', 'HEAD'):
            url += '?%s' % rbody
            rbody = ''

        response, content = self.http.request(url, method, rbody, rheaders)

        if self.persistent and 'set-cookie' in response:
            self.headers['set-cookie'] = response['set-cookie']

        if not self.persistent:
            if 'connection' in response:
                self.headers['connection'] = response['connection']

        return response, content