def send_request(self, method=None, parameters=None, files=None):
        if USE_SSL:
            protocol = 'https://'
        else:
            protocol = 'http://'

        url = '%s%s/%s' % (protocol, API_HOST, method)

        data = {
            'api_key': self.api_key,
            'api_secret': self.api_secret,
            'format': self.format
        }

        if parameters:
            data.update(parameters)

        # Local file is provided, use multi-part form
        if files:
            from multipart import Multipart
            form = Multipart()

            for key, value in data.iteritems():
                form.field(key, value)

            for i, file in enumerate(files, 1):
                if hasattr(file, 'read'):
                    if hasattr(file, 'name'):
                        name = os.path.basename(file.name)
                    else:
                        name = 'attachment_%d' % i
                    close_file = False
                else:
                    name = os.path.basename(file)
                    file = open(file, 'r')
                    close_file = True

                try:
                    form.file(name, name, file.read())
                finally:
                    if close_file:
                        file.close()

            (content_type, post_data) = form.get()
            headers = {'Content-Type': content_type}
        else:
            post_data = urllib.urlencode(data)
            headers = {}

        request = urllib2.Request(url, headers=headers, data=post_data)
        response = urllib2.urlopen(request)
        response = response.read()
        response_data = json.loads(response)

        if 'status' in response_data and \
            response_data['status'] == 'failure':
            raise FaceError(response_data['error_code'],
                            response_data['error_message'])

        return response_data
Beispiel #2
0
    def send_request(self, method=None, parameters=None, files=None):
        if USE_SSL:
            protocol = 'https://'
        else:
            protocol = 'http://'

        url = '%s%s/%s' % (protocol, API_HOST, method)

        data = {'api_key': self.api_key,
                'api_secret': self.api_secret,
                'format': self.format}

        if parameters:
            data.update(parameters)

        # Local file is provided, use multi-part form
        if files:
            from multipart import Multipart
            form = Multipart()

            for key, value in data.iteritems():
                form.field(key, value)

            for i, file in enumerate(files, 1):
                if hasattr(file, 'read'):
                    if hasattr(file, 'name'):
                        name = os.path.basename(file.name)
                    else:
                        name = 'attachment_%d' % i
                    close_file = False
                else:
                    name = os.path.basename(file)
                    file = open(file, 'r')
                    close_file = True

                try:
                    form.file(name, name, file.read())
                finally:
                    if close_file:
                        file.close()

            (content_type, post_data) = form.get()
            headers = {'Content-Type': content_type}
        else:
            post_data = urllib.urlencode(data)
            headers = {}

        request = urllib2.Request(url, headers=headers, data=post_data)
        response = urllib2.urlopen(request)
        response = response.read()
        response_data = json.loads(response)

        if 'status' in response_data and \
            response_data['status'] == 'failure':
            raise FaceError(response_data['error_code'],
                            response_data['error_message'])

        return response_data
Beispiel #3
0
    def send_request(self, method=None, parameters=None, files=None, buffers=None):
        protocol = "https://" if USE_SSL else "http://"
        url = "%s%s/%s.%s" % (protocol, API_HOST, method, self.format)
        data = {"api_key": self.api_key, "api_secret": self.api_secret}

        if parameters:
            data.update(parameters)

        # Local file is provided, use multi-part form
        if files or buffers:
            from multipart import Multipart

            form = Multipart()

            for key, value in data.iteritems():
                form.field(key, value)

            if files:
                for i, file in enumerate(files, 1):
                    if hasattr(file, "read"):
                        if hasattr(file, "name"):
                            name = os.path.basename(file.name)
                        else:
                            name = "attachment_%d" % i
                        close_file = False
                    else:
                        name = os.path.basename(file)
                        file = open(file, "rb")
                        close_file = True

                    try:
                        form.file(name, name, file.read())
                    finally:
                        if close_file:
                            file.close()
            else:
                for i, buffer in enumerate(buffers, 1):
                    name = "attachment_%d" % i
                    form.file(name, name, buffer)
            (content_type, post_data) = form.get()
            headers = {"Content-Type": content_type}
        else:
            post_data = urllib.urlencode(data)
            headers = {}

        request = urllib2.Request(url, headers=headers, data=post_data)
        try:
            response = urllib2.urlopen(request)
            response = response.read()
        except urllib2.HTTPError as e:
            response = e.read()
        response_data = json.loads(response)

        if "status" in response_data and response_data["status"] == "failure":
            raise FaceError(response_data["error_code"], response_data["error_message"])

        return response_data
