Skip to content

Commit

Permalink
better separation of request and receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
ndenny committed Oct 28, 2024
1 parent a755cfc commit 436fc70
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions lib/Bastion/CARP.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,46 @@ def is_inconclusive(self):
ResultStatus = ReplyStatus #-- Alias for ReplyStatus


class isRequest:
def __init__(self, action, *args, **kwargs):
self.ID = kwargs.get('ID', str(uuid.uuid4()))
self.action = action
self.args = args
self.when = kwargs.get('when', datetime.datetime.now())

def toJDN(self, **kwargs):
jdn = {
'ID': self.ID,
'action': self.action,
'args': list(self.args),
'opened': self.when.isoformat()
}
return jdn


class Request(isRequest):
pass


class isReceipt:
def __init__(self, request, status, brief, obj, context = None, **kwargs):
#-- request is a 2-tuple of (action, [arg1, ...])
#-- status is an instance of ReplyStatus
#-- brief is an arbitrary (assumed human readable) text string
#-- obj is any JSON serializable object.
#-- context, if given, is a dict of extra key-value associations.
if not isinstance(request, isRequest):
raise ValueError("request must be of type isRequest")

if not isinstance(status, ReplyStatus):
raise ValueError("status must be of type ReplyStatus")

self.RID = kwargs.get('ID', str(uuid.uuid4()))
self.action = request[0]
self.args = request[1]
self.request = request
self.status = status
self.brief = str(msg)
self.body = obj
self.context = { }
self.when = kwargs.get('when', datetime.datetime.now())

if context is not None:
self.context = context.copy()
Expand All @@ -143,20 +166,19 @@ def toJDN(self, **kwargs):

jdn = { }

jdn['request'] = {
'ID': self.RID,
'action': self.action,
'args': list(self.args),
'context': self.context.copy(),
}
jdn['request'] = self.request.toJDN(**kwargs)

jdn['request']['result'] = {
'status': self.status.code,
'message': self.status.gloss,
'summary': self.brief,
'closed': self.when.isoformat()
}

jdn['request']['result']['body'] = {
'data': payload
}

return jdn

@property
Expand All @@ -183,7 +205,7 @@ def __init__(self, status, brief, doc, obj = None, **kwargs):

def toJDN(self, **kwargs):
jdn = isReceipt.toJDN(self, **kwargs)
jdn['request']['result']['report'] = str(self.report)
jdn['request']['result']['body']['report'] = str(self.report)
return jdn


Expand Down

0 comments on commit 436fc70

Please sign in to comment.