@formation.client
class Google(object):
base_uri = "https://google.com"
middleware = [request_logger(structlog.getLogger())]
response_as = html_response
def search(self, text):
return self.request.get("/", params=Query(text))
if __name__ == "__main__":
google = Google()
(xml, _code, _headers) = google.search("larry page")
print(xml.xpath("//title/text()"))
def log(ctx, call):
print("started")
ctx = call(ctx)
print("ended")
return ctx
def timeit(ctx, call):
started = now()
ctx = call(ctx)
ended = now() - started
ctx['duration'] = ended
print(fmt_duration(ended))
return ctx
def to_requests(ctx):
get(ctx['url'])
fancy_get = formation.wrap(to_requests, middleware=[log, timeit])
fancy_get('url':'https://google.com')
#> started
#> 200ms
#> ended