def demo(key): client = HTTPClient() try: logging.info("Requesting a trigger list") trigger_list = yield client.list_triggers(api_key=key) if trigger_list: logging.info("Success retrieving a trigger list:\n%s\n" % trigger_list) else: logging.error("Problem occurred listing triggers") except Exception, ex: logging.error("Error: %s" % str(ex))
def demo(key, feed_id=None, datastream_id=None, timestamp=None): client = HTTPClient() if feed_id and datastream_id: if timestamp: # read the datapoint at the specified timestamp try: logging.info("Reading the datapoint at %s" % timestamp) datapoint = yield client.read_datapoint(api_key=key, feed_id=feed_id, datastream_id=datastream_id, timestamp=timestamp) if datapoint: logging.info("Success reading the datapoint:\n%s\n" % datapoint) else: logging.error("Problem occurred reading the datapoint") except Exception, ex: logging.error("Problem reading the datapoint") logging.exception(ex) else: # no specific datapoint was requested so just show the last 10 minutes worth # create historical query parameters spanning the last 10 minutes ten_minutes_ago_timestamp = datetime.datetime.utcnow() - datetime.timedelta(minutes=10) start_timestamp = "%sZ" % (ten_minutes_ago_timestamp.isoformat()) parameters = {'start': start_timestamp, 'interval': 0} try: logging.info("Requesting to view the last 10 minutes of historical datapoints for datastream %s in feed %s starting from %s" % (datastream_id, feed_id, start_timestamp)) datastream = yield client.read_datastream(api_key=key, feed_id=feed_id, datastream_id=datastream_id, parameters=parameters) if datastream: logging.info("Success retrieving datastream historical datapoints:\n%s\n" % datastream) else: logging.error("Problem reading datastream") except Exception, ex: logging.error("Error reading datastream") logging.exception(ex)
def demo(user_api_key): """ Perform the txcosm.client.Client tests """ #user_api_key_id = None #user_api_key_found = False required_permissions = [u'get', u'put', u'post', u'delete'] #required_permissions_available = True client = HTTPClient() ################################################################################ # Api Keys ################################################################################ # check that supplied key has appropriate permissions for the test. try: logging.info("Requesting details of the supplied API key") apikey = yield client.read_api_key(api_key=user_api_key, key_id=user_api_key) logging.info("Received API key:\n%s\n" % apikey) # Multiple access_methods declarations can exist within a permissions dict. # Create a consolidated list that can be checked once. consolidated_access_methods = [] for permission in apikey.permissions: for method in permission.access_methods: if method not in consolidated_access_methods: consolidated_access_methods.append(method) for required_permission in required_permissions: if required_permission not in consolidated_access_methods: logging.error("The supplied key does not have the %s permission which is required" % required_permission) defer.returnValue(False) logging.info("The supplied API key supports the required permissions for this test") except Exception, ex: logging.error("Error reading key details: %s" % str(ex)) defer.returnValue(False)
def demo(key, key_id): client = HTTPClient() try: if key_id: # request key details for the supplied key only logging.info("Requesting key details for key: %s" % key_id) dataStructure = yield client.read_api_key(api_key=key, key_id=key_id) else: # request key details for all keys visible to the key supplied logging.info("Requesting a key listing") dataStructure = yield client.list_api_keys(api_key=key) logging.info("Received response from Cosm:\n%s\n" % dataStructure) reactor.callLater(0.1, reactor.stop) defer.returnValue(True) except Exception, ex: logging.exception(ex) reactor.callLater(0.1, reactor.stop) defer.returnValue(False)
def demo(key, feed_id): client = HTTPClient() try: if feed_id: # request feed details for the supplied identifier only logging.info("Requesting feed details for feed: %s" % feed_id) dataStructure = yield client.read_feed(api_key=key, feed_id=feed_id) else: # request feed details for all feeds visible to the key supplied logging.info("Requesting a feed listing") dataStructure = yield client.list_feeds(api_key=key, parameters={'per_page': 5, 'status': 'live'}) logging.info("Received response from Cosm:\n%s\n" % dataStructure) reactor.callLater(0.1, reactor.stop) defer.returnValue(True) except Exception, ex: logging.exception(ex) reactor.callLater(0.1, reactor.stop) defer.returnValue(False)
def demo(key): client = HTTPClient() # set a REALLY low value to force timeout functionality client.request_timeout = 0.2 try: # request key details for all keys visible to the key supplied logging.info("Requesting a key listing") dataStructure = yield client.list_api_keys(api_key=key) if dataStructure: logging.info("Received response from Cosm:\n%s\n" % dataStructure) else: logging.error("Unable to retrieve key list") reactor.callLater(0.1, reactor.stop) defer.returnValue(True) except Exception, ex: logging.exception(ex) reactor.callLater(0.1, reactor.stop) defer.returnValue(False)