Mercurial > code > home > repos > homeauto
annotate service/reasoning/httpputoutputs.py @ 1560:c3d699b5759c
more py3 fixes
Ignore-this: f212b4a5edf8e599e9efd70bc65e7651
darcs-hash:d944ca9d7d7b36c2c02529dcf9225a99c0aa1831
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Fri, 14 Feb 2020 00:33:31 -0800 |
parents | d36d3b9ae516 |
children |
rev | line source |
---|---|
1521 | 1 import logging |
2 import time | |
3 | |
1556
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
4 from rdflib import URIRef |
1521 | 5 from rx.subjects import BehaviorSubject |
6 from twisted.internet import reactor | |
1556
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
7 from twisted.python.failure import Failure |
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
8 from twisted.internet.interfaces import IDelayedCall |
1521 | 9 import treq |
1556
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
10 from typing import Optional |
1521 | 11 |
12 log = logging.getLogger('httpputoutputs') | |
13 | |
1556
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
14 |
1521 | 15 class HttpPutOutput(object): |
1556
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
16 lastChangeTime: float |
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
17 |
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
18 def __init__(self, url: str, |
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
19 refreshSecs: BehaviorSubject, |
1521 | 20 mockOutput=False): |
21 self.url = url | |
22 self.mockOutput = mockOutput | |
1556
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
23 self.payload: Optional[str] = None |
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
24 self.foafAgent: Optional[URIRef] = None |
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
25 self.nextCall: IDelayedCall = None |
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
26 self.lastErr: Optional[Failure] = None |
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
27 self.numRequests: int = 0 |
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
28 self.refreshSecs: float = refreshSecs |
1521 | 29 |
30 def report(self): | |
31 return { | |
32 'url': self.url, | |
33 'urlAbbrev': self.url | |
34 .replace('http%3A%2F%2Fprojects.bigasterisk.com%2Froom%2F', ':') | |
35 .replace('http://projects.bigasterisk.com/room/', ':') | |
36 .replace('.vpn-home.bigasterisk.com', '.vpn-home'), | |
37 'payload': self.payload, | |
38 'numRequests': self.numRequests, | |
39 'lastChangeTime': round(self.lastChangeTime, 2), | |
40 'lastErr': str(self.lastErr) if self.lastErr is not None else None, | |
41 } | |
42 | |
1556
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
43 def setPayload(self, payload: str, foafAgent: URIRef): |
1521 | 44 if self.numRequests > 0 and (self.payload == payload and |
45 self.foafAgent == foafAgent): | |
46 return | |
47 self.payload = payload | |
48 self.foafAgent = foafAgent | |
49 self.lastChangeTime = time.time() | |
50 self.makeRequest() | |
51 | |
52 def makeRequest(self): | |
53 if self.payload is None: | |
54 log.debug("PUT None to %s - waiting", self.url) | |
55 return | |
56 h = {} | |
57 if self.foafAgent: | |
58 h['x-foaf-agent'] = self.foafAgent | |
59 if self.nextCall and self.nextCall.active(): | |
60 self.nextCall.cancel() | |
61 self.nextCall = None | |
62 self.lastErr = None | |
63 log.debug("PUT %s payload=%s agent=%s", | |
64 self.url, self.payload, self.foafAgent) | |
65 if not self.mockOutput: | |
1560 | 66 self.currentRequest = treq.put(self.url, data=self.payload.encode('utf8'), |
1521 | 67 headers=h, timeout=3) |
68 self.currentRequest.addCallback(self.onResponse).addErrback( | |
69 self.onError) | |
70 else: | |
71 reactor.callLater(.2, self.onResponse, None) | |
72 | |
73 self.numRequests += 1 | |
74 | |
75 def currentRefreshSecs(self): | |
76 out = None | |
77 if 1: | |
78 # workaround | |
79 def secsFromLiteral(v): | |
80 if v[-1] != 's': | |
81 raise NotImplementedError(v) | |
82 return float(v[:-1]) | |
83 | |
84 out = secsFromLiteral(self.refreshSecs.value) | |
85 else: | |
86 # goal: caller should map secsFromLiteral on the | |
87 # observable, so we see a float | |
88 def recv(v): | |
89 log.info('recv %r', v) | |
90 import ipdb;ipdb.set_trace() | |
91 self.refreshSecs.subscribe(recv) | |
92 if out is None: | |
93 raise ValueError('refreshSecs had no value') | |
94 log.debug(' got refresh %r', out) | |
95 return out | |
96 | |
97 def onResponse(self, resp): | |
98 log.debug(" PUT %s ok", self.url) | |
99 self.lastErr = None | |
100 self.currentRequest = None | |
101 self.nextCall = reactor.callLater(self.currentRefreshSecs(), | |
102 self.makeRequest) | |
103 | |
104 def onError(self, err): | |
105 self.lastErr = err | |
106 log.debug(' PUT %s failed: %s', self.url, err) | |
107 self.currentRequest = None | |
108 self.nextCall = reactor.callLater(self.currentRefreshSecs(), | |
109 self.makeRequest) | |
110 | |
1556
d36d3b9ae516
python 3! and some types and cleanups
drewp <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
111 |
1521 | 112 class HttpPutOutputs(object): |
113 """these grow forever""" | |
114 def __init__(self, mockOutput=False): | |
115 self.mockOutput = mockOutput | |
116 self.state = {} # url: HttpPutOutput | |
117 | |
1560 | 118 def put(self, url: str, payload: str, foafAgent: str, refreshSecs: float): |
1521 | 119 if url not in self.state: |
120 self.state[url] = HttpPutOutput(url, mockOutput=self.mockOutput, | |
121 refreshSecs=refreshSecs) | |
122 self.state[url].setPayload(payload, foafAgent) |