Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating interceptor #65

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions src/keria/app/agenting.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,25 @@
from ..core.authing import Authenticater
from ..core.keeping import RemoteManager
from ..db import basing

import requests
logger = ogler.getLogger()

#function to push data to an webhook endpoint

class Interceptor:

def setup(name, bran, adminPort, bootPort, base='', httpPort=None, configFile=None, configDir=None):
def __init__(self, webhook, headers):
self.webhook = webhook
self.headers = headers

def push(self, data):
try:
resp = requests.post(self.webhook, data=json.dumps(data), headers=self.headers)
if resp.status_code != 200:
logger.info('Error in pushing data to webhook')
except Exception as e:
logger.info('Error in pushing data to webhook')
def setup(name, bran, adminPort, bootPort, base='', httpPort=None, configFile=None, configDir=None, interceptor_webhook=None, interceptor_headers=None):
""" Set up an ahab in Signify mode """

agency = Agency(name=name, base=base, bran=bran, configFile=configFile, configDir=configDir)
Expand Down Expand Up @@ -107,14 +121,16 @@ def setup(name, bran, adminPort, bootPort, base='', httpPort=None, configFile=No


class Agency(doing.DoDoer):
def __init__(self, name, bran, base="", configFile=None, configDir=None, adb=None, temp=False):
def __init__(self, name, bran, base="", configFile=None, configDir=None, adb=None, temp=False,interceptor_webhook=None,interceptor_headers=None):
self.name = name
self.base = base
self.bran = bran
self.temp = temp
self.configFile = configFile
self.configDir = configDir
self.cf = None
self.interceptor_webhook = interceptor_webhook
self.interceptor_headers = interceptor_headers
if self.configFile is not None: # Load config file if creating database
self.cf = configing.Configer(name=self.configFile,
base="",
Expand Down Expand Up @@ -159,7 +175,10 @@ def create(self, caid):
caid=caid,
agency=self,
configDir=self.configDir,
configFile=self.configFile)
configFile=self.configFile,
interceptor_webhook=self.interceptor_webhook,
interceptor_headers=self.interceptor_headers
)

self.adb.agnt.pin(keys=(caid,),
val=coring.Prefixer(qb64=agent.pre))
Expand Down Expand Up @@ -247,6 +266,10 @@ def __init__(self, hby, rgy, agentHab, agency, caid, **opts):

self.mgr = RemoteManager(hby=hby)


interceptor = Interceptor(self.agency.interceptor_webhook, self.agency.interceptor_headers)


self.cues = decking.Deck()
self.groups = decking.Deck()
self.anchors = decking.Deck()
Expand Down Expand Up @@ -313,10 +336,10 @@ def __init__(self, hby, rgy, agentHab, agency, caid, **opts):
Escrower(kvy=self.kvy, rgy=self.rgy, rvy=self.rvy, tvy=self.tvy, exc=self.exc, vry=self.verifier,
registrar=self.registrar, credentialer=self.credentialer),
Messager(kvy=self.kvy, parser=self.parser),
Witnesser(receiptor=receiptor, witners=self.witners),
Delegator(agentHab=agentHab, swain=self.swain, anchors=self.anchors),
Witnesser(receiptor=receiptor, witners=self.witners, interceptor=interceptor),
Delegator(agentHab=agentHab, swain=self.swain, anchors=self.anchors,interceptor=interceptor),
GroupRequester(hby=hby, agentHab=agentHab, postman=self.postman, counselor=self.counselor,
groups=self.groups),
groups=self.groups,interceptor=interceptor),
])

super(Agent, self).__init__(doers=doers, always=True, **opts)
Expand Down Expand Up @@ -366,15 +389,17 @@ def recur(self, tyme=None):

class Witnesser(doing.Doer):

def __init__(self, receiptor, witners):
def __init__(self, receiptor, witners, interceptor):
self.receiptor = receiptor
self.witners = witners
self.interceptor = interceptor
super(Witnesser, self).__init__()

def recur(self, tyme=None):
while True:
if self.witners:
msg = self.witners.popleft()
self.interceptor.push(msg)
serder = msg["serder"]

# If we are a rotation event, may need to catch new witnesses up to current key state
Expand All @@ -390,15 +415,17 @@ def recur(self, tyme=None):

class Delegator(doing.Doer):

def __init__(self, agentHab, swain, anchors):
def __init__(self, agentHab, swain, anchors, interceptor):
self.agentHab = agentHab
self.swain = swain
self.anchors = anchors
self.interceptor = interceptor
super(Delegator, self).__init__()

def recur(self, tyme=None):
if self.anchors:
msg = self.anchors.popleft()
self.interceptor.push(msg)
sn = msg["sn"] if "sn" in msg else None
self.swain.delegation(pre=msg["pre"], sn=sn, proxy=self.agentHab)

Expand All @@ -422,19 +449,20 @@ def recur(self, tyme):

class GroupRequester(doing.Doer):

def __init__(self, hby, agentHab, postman, counselor, groups):
def __init__(self, hby, agentHab, postman, counselor, groups, interceptor):
self.hby = hby
self.agentHab = agentHab
self.postman = postman
self.counselor = counselor
self.groups = groups

self.interceptor = interceptor
super(GroupRequester, self).__init__()

def recur(self, tyme):
""" Checks cue for group proceccing requests and processes any with Counselor """
if self.groups:
msg = self.groups.popleft()
self.interceptor.push(msg)
serder = msg["serder"]
sigers = msg["sigers"]

Expand Down
Loading
Loading