class Consult(object): def __init__(self, x_api_key=X_API_KEY): self._url_api = URL_API self._x_api_id = X_API_ID self._x_api_key = x_api_key self.audit = Audit() def get_information(self): try: headers = { "Content-Type": "application/json", "x-api-id": self._x_api_id, "x-api-key": self._x_api_key } response = requests.get(self._url_api, headers=headers).json() if self.response_validation(response): return response else: self.audit.insert_audit_log( 'Consulta retornou resposta inesperada: {} - {}'.format( response['status']['code'], response['status']['message'])) return None except Exception as e: self.audit.insert_audit_log('Erro: {}'.format(e)) @staticmethod def response_validation(response): if response['status']['code'] == 200: return True else: return False
class Main: def __init__(self): self.audit = Audit() self.audit.set_info(X_API_KEY, 'Arquivei-Consulta') self.db = DbController() self.api_consult = Consult() self.crypt = Crypt() def execute_consult(self): self.audit.insert_audit_log( 'Realizando consulta no endpoint do Arquivei') notes = self.api_consult.get_information() if notes: self.audit.insert_audit_log('{} notas encontradas'.format( len(notes['data']))) self.db_insert(notes) else: self.audit.insert_audit_log('Erro na consulta') def db_insert(self, notes): for note in notes['data']: self.audit.insert_audit_log('Inserindo chave de acesso: {}'.format( note['access_key'])) access_key = self.crypt.encrypt(note['access_key']) value = self.crypt.encrypt(str(base64.b64decode(note['xml']))) self.db.insert_file(access_key, value)
class Notes(Resource): def __init__(self): self.db = DbController() self.crypt = Crypt() self.audit = Audit() self.audit.set_info(X_API_KEY, 'Arquivei-Consulta') def get(self, note): notes = self.db.select_all_keys() for dbnote in notes: if note == self.crypt.decrypt(dbnote[1]): self.audit.insert_audit_log( 'Nota pesquisada: {} - Encontrada'.format(note)) return self.crypt.decrypt(dbnote[2]), 200 self.audit.insert_audit_log( 'Nota pesquisada: {} - Nao encontrada'.format(note)) return "Nota nao encontrada", 404
class DbController(metaclass=Singleton): def __init__(self, db_name=None): self.db_name = db_name if db_name else DB_NAME self.conn = self.connect() self.audit = Audit() def connect(self): host = 'localhost' port = 3306 user = '******' password = '******' return pymysql.connect(host=host, port=port, user=user, password=password, db=self.db_name, charset='utf8mb4') def insert(self, sql): try: cursor = self.conn.cursor() cursor.execute(sql) self.conn.commit() id = cursor.lastrowid cursor.close() return id except Exception as e: raise Exception('error saving on table: ' + str(e)) def select(self, sql, dict=False): try: if dict: cursor = self.conn.cursor(pymysql.cursors.DictCursor) else: cursor = self.conn.cursor() cursor.execute(sql) result = cursor.fetchall() return result except pymysql.Error as e: self.audit.insert_audit_log('error select on table: ' + str(e)) def insert_file(self, access_key, value, table=DB_TABLE_NOTES): try: sql = """INSERT INTO {} (access_key, value) VALUES ('{}','{}')""".format( table, access_key, value) return self.insert(sql) except Exception as e: self.audit.insert_audit_log('Error insert new file: ' + str(e)) def select_all_keys(self, table=DB_TABLE_NOTES): try: sql = """SELECT * FROM {}""".format(table, dict=True) return self.select(sql) except Exception as e: self.audit.insert_audit_log('Error insert new file: ' + str(e))