Browse Source

refactor: clean database calls in `/v1/invoices` view

And return fails first to simplify if..else and make the
code more readable.
fee_issues
Eneko Illarramendi 5 years ago
parent
commit
dfd00cfe05
  1. 70
      LNbits/__init__.py

70
LNbits/__init__.py

@ -269,69 +269,49 @@ def wallet():
) )
# API requests
@app.route("/v1/invoices", methods=["GET", "POST"]) @app.route("/v1/invoices", methods=["GET", "POST"])
def api_invoices(): def api_invoices():
if request.headers["Content-Type"] == "application/json": if request.headers["Content-Type"] != "application/json":
return jsonify({"ERROR": "MUST BE JSON"}), 200
postedjson = request.json postedjson = request.json
print(postedjson)
if "value" in postedjson: if "value" not in postedjson:
if postedjson["value"].isdigit() == True: return jsonify({"ERROR": "NO VALUE"}), 200
if "memo" in postedjson:
con = db_connect()
cur = con.cursor()
cur.execute( if not postedjson["value"].isdigit():
"select * from wallets WHERE inkey = '" + request.headers["Grpc-Metadata-macaroon"] + "'" return jsonify({"ERROR": "VALUE MUST BE A NUMMBER"}), 200
)
rows = cur.fetchall()
if len(rows) > 0: if "memo" not in postedjson:
cur.close() return jsonify({"ERROR": "NO MEMO"}), 200
dataj = {"amt": postedjson["value"], "memo": postedjson["memo"]} with Database() as db:
headers = {"Authorization": "Basic %s" % INVOICE_KEY} rows = db.fetchall("SELECT * FROM wallets WHERE inkey = ?", (request.headers["Grpc-Metadata-macaroon"],))
r = requests.post(url=API_ENDPOINT + "/addinvoice", json=dataj, headers=headers)
if len(rows) == 0:
return jsonify({"ERROR": "NO KEY"}), 200
dataj = {"amt": postedjson["value"], "memo": postedjson["memo"]}
headers = {"Authorization": f"Basic {INVOICE_KEY}"}
r = requests.post(url=f"{API_ENDPOINT}/addinvoice", json=dataj, headers=headers)
data = r.json() data = r.json()
pay_req = data["pay_req"] pay_req = data["pay_req"]
payment_hash = data["payment_hash"] payment_hash = data["payment_hash"]
con = db_connect() db.execute(
cur = con.cursor() "INSERT INTO apipayments (payhash, amount, wallet, paid, inkey, memo) VALUES (?, ?, ?, 0, ?, ?)'",
(
cur.execute( payment_hash,
"INSERT INTO apipayments (payhash, amount, wallet, paid, inkey, memo) VALUES ('" postedjson["value"],
+ payment_hash rows[0][0],
+ "','" request.headers["Grpc-Metadata-macaroon"],
+ postedjson["value"] postedjson["memo"],
+ "','" ),
+ rows[0][0]
+ "','0','"
+ request.headers["Grpc-Metadata-macaroon"]
+ "','"
+ postedjson["memo"]
+ "')"
) )
con.commit()
cur.close()
return jsonify({"pay_req": pay_req, "payment_hash": payment_hash}), 200 return jsonify({"pay_req": pay_req, "payment_hash": payment_hash}), 200
else:
return jsonify({"ERROR": "NO KEY"}), 200
else:
return jsonify({"ERROR": "NO MEMO"}), 200
else:
return jsonify({"ERROR": "VALUE MUST BE A NUMMBER"}), 200
else:
return jsonify({"ERROR": "NO VALUE"}), 200
else:
return jsonify({"ERROR": "MUST BE JSON"}), 200
# API requests # API requests
@app.route("/v1/channels/transactions", methods=["GET", "POST"]) @app.route("/v1/channels/transactions", methods=["GET", "POST"])

Loading…
Cancel
Save