Spaces:
Running
Running
Se agregó nuevo endpoint Authorize
Browse files- avaimet.py +29 -0
- funciones.py +20 -1
- main.py +27 -2
avaimet.py
CHANGED
@@ -45,6 +45,35 @@ def obtenTokens(sftp, caja):
|
|
45 |
|
46 |
return tokens
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
def restaToken(sftp, caja, tokens, work):
|
49 |
|
50 |
#Standard cost.
|
|
|
45 |
|
46 |
return tokens
|
47 |
|
48 |
+
def autoriza(tokens, work):
|
49 |
+
|
50 |
+
print(tokens)
|
51 |
+
|
52 |
+
#Standard cost.
|
53 |
+
costo_tarea = 2
|
54 |
+
|
55 |
+
#Aplica reglas de cobro de tokens.
|
56 |
+
#Posteriormente las equivalencias de tardeas y costos vendrán de una tabla aparte.
|
57 |
+
#Por ahora se definen via éste IF:
|
58 |
+
if work == 'picswap':
|
59 |
+
costo_tarea = 1
|
60 |
+
print(f"Work: {work}, tokens cost: {costo_tarea}")
|
61 |
+
time.sleep(1)
|
62 |
+
else:
|
63 |
+
print("The work specified doesn't exists.")
|
64 |
+
|
65 |
+
#Ahora evaluaremos si se tiene el suficiente crédito como para ejecutar la tarea.
|
66 |
+
if tokens >= costo_tarea:
|
67 |
+
print("Tarea autorizada...")
|
68 |
+
result = True
|
69 |
+
else:
|
70 |
+
print("Tarea no autorizada, no tienes suficientes tokens...")
|
71 |
+
result = False
|
72 |
+
|
73 |
+
return result
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
def restaToken(sftp, caja, tokens, work):
|
78 |
|
79 |
#Standard cost.
|
funciones.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import time
|
2 |
import avaimet
|
3 |
|
4 |
-
def
|
5 |
|
6 |
#Genera conexión inicial.
|
7 |
sshListo, sftpListo = avaimet.conecta()
|
@@ -14,6 +14,25 @@ def getAccess(userfile):
|
|
14 |
|
15 |
return tokens
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def debitTokens(userfile, work):
|
18 |
|
19 |
print(f"Task received : {work}, type: {type(work)} ...")
|
|
|
1 |
import time
|
2 |
import avaimet
|
3 |
|
4 |
+
def getTokens(userfile):
|
5 |
|
6 |
#Genera conexión inicial.
|
7 |
sshListo, sftpListo = avaimet.conecta()
|
|
|
14 |
|
15 |
return tokens
|
16 |
|
17 |
+
def authorize(userfile, work):
|
18 |
+
|
19 |
+
print(f"Task received : {work}, type: {type(work)} ...")
|
20 |
+
|
21 |
+
#Genera conexión inicial.
|
22 |
+
sshListo, sftpListo = avaimet.conecta()
|
23 |
+
#Obtiene la caja donde está guardados los tokens.
|
24 |
+
caja = avaimet.obtenCaja(userfile)
|
25 |
+
#Obtiene los tokens que hay en esa caja.
|
26 |
+
tokens = avaimet.obtenTokens(sftpListo, caja)
|
27 |
+
|
28 |
+
#True si autoriza o false si no autoriza.
|
29 |
+
result = avaimet.autoriza(tokens, work)
|
30 |
+
|
31 |
+
#Cierra la conexión.
|
32 |
+
avaimet.cierraConexion(sshListo, sftpListo)
|
33 |
+
|
34 |
+
return result
|
35 |
+
|
36 |
def debitTokens(userfile, work):
|
37 |
|
38 |
print(f"Task received : {work}, type: {type(work)} ...")
|
main.py
CHANGED
@@ -8,24 +8,49 @@ def start():
|
|
8 |
|
9 |
return {"Status":"Deployed"}
|
10 |
|
|
|
|
|
|
|
11 |
@app.get("/getTokens/{userfile}")
|
12 |
def getTokens(userfile: str):
|
13 |
-
tokens = funciones.
|
14 |
print("Tipo de resultado:", type(tokens))
|
15 |
return tokens
|
16 |
|
|
|
17 |
@app.get("/getTokensQ/")
|
18 |
def getTokens(userfile: str = "gAAAAABmEZA4SLBC2YczouOrjIEi9WNCNGOIvyUcqBUnzxNsftXTdy54KaX9x8mAjFkABSI6FJrdZDQKk_5lpJOgJoMChxlniw=="):
|
19 |
-
tokens = funciones.
|
20 |
print("Tipo de resultado:", type(tokens))
|
21 |
return tokens
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
@app.get("/debitTokens/{userfile}/{work}")
|
24 |
def debitTokens(userfile: str, work: str):
|
25 |
tokens = funciones.debitTokens(userfile,work)
|
26 |
print("Tipo de resultado:", type(tokens))
|
27 |
return tokens
|
28 |
|
|
|
29 |
@app.get("/debitTokensQ/")
|
30 |
def debitTokens(userfile: str, work: str = "picswap"):
|
31 |
tokens = funciones.debitTokens(userfile,work)
|
|
|
8 |
|
9 |
return {"Status":"Deployed"}
|
10 |
|
11 |
+
## GET TOKENS ##
|
12 |
+
|
13 |
+
#Vía Path
|
14 |
@app.get("/getTokens/{userfile}")
|
15 |
def getTokens(userfile: str):
|
16 |
+
tokens = funciones.getTokens(userfile)
|
17 |
print("Tipo de resultado:", type(tokens))
|
18 |
return tokens
|
19 |
|
20 |
+
#Vía Query
|
21 |
@app.get("/getTokensQ/")
|
22 |
def getTokens(userfile: str = "gAAAAABmEZA4SLBC2YczouOrjIEi9WNCNGOIvyUcqBUnzxNsftXTdy54KaX9x8mAjFkABSI6FJrdZDQKk_5lpJOgJoMChxlniw=="):
|
23 |
+
tokens = funciones.getTokens(userfile)
|
24 |
print("Tipo de resultado:", type(tokens))
|
25 |
return tokens
|
26 |
|
27 |
+
## AUTHORIZE WORK ##
|
28 |
+
|
29 |
+
#Vía Parameters
|
30 |
+
@app.get("/authorize/{tokens}/{work}")
|
31 |
+
def getTokens(tokens: int, work: str):
|
32 |
+
autorizacion = funciones.authorize(tokens, work)
|
33 |
+
print("Tipo de resultado:", type(autorizacion))
|
34 |
+
return autorizacion
|
35 |
+
|
36 |
+
#Vía Query
|
37 |
+
@app.get("/authorizeQ/")
|
38 |
+
def debitTokens(tokens: int, work: str = "picswap"):
|
39 |
+
autorizacion = funciones.authorize(tokens,work)
|
40 |
+
print("Tipo de resultado:", type(autorizacion))
|
41 |
+
return tokens
|
42 |
+
|
43 |
+
|
44 |
+
## DEBIT TOKENS ##
|
45 |
+
|
46 |
+
#Vía Parámeters
|
47 |
@app.get("/debitTokens/{userfile}/{work}")
|
48 |
def debitTokens(userfile: str, work: str):
|
49 |
tokens = funciones.debitTokens(userfile,work)
|
50 |
print("Tipo de resultado:", type(tokens))
|
51 |
return tokens
|
52 |
|
53 |
+
#Vía Query
|
54 |
@app.get("/debitTokensQ/")
|
55 |
def debitTokens(userfile: str, work: str = "picswap"):
|
56 |
tokens = funciones.debitTokens(userfile,work)
|