comparison service/reasoning/actions.py @ 1409:e78b8806ad05

actually set put payload this time. add treq dep Ignore-this: b6a05c294d9a5ee6052972cad4ecb5c5 darcs-hash:d41a16eb44db885ff391ddbe64352245b885a838
author drewp <drewp@bigasterisk.com>
date Tue, 23 Jul 2019 17:37:24 -0700
parents 89bf0d204b29
children 7f5451a76a80
comparison
equal deleted inserted replaced
1408:89bf0d204b29 1409:e78b8806ad05
11 REASONING = Namespace("http://projects.bigasterisk.com/ns/reasoning/") 11 REASONING = Namespace("http://projects.bigasterisk.com/ns/reasoning/")
12 12
13 class HttpPutOutput(object): 13 class HttpPutOutput(object):
14 def __init__(self, url): 14 def __init__(self, url):
15 self.url = url 15 self.url = url
16 self.body = None 16 self.payload = None
17 self.foafAgent = None 17 self.foafAgent = None
18 self.nextCall = None 18 self.nextCall = None
19 self.numRequests = 0 19 self.numRequests = 0
20 20
21 def setBody(self, body, foafAgent): 21 def setPayload(self, payload, foafAgent):
22 if self.numRequests > 0 and (self.body == body or self.foafAgent == foafAgent): 22 if self.numRequests > 0 and (self.payload == payload or self.foafAgent == foafAgent):
23 return 23 return
24 self.payload = payload
24 self.foafAgent = foafAgent 25 self.foafAgent = foafAgent
25 self.makeRequest() 26 self.makeRequest()
26 27
27 def makeRequest(self): 28 def makeRequest(self):
28 if self.body is None: 29 if self.payload is None:
29 log.info("PUT None to %s - waiting", self.url) 30 log.info("PUT None to %s - waiting", self.url)
30 return 31 return
31 h = {} 32 h = {}
32 if self.foafAgent: 33 if self.foafAgent:
33 h['x-foaf-agent'] = self.foafAgent 34 h['x-foaf-agent'] = self.foafAgent
34 if self.nextCall and self.nextCall.active(): 35 if self.nextCall and self.nextCall.active():
35 self.nextCall.cancel() 36 self.nextCall.cancel()
36 self.nextCall = None 37 self.nextCall = None
37 self.lastErr = None 38 self.lastErr = None
38 log.info("PUT %s payload=%s agent=%s", self.url, self.body, self.foafAgent) 39 log.info("PUT %s payload=%s agent=%s", self.url, self.payload, self.foafAgent)
39 self.currentRequest = treq.put(self.url, data=self.body, headers=h, timeout=3) 40 self.currentRequest = treq.put(self.url, data=self.payload, headers=h, timeout=3)
40 self.currentRequest.addCallback(self.onResponse).addErrback(self.onError) 41 self.currentRequest.addCallback(self.onResponse).addErrback(self.onError)
41 self.numRequests += 1 42 self.numRequests += 1
42 43
43 def onResponse(self, resp): 44 def onResponse(self, resp):
44 log.info(" PUT %s ok", self.url) 45 log.info(" PUT %s ok", self.url)
55 class HttpPutOutputs(object): 56 class HttpPutOutputs(object):
56 """these grow forever""" 57 """these grow forever"""
57 def __init__(self): 58 def __init__(self):
58 self.state = {} # url: HttpPutOutput 59 self.state = {} # url: HttpPutOutput
59 60
60 def put(self, url, body, foafAgent): 61 def put(self, url, payload, foafAgent):
61 if url not in self.state: 62 if url not in self.state:
62 self.state[url] = HttpPutOutput(url) 63 self.state[url] = HttpPutOutput(url)
63 self.state[url].setBody(body, foafAgent) 64 self.state[url].setPayload(payload, foafAgent)
64 log.info('PutOutputs has %s urls', len(self.state)) 65 log.info('PutOutputs has %s urls', len(self.state))
65 66
66 class Actions(object): 67 class Actions(object):
67 def __init__(self, sendToLiveClients): 68 def __init__(self, sendToLiveClients):
68 self.putOutputs = HttpPutOutputs() 69 self.putOutputs = HttpPutOutputs()