Esempio n. 1
0
    def upload(self, args={}, url=None, file_path=None, verbose=False):
        """Execute an API upload call, returning a Python structure"""

        if url:
            url = str(url)
        else:
            url = self._url

        if ('api_format' not in args):
            # Use serialised Python format for the API output,
            # otherwise use format specified in the args.
            args['api_format'] = 'py'

        query = urllib.urlencode(args)

        if request.__name__ == 'pycurl':
            curl = request.Curl()

            if verbose:
                # Enable verbose output
                curl.setopt(curl.VERBOSE, 1)

            curl.setopt(request.URL, url + '?' + query)

            post = [('file', (request.FORM_FILE, str(file_path)))]
            curl.setopt(request.HTTPPOST, post)

            if verbose:
                # Show upload progress
                curl.setopt(request.NOPROGRESS, 0)
                curl.setopt(request.PROGRESSFUNCTION, self._progress)

            # Have the response written back to a string
            output = StringIO.StringIO()
            curl.setopt(request.WRITEFUNCTION, output.write)

            curl.perform()
            curl.close()

            response = output.getvalue()
        elif request.__name__ == 'urllib2':
            try:
                response = request.urlopen(url, urllib.urlencode(args)).read()
            except request.URLError, e:
                try:
                    error_code = e.code
                    response = e.read()
                except AttributeError:
                    response = e
Esempio n. 2
0
    def call(self, api_call, args=None, url=None, verbose=False):
        """Execute an API call, returning a Python structure"""

        if url:
            url = url + api_call
        else:
            url = self._url + api_call

        args = self._args(args or {})
        query = urllib.urlencode(args)

        if request.__name__ == 'pycurl':
            curl = request.Curl()

            if verbose:
                # Enable verbose output
                curl.setopt(curl.VERBOSE, 1)

            curl.setopt(request.URL, url + '?' + query)
            curl.setopt(request.HTTPGET, 1)

            # Write response to a string
            output = StringIO.StringIO()
            curl.setopt(request.WRITEFUNCTION, output.write)

            curl.perform()
            curl.close()

            response = output.getvalue()
        elif request.__name__ == 'urllib2':
            try:
                response = request.urlopen(url, query).read()
            except request.URLError, e:
                try:
                    error_code = e.code
                    response = e.read()
                except AttributeError:
                    response = e