-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli_api.py
70 lines (55 loc) · 1.93 KB
/
cli_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import sqlite3
from encrypt import Encrypt
class Apikey:
def __init__(self, db):
self.db = db
self.make_table()
def api_db_connect(self):
return sqlite3.connect(self.db)
# make a table to store the api keys
def make_table(self):
schema = """CREATE TABLE IF NOT EXISTS apikey
(id TEXT PRIMARY KEY,
key TEXT UNIQUE NOT NULL);"""
with self.api_db_connect() as db:
c = db.cursor()
c.execute(schema)
db.commit()
# use the uid to generate the new api key using a lot of maths
def generateAndAdd_api_key(self, uid, seed):
key = Encrypt(str(uid), str(seed))
# add the api key to the database
with self.api_db_connect() as db:
c = db.cursor()
c.execute(
"INSERT INTO apikey (id, key) " "VALUES (?, ?)",
(uid, key),
)
db.commit()
return key
# write a funtion to retrive the uid using the key from the database
def get_(self, key):
with self.api_db_connect() as db:
userkey = db.execute(
"SELECT * FROM apikey WHERE key = ?", (key,)
).fetchall()
if not userkey:
return None
uid_ = userkey[0][0]
return uid_
# write a funtion to verify if a key already exists in the database
def exists_(self, uid):
with self.api_db_connect() as db:
c = db.cursor()
userid = c.execute(f"SELECT * FROM apikey WHERE id = {uid}").fetchall()
if not userid:
return False
else:
return True
# write a function to delte an apikey from the database
def delete(self, uid):
uid_ = str(uid)
delquery = f"""DELETE FROM apikey WHERE id={uid_};"""
with self.api_db_connect() as db:
c = db.cursor()
c.execute(delquery)