-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
61 lines (44 loc) · 1.5 KB
/
app.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
# stdlib
import json
# flask
from flask import Flask, jsonify, request
# deps
from web3 import Web3
# local
from src.exceptions import ProductDoesNotExists
from src.products import (
accept_product,
create_product,
delegate_product,
get_product,
get_product_by_name,
get_products,
)
app = Flask(__name__)
@app.route("/")
def products():
products = get_products()
return jsonify({"products": products})
@app.route("/product/<int:prod_id>")
def product(prod_id):
try:
product = get_product(prod_id)
except ProductDoesNotExists:
return {"error": f"Product {prod_id} does not exists."}
return jsonify({"product": product.to_dict()})
@app.route("/product/<prod_name>")
def find(prod_name):
product = get_product_by_name(prod_name)
return jsonify({"product": product})
@app.route("/product/", methods=["POST"])
def add():
product = create_product(request.form["name"], request.form["address"])
return jsonify({"transaction_hash": json.loads(Web3.toJSON(product))})
@app.route("/product/<int:prod_id>/delegate/", methods=["POST"])
def delegate(prod_id):
product = delegate_product(prod_id, request.form["address"], request.form["new_address"])
return jsonify({"transaction_hash": json.loads(Web3.toJSON(product))})
@app.route("/product/<int:prod_id>/accept/", methods=["POST"])
def accept(prod_id):
product = accept_product(prod_id, request.form["address"])
return jsonify({"transaction_hash": json.loads(Web3.toJSON(product))})