import calendar
+import io
import json
import os
import re
self.log_response = log_response
- def method(self, method, path, base, params, links={}):
+ def method_start(self, method, path, base, params, links={}):
headers = {}
if self.bearer_token is not None:
headers['Authorization'] = 'Bearer ' + self.bearer_token
break
links[m.group(2)] = m.group(1)
linkhdr = linkhdr[m.end():]
- return rsp.json()
+ return rsp
+
+ def method(self, method, path, base, params, links={}):
+ return self.method_start(method, path, base, params, links).json()
def get(self, path, base='api', **params):
return self.method(requests.get, path, base, params)
data = self.method(requests.get, link, None, {}, links)
return data, links
+ def get_streaming_lines(self, path, base='api', **params):
+ reqgetstream = lambda *args, **kws: requests.get(
+ *args, stream=True, **kws)
+ rsp = self.method_start(reqgetstream, path, base, params, {})
+ if rsp.status_code != 200:
+ raise HTTPError(rsp)
+
+ it = rsp.iter_content(None)
+ fh = io.BytesIO()
+ for chunk in it:
+ while b'\n' in chunk:
+ pos = chunk.index(b'\n')
+ fh.write(chunk[:pos])
+ chunk = chunk[pos+1:]
+
+ yield fh.getvalue().decode('utf-8', errors='replace')
+ fh = io.BytesIO()
+
+ fh.write(chunk)
+
def get_url(self, path, base='api', **params):
r = requests.Request(method="GET", url=self.urls[base] + path,
params=params)
for line in thing.render(80):
print(line.ecma48())
+class StreamUI(client.Client):
+ def run(self):
+ import time
+ for chunk in self.get_streaming_lines("streaming/user"):
+ print(time.strftime("%Y-%m-%d %H:%M:%S"), repr(chunk))
+
class MyTestLoader(unittest.TestLoader):
def loadTestsFromModule(self, module):
suite = super().loadTestsFromModule(module)
const=CombinedUI, help="Temporary mode to fetch "
"the user's timeline and mentions, interleave them, "
"and print the result on the terminal.")
+ parser.add_argument("--stream", action="store_const", dest="action",
+ const=StreamUI, help="Test mode for streaming "
+ "HTTP retrievals.")
parser.add_argument("--login", action="store_const", dest="action",
const=login.LoginUI, help="Log in to a user account.")
parser.set_defaults(action=cursesclient.CursesUI)