def store_api_key(account_id=None, account_api_key=None): """ Stores API key :param account_id: User account ID :param account_api_key: API Key :return: """ if account_id is None: raise AttributeError("Provide account_id as parameter") if account_api_key is None: raise AttributeError("Provide account_api_key as parameter") try: connection = get_sqlite_connection() except Exception as exp: exp = append_description_to_exception( exp=exp, description='Could not get connection SQL database.') logger.error('Could not get connection SQL database: ' + repr(exp)) raise try: cursor, connection = get_sqlite_cursor(connection=connection) except Exception as exp: exp = append_description_to_exception( exp=exp, description='Could not get cursor for database connection') logger.error('Could not get cursor for database connection: ' + repr(exp)) raise try: cursor = store_api_key_to_db(account_id=account_id, account_api_key=account_api_key, cursor=cursor) except Exception as exp: exp = append_description_to_exception( exp=exp, description='Could not store API Key to database') logger.error('Could not store API Key to database: ' + repr(exp)) connection.rollback() raise else: connection.commit() connection.close() logger.debug('API Key and account_id stored')
def get_account_api_key(account_id=None): """ Get API Key by account ID :param account_id: :return: API Key """ logger.info("Get Account APIKey by Account ID") if account_id is None: raise AttributeError("Provide account_id as parameter") try: logger.info("Getting DB connection") connection = get_sqlite_connection() except Exception as exp: exp = append_description_to_exception(exp=exp, description='Could not get connection SQL database.') logger.error('Could not get connection SQL database: ' + repr(exp)) raise else: logger.info("Got DB connection") try: logger.info("Getting DB cursor") cursor, connection = get_sqlite_cursor(connection=connection) except Exception as exp: exp = append_description_to_exception(exp=exp, description='Could not get cursor for database connection') logger.error('Could not get cursor for database connection: ' + repr(exp)) raise else: logger.info("Got DB cursor") try: cursor, api_key = get_api_key(account_id=account_id, cursor=cursor) except Exception as exp: exp = append_description_to_exception(exp=exp, description='Could not find API key from database') logger.error('Could not get API key from database: ' + repr(exp)) connection.rollback() connection.close() raise else: connection.close() logger.debug('API key fetched') return api_key
def get_account_id_by_api_key(api_key=None): """ Get User account ID by Api Key :param api_key: :return: User account ID """ if api_key is None: raise AttributeError("Provide api_key as parameter") try: connection = get_sqlite_connection() except Exception as exp: exp = append_description_to_exception( exp=exp, description='Could not get connection SQL database.') logger.error('Could not get connection SQL database: ' + repr(exp)) raise try: cursor, connection = get_sqlite_cursor(connection=connection) except Exception as exp: exp = append_description_to_exception( exp=exp, description='Could not get cursor for database connection') logger.error('Could not get cursor for database connection: ' + repr(exp)) raise try: cursor, account_id = get_account_id(api_key=api_key, cursor=cursor) except Exception as exp: exp = append_description_to_exception( exp=exp, description='Could not Account ID from database') logger.error('Could not get Account ID from database: ' + repr(exp)) connection.rollback() connection.close() raise else: connection.close() logger.debug('Account ID fetched') return account_id