import logging
from typing import Any
from actingweb.db import get_peer_trustee
logger = logging.getLogger(__name__)
[docs]
class PeerTrustee:
[docs]
def get(self) -> dict[str, Any] | bool | None:
"""Retrieve peer trustee from database.
Returns:
dict[str, Any]: Peer trustee data if exactly one match found
False: When multiple peer trustees of same type exist (ambiguous lookup)
None: When no peer trustee found or required parameters missing
"""
if (
self.peertrustee
and not isinstance(self.peertrustee, bool)
and len(self.peertrustee) > 0
):
return self.peertrustee
if self.handle:
self.peertrustee = self.handle.get(
actor_id=self.actor_id, peerid=self.peerid, peer_type=self.peer_type
)
else:
self.peertrustee = {}
return self.peertrustee
[docs]
def create(self, baseuri=None, passphrase=None):
if not self.handle:
if self.config:
self.handle = get_peer_trustee(self.config)
else:
return False
if not self.actor_id or not self.peerid:
logger.debug(
"Attempt to create new peer trustee without actor_id or peerid set"
)
return False
if not self.peer_type or len(self.peer_type) == 0:
logger.debug("Attempt to create peer trustee without peer_type set.")
return False
return self.handle.create(
actor_id=self.actor_id,
peerid=self.peerid,
peer_type=self.peer_type,
baseuri=baseuri,
passphrase=passphrase,
)
[docs]
def delete(self):
if not self.handle:
logger.debug("Attempt to delete peertrustee without db handle")
return False
return self.handle.delete()
def __init__(
self, actor_id=None, peerid=None, short_type=None, peer_type=None, config=None
):
self.config = config
if self.config:
self.handle = get_peer_trustee(self.config)
else:
self.handle = None
self.peertrustee = {}
self.peer_type = None
if not actor_id or len(actor_id) == 0:
logger.debug("No actorid set in initialisation of peertrust")
return
if peer_type:
self.peer_type = peer_type
elif not peer_type and short_type:
if (
not self.config
or not self.config.actors
or short_type not in self.config.actors
):
logger.error(
"Got request to initialise peer trustee with unknown shortpeer_type("
+ (peer_type or "None")
+ ")"
)
return
self.peer_type = self.config.actors[short_type]["type"]
elif not peerid:
logger.debug(
"Peerid and short_type are not set in initialisation of peertrustee. One is required"
)
return
self.actor_id = actor_id
self.peerid = peerid
if self.handle:
self.get()