def extract_request(): """ A context manager that helps extracting the Request object from a function decorated with the apimethod decorator. Usage is: with extract_request(): request = self.decorated_method() It works by temporarily substituting the executor with a dummy one that just returns the request unchanged. """ prev = base.current_executor() try: base.use_executor(lambda request, _: request) yield finally: base.use_executor(prev)
def change_parser(parser): """ A context manager that allows overriding the function that will be used to parse the response. Usage is with change_parser(new_parser): result = service.resource().method() It works by temporarily substituting the executor with one that replaces the provided parser function with the one the context manager received. """ prev = base.current_executor() try: base.use_executor(lambda request, _: prev(request, parser)) yield finally: base.use_executor(prev)
def use(extra_handlers=()): base.use_executor(Urllib2Executor(extra_handlers=extra_handlers))
#!/usr/bin/env python import argparse import json import urllib from pprint import pprint from twisted.internet import reactor from libsaas.executors import base, twisted_executor base.use_executor(twisted_executor.TwistedExecutor(None, True)) from libsaas.services import ducksboard, twilio from autobahn.websocket import (connectWS, WebSocketClientFactory, WebSocketClientProtocol) TWIML_URL = 'http://aitorciki.net:8080/?message={0}' def parse_arguments(): description = ('Uh... kind of notifies stuff through Twilio ' 'based on Ducksboard updates') parser = argparse.ArgumentParser(description=description) parser.add_argument('-a', '--api_key', required=True, help='your Ducksboard API key, please') parser.add_argument('-w', '--widget_id', required=True, help='the data source to track') parser.add_argument('-s', '--sid', required=True, help='Twilio SID')