Esempio n. 1
0
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)
Esempio n. 2
0
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)