Skip to content

Commit

Permalink
added set bandwidth call to api
Browse files Browse the repository at this point in the history
  • Loading branch information
F483 committed Mar 10, 2016
1 parent ca36434 commit a89bc6b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
11 changes: 10 additions & 1 deletion dataserv/Farmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Farmer(db.Model):
last_seen = db.Column(DateTime, index=True, default=datetime.utcnow)
reg_time = db.Column(DateTime, default=datetime.utcnow)
uptime = db.Column(db.Interval, default=timedelta(seconds=0))
bandwidth = db.Column(db.Integer, default=0) # TODO add set bandwidth call
bandwidth = db.Column(db.Integer, default=0)
ip = db.Column(db.String(40), default="")

def __init__(self, btc_addr, last_seen=None):
Expand Down Expand Up @@ -159,6 +159,15 @@ def set_height(self, height, ip=None):
db.session.commit()
return self.height

def set_bandwidth(self, bandwidth, ip=None):
farmer = self.lookup()
farmer.bandwidth = bandwidth
# better 2 db commits than implementing ping with
# update calculation again
self.ping(ip=ip)
db.session.commit()
return self.bandwidth

def calculate_uptime(self):
"""Calculate uptime from registration date."""
farmer = self.lookup()
Expand Down
28 changes: 28 additions & 0 deletions dataserv/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,34 @@ def total():
return resp


@app.route('/api/bandwidth/<btc_addr>/<int:bandwidth>', methods=["GET"])
def set_bandwidth(btc_addr, bandwidth):
logger.info("CALLED /api/bandwidth/{0}/{1}".format(btc_addr, bandwidth))
error_msg = "Set height failed: {0}"
try:
user = Farmer(btc_addr)
user.authenticate(dict(request.headers))
assert(bandwidth >= 0)
user.set_bandwidth(bandwidth, ip=request.remote_addr)
return make_response("Bandwidth accepted.", 200)
except AssertionError:
msg = "Invalid bandwidth value: {0}".format(bandwidth)
logger.warning(msg)
return make_response(msg, 400)
except ValueError:
msg = "Invalid Bitcoin address."
logger.warning(msg)
return make_response(msg, 400)
except LookupError:
msg = "Farmer not found."
logger.warning(msg)
return make_response(msg, 404)
except storjcore.auth.AuthError:
msg = "Invalid authentication headers."
logger.warning(msg)
return make_response(error_msg.format(msg), 401)


@app.route('/api/height/<btc_addr>/<int:height>', methods=["GET"])
def set_height(btc_addr, height):
logger.info("CALLED /api/height/{0}/{1}".format(btc_addr, height))
Expand Down

0 comments on commit a89bc6b

Please sign in to comment.