Beispiel #4
0
    def uploadRun(self, email, password, files):
        '''
        '''
        def file2text(fn):
            fh = open(fn)
            fc = fh.read()
            fh.close()
            return fc

        upload_url = self.url + '/add_run.php'
        success = True
        backupdir = self.getConfigDataFromFile()[2]

        if not self.isLoggedIn:
            self.isLoggedIn = self.webLogin(email, password)

        if self.isLoggedIn:

            for full_path in files:
                self.logger.info("uploading file %s" % full_path)
                path, fname = os.path.split(full_path)

                if self.isNewFile(fname):

                    m = Multipart()
                    m.field('public_run', 'checked')
                    m.field('MAX_FILE_SIZE', '14000000')
                    m.file(
                        'runfile', fname, file2text(full_path),
                        {'Content-Type': 'application/x-www-form-urlencoded'})
                    ct, body = m.get()

                    request = urllib2.Request(upload_url,
                                              headers={'Content-Type': ct},
                                              data=body)
                    reply = ClientCookie.urlopen(request)
                    file_uploaded = 'Runometer: Show Run' in reply.read()

                    if file_uploaded:
                        self.logFileUploaded(fname)
                        shutil.copy(full_path, backupdir)

                    success = success * file_uploaded

                else:
                    self.logger.info(
                        "File %s already uploaded in some previous session" %
                        fname)

        return success
Beispiel #5
0
    def send_request(self, method=None, parameters=None):
        url = '%s/%s' % (API_URL, method)

        data = {
            'api_key': self.api_key,
            'api_secret': self.api_secret,
            'format': self.format
        }

        if parameters:
            data.update(parameters)
        # raise Exception(url, data)
        # Local file is provided, use multi-part form
        if 'file' in parameters:
            from multipart import Multipart
            form = Multipart()

            for key, value in data.iteritems():

                if key == 'file':
                    file = open(value, 'r')
                    # with open(value, 'r') as file:
                    form.file(os.path.basename(key), os.path.basename(key),
                              file.read())
                else:
                    form.field(key, value)

            (content_type, post_data) = form.get()
            headers = {'Content-Type': content_type}
        else:
            post_data = urllib.urlencode(data)
            headers = {}

        request = urllib2.Request(url, headers=headers, data=post_data)
        response = urllib2.urlopen(request)
        response = response.read()
        response_data = json.loads(response)

        if 'status' in response_data and \
         response_data['status'] == 'failure':
            raise FaceError(response_data['error_code'],
                            response_data['error_message'])

        return response_data
    def send_request(self, method=None, parameters=None):
        url = '%s/%s' % (API_URL, method)

        data = {'api_key': self.api_key,
                'api_secret': self.api_secret,
                'format': self.format}

        post_data = urllib.urlencode(data)
        headers = {}
        
        if parameters:
            data.update(parameters)

            # Local file is provided, use multi-part form
            if 'file' in parameters:
                from multipart import Multipart
                form = Multipart()
                
                for key, value in data.iteritems():
    
                    if key == 'file':
                        with open(value, 'r') as file:
                            form.file(os.path.basename(key), os.path.basename(key),
                                      file.read())
                    else:
                        form.field(key, value)
    
                (content_type, post_data) = form.get()
                headers = {'Content-Type': content_type}
                
            else:
                post_data = urllib.urlencode(data)

        request = urllib2.Request(url, headers=headers, data=post_data)
        response = urllib2.urlopen(request)
        response = response.read()
        response_data = json.loads(response)

        if 'status' in response_data and \
            response_data['status'] == 'failure':
            raise FaceError(response_data['error_code'],
                            response_data['error_message'])

        return response_data
Beispiel #7
0
    def uploadRun(self, email, password, files):
        '''
        '''
        def file2text(fn):
            fh=open(fn)
            fc = fh.read()
            fh.close()
            return fc
            
        upload_url = self.url + '/add_run.php'
        success = True
        backupdir = self.getConfigDataFromFile()[2]

        if not self.isLoggedIn: self.isLoggedIn = self.webLogin(email, password)
                
        if self.isLoggedIn:
            
            for full_path in files:
                self.logger.info("uploading file %s" % full_path)
                path, fname = os.path.split(full_path)

                if self.isNewFile(fname):

                    m = Multipart()
                    m.field('public_run','checked')
                    m.field('MAX_FILE_SIZE','14000000')
                    m.file('runfile',fname ,file2text(full_path),{'Content-Type':'application/x-www-form-urlencoded'})
                    ct,body = m.get()
                   
                    request = urllib2.Request(upload_url, headers={'Content-Type':ct}, data=body)
                    reply = ClientCookie.urlopen(request)
                    file_uploaded = 'Runometer: Show Run' in reply.read()
                    
                    if file_uploaded:
                        self.logFileUploaded(fname)
                        shutil.copy(full_path, backupdir)

                    success = success * file_uploaded
                    
                else:
                    self.logger.info("File %s already uploaded in some previous session" % fname)

        return success