class BaseClient(object): """ This is the base client, implements basic rest methods """ def __init__(self): """ Create the api client """ # init config try: self.username = settings.get("credentials", "username") self.password = settings.get("credentials", "password") except Exception, e: log.error("Error getting api credentials from settings file") log.error(e) log.error(traceback.format_exc()) raise e self._reset_headers() try: self.base_path = settings.get("base", "paths.api") except Exception, e: log.error("Error getting api path from settings file") log.error(e) log.error(traceback.format_exc()) raise e
def path(self, args, url_params=None): """ Build path for api call @params url path params """ try: path_array = copy(args) path_array.reverse() path_array.append(self.base_path) path_array.reverse() path = '/'.join(path_array) # add request parameters if url_params is not None: path = path + '?' for key, value in url_params.iteritems(): path = path + '&' + key + '=' + urllib.quote(value, safe='') return path except Exception, e: log.error("Error building path with params:") log.error(args) log.error(e) log.error(traceback.format_exc()) raise
def _reset_headers(self, headers={}, accept=None): self.headers = headers import base64 if accept is None: accept = settings.get('base', settings.get('base', 'accept.default')) else: accept = settings.get('base', 'accept.%s' % accept) if self.username != "" and self.password != "": auth_string = base64.encodestring('%s:%s' % (self.username, self.password))[:-1] self.headers.update({'authorization': 'basic %s' % auth_string, 'accept': accept, }) else: log.error('The username and/or password are empty.') exit()
def _reset_headers(self, headers={}, accept=None): self.headers = headers import base64 if accept is None: accept = settings.get('base', settings.get('base', 'accept.default')) else: accept = settings.get('base', 'accept.%s' % accept) if self.username != "" and self.password != "": auth_string = base64.encodestring( '%s:%s' % (self.username, self.password))[:-1] self.headers.update({ 'authorization': 'basic %s' % auth_string, 'accept': accept, }) else: log.error('The username and/or password are empty.') exit()
def __init__(self): """ Create the api client """ # init config try: self.username = settings.get("credentials", "username") self.password = settings.get("credentials", "password") except Exception, e: log.error("Error getting api credentials from settings file") log.error(e) log.error(traceback.format_exc()) raise e
def upload_transcribe(media_filename): """ Upload a media file to Koemei for transcription """ try: log.info("Upload and transcribe file %s ..." % media_filename) media_item = Media.create(client=client, media_filename=media_filename) log.info("... OK - media uuid: %s" % media_item.uuid) except Exception, e: log.error("... Error creating media %s ..." % media_filename) log.error(e) log.error(traceback.format_exc()) raise e
def __init__(self): """ Create the api client """ # init config try: self.username = settings.get("credentials", "username") self.password = settings.get("credentials", "password") except Exception,e: log.error("Error getting api credentials from settings file") log.error(e) log.error(traceback.format_exc()) raise e
def upload_align(media_filename, aligndata): """ Upload a media file to Koemei for alignment NOTE : you will need your account to be specially setup to use this feature """ try: log.info("Upload and align media %s ..." % media_filename) media_item = Media.create(client=client, media_filename=media_filename, aligndata=aligndata, transcribe=False) log.info("... OK - media uuid: %s" % media_item.uuid) except Exception, e: log.error("... Error aligning media %s ..." % media_filename) log.error(e) log.error(traceback.format_exc()) raise e
def upload_align(media_filename, aligndata): """ Upload a media file to Koemei for alignment NOTE : you will need your account to be specially setup to use this feature """ register_openers() try: log.info("Upload and align media %s ..." % media_filename) media_item = Media.create( client=client, media_filename=media_filename, aligndata=aligndata, transcribe=False ) log.info("... OK - media uuid: %s" % media_item.uuid) except Exception, e: log.error("... Error aligning media %s ..." % media_filename) log.error(e) log.error(traceback.format_exc()) raise e