mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 14:14:22 +01:00
plugin - defines TowerSummary as object to improve readability of the code
This commit is contained in:
@@ -59,7 +59,7 @@ def add_appointment(plugin, tower_id, tower, appointment_dict, signature):
|
||||
def send_appointment(tower_id, tower, appointment_dict, signature):
|
||||
data = {"appointment": appointment_dict, "signature": signature}
|
||||
|
||||
add_appointment_endpoint = f"{tower.get('netaddr')}/add_appointment"
|
||||
add_appointment_endpoint = f"{tower.netaddr}/add_appointment"
|
||||
response = process_post_response(post_request(data, add_appointment_endpoint, tower_id))
|
||||
|
||||
tower_signature = response.get("signature")
|
||||
|
||||
@@ -49,10 +49,16 @@ class TowerInfo:
|
||||
return self.__dict__
|
||||
|
||||
def get_summary(self):
|
||||
return {
|
||||
"netaddr": self.netaddr,
|
||||
"status": self.status,
|
||||
"available_slots": self.available_slots,
|
||||
"pending_appointments": self.pending_appointments,
|
||||
"invalid_appointments": self.invalid_appointments,
|
||||
}
|
||||
return TowerSummary(self)
|
||||
|
||||
|
||||
class TowerSummary:
|
||||
def __init__(self, tower_info):
|
||||
self.netaddr = tower_info.netaddr
|
||||
self.status = tower_info.status
|
||||
self.available_slots = tower_info.available_slots
|
||||
self.pending_appointments = tower_info.pending_appointments
|
||||
self.invalid_appointments = tower_info.invalid_appointments
|
||||
|
||||
def to_dict(self):
|
||||
return self.__dict__
|
||||
|
||||
@@ -163,7 +163,6 @@ def get_appointment(plugin, tower_id, locator):
|
||||
"""
|
||||
|
||||
# FIXME: All responses from the tower should be signed.
|
||||
|
||||
try:
|
||||
tower_id, locator = arg_parser.parse_get_appointment_arguments(tower_id, locator)
|
||||
|
||||
@@ -175,7 +174,7 @@ def get_appointment(plugin, tower_id, locator):
|
||||
data = {"locator": locator, "signature": signature}
|
||||
|
||||
# Send request to the server.
|
||||
tower_netaddr = plugin.wt_client.towers[tower_id].get("netaddr")
|
||||
tower_netaddr = plugin.wt_client.towers[tower_id].netaddr
|
||||
get_appointment_endpoint = f"{tower_netaddr}/get_appointment"
|
||||
plugin.log(f"Requesting appointment from {tower_id}")
|
||||
|
||||
@@ -190,10 +189,10 @@ def get_appointment(plugin, tower_id, locator):
|
||||
@plugin.method("listtowers", desc="List all towers registered towers.")
|
||||
def list_towers(plugin):
|
||||
towers_info = {"towers": []}
|
||||
for tower_id, info in plugin.wt_client.towers.items():
|
||||
values = {k: v for k, v in info.items() if k != "pending_appointments"}
|
||||
pending_appointments = [appointment.get("locator") for appointment, signature in info["pending_appointments"]]
|
||||
invalid_appointments = [appointment.get("locator") for appointment, signature in info["invalid_appointments"]]
|
||||
for tower_id, tower in plugin.wt_client.towers.items():
|
||||
values = {k: v for k, v in tower.to_dict().items() if k not in ["pending_appointments", "invalid_appointments"]}
|
||||
pending_appointments = [appointment.get("locator") for appointment, signature in tower.pending_appointments]
|
||||
invalid_appointments = [appointment.get("locator") for appointment, signature in tower.invalid_appointments]
|
||||
values["pending_appointments"] = pending_appointments
|
||||
values["invalid_appointments"] = invalid_appointments
|
||||
towers_info["towers"].append({"id": tower_id, **values})
|
||||
@@ -225,15 +224,15 @@ def retry_tower(plugin, tower_id):
|
||||
|
||||
if not tower:
|
||||
response = {"error": f"{tower_id} is not a registered tower"}
|
||||
if tower.get("status") not in ["unreachable", "subscription error"]:
|
||||
response = {"error": f"{tower_id} is not unreachable. {tower.get('status')}"}
|
||||
if not tower.get("pending_appointments"):
|
||||
if tower.status not in ["unreachable", "subscription error"]:
|
||||
response = {"error": f"{tower_id} is not unreachable. {tower.status}"}
|
||||
if not tower.pending_appointments:
|
||||
response = {"error": f"{tower_id} does not have pending appointments"}
|
||||
|
||||
if not response:
|
||||
response = f"Retrying tower {tower_id}"
|
||||
plugin.log(response)
|
||||
plugin.wt_client.towers[tower_id]["status"] = "temporarily unreachable"
|
||||
plugin.wt_client.towers[tower_id].status = "temporarily unreachable"
|
||||
plugin.wt_client.retrier.temp_unreachable_towers.put(tower_id)
|
||||
|
||||
plugin.wt_client.lock.release()
|
||||
@@ -260,11 +259,11 @@ def on_commitment_revocation(plugin, **kwargs):
|
||||
for tower_id, tower in plugin.wt_client.towers.items():
|
||||
tower_update = {}
|
||||
|
||||
if tower.get("status") == "misbehaving":
|
||||
if tower.status == "misbehaving":
|
||||
return {"result": "continue"}
|
||||
|
||||
try:
|
||||
if tower.get("status") == "reachable":
|
||||
if tower.status == "reachable":
|
||||
tower_signature, available_slots = add_appointment(
|
||||
plugin, tower_id, tower, appointment.to_dict(), signature
|
||||
)
|
||||
@@ -272,9 +271,9 @@ def on_commitment_revocation(plugin, **kwargs):
|
||||
tower_update["available_slots"] = available_slots
|
||||
|
||||
else:
|
||||
if tower.get("status") in ["temporarily unreachable", "unreachable"]:
|
||||
plugin.log(f"{tower_id} is {tower.get('status')}. Adding {appointment.locator} to pending")
|
||||
elif tower.get("status") == "subscription error":
|
||||
if tower.status in ["temporarily unreachable", "unreachable"]:
|
||||
plugin.log(f"{tower_id} is {tower.status}. Adding {appointment.locator} to pending")
|
||||
elif tower.status == "subscription error":
|
||||
plugin.log(f"There is a subscription issue with {tower_id}. Adding appointment to pending")
|
||||
|
||||
tower_update["pending_appointment"] = (appointment.to_dict(), signature), "add"
|
||||
|
||||
Reference in New Issue
Block a user