コード例 #1
0
def images(filter_term=None, refresh=False):
    """Calls the API and returns a list of images."""
    cached_image_list = cfg.get_conf(cfg.IMAGE_LIST_KEY)

    if refresh or not cached_image_list:
        response = call_api(IMAGES_ENDPOINT, ())
        all_images = [[i['id'], i['name']] for i in response]

        # Caching response for subsequent queries
        cfg.set_conf(cfg.IMAGE_LIST_KEY, json.dumps(all_images), write=True)
    else:
        all_images = json.loads(cached_image_list)

    if filter_term:
        filtered_images = [
            image for image in all_images
            if filter_term.lower() in image[1].lower()
        ]

        if not filtered_images:
            raise ValueError('No matches found for that filter.')

        return filtered_images

    return all_images
コード例 #2
0
ファイル: api.py プロジェクト: rhefner1/vcl-client
def auth_check():
    """Checks if user/pass exist."""
    username = cfg.get_conf(cfg.USERNAME_KEY)
    password = cfg.get_password()

    if not username or not password:
        click.echo('Credentials not found. Run `vcl config`.')
        sys.exit(1)
コード例 #3
0
def auth_check():
    """Checks if user/pass exist."""
    username = cfg.get_conf(cfg.USERNAME_KEY)
    password = cfg.get_password()

    if not username or not password:
        click.echo('Credentials not found. Run `vcl config`.')
        sys.exit(1)
コード例 #4
0
ファイル: api.py プロジェクト: rhefner1/vcl-client
def call_api(endpoint, params):
    """Calls the API."""
    auth_check()

    base_url = cfg.get_conf(cfg.ENDPOINT_KEY)
    username = cfg.get_conf(cfg.USERNAME_KEY)
    data = xmlrpclib.dumps(params, endpoint)
    headers = {
        'Content-Type': 'text/xml',
        'X-User': username,
        'X-Pass': cfg.get_password(),
        'X-APIVERSION': '2'
    }

    req = urllib2.Request(base_url, data, headers)
    response = urllib2.urlopen(req)

    raw_xml = response.read()
    return xmlrpclib.loads(raw_xml)[0][0]
コード例 #5
0
def call_api(endpoint, params):
    """Calls the API."""
    auth_check()

    base_url = cfg.get_conf(cfg.ENDPOINT_KEY)
    username = cfg.get_conf(cfg.USERNAME_KEY)
    data = xmlrpclib.dumps(params, endpoint)
    headers = {
        'Content-Type': 'text/xml',
        'X-User': username,
        'X-Pass': cfg.get_password(),
        'X-APIVERSION': '2'
    }

    req = urllib2.Request(base_url, data, headers)
    response = urllib2.urlopen(req)

    raw_xml = response.read()
    return xmlrpclib.loads(raw_xml)[0][0]
コード例 #6
0
ファイル: api.py プロジェクト: rhefner1/vcl-client
def images(filter_term=None, refresh=False):
    """Calls the API and returns a list of images."""
    cached_image_list = cfg.get_conf(cfg.IMAGE_LIST_KEY)

    if refresh or not cached_image_list:
        response = call_api(IMAGES_ENDPOINT, ())
        all_images = [[i['id'], i['name']] for i in response]

        # Caching response for subsequent queries
        cfg.set_conf(cfg.IMAGE_LIST_KEY, json.dumps(all_images), write=True)
    else:
        all_images = json.loads(cached_image_list)

    if filter_term:
        filtered_images = [image for image in all_images
                           if filter_term.lower() in image[1].lower()]

        if not filtered_images:
            raise ValueError('No matches found for that filter.')

        return filtered_images

    return all_images