def get_cloud_name_from_obj(obj_type, name=None, uuid=None, **kwargs): status_code, resp = get(obj_type, name=name, uuid=uuid, **kwargs) cloud_ref = resp.get('cloud_ref', None) if cloud_ref: return get_name_from_ref(cloud_ref, **kwargs) return None
def wait_until_n_cluster_nodes_ready_inner(): rsp = None try: st, rsp = get('cluster/runtime') except Exception as ex: fail('Cluster api runtime exception: %s' % ex) if rsp and st == 200: node_states = rsp.get('node_states', []) cluster_state = rsp.get('cluster_state', {}) cl_state = cluster_state.get('state', 'unknown') up_nodes = 0 for node in node_states: if node.get('state') == 'CLUSTER_ACTIVE': up_nodes += 1 if (up_nodes != n): logger.debug('Cluster (status:%s) expects %d active nodes ' 'but contains %d active nodes' % (cl_state, n, up_nodes)) elif (n == 1 and cl_state == 'CLUSTER_UP_NO_HA'): logger.info('Cluster is ready! Cluster state is %s' % cluster_state) return elif (n == 2 and cl_state == 'CLUSTER_UP_HA_COMPROMISED'): logger.info('Cluster is ready! Cluster state is %s' % cluster_state) return elif (n == 3 and cl_state == 'CLUSTER_UP_HA_ACTIVE'): logger.info('Cluster is ready! Cluster state is %s' % cluster_state) return fail('Cluster runtime response not as expected %s' % (rsp if rsp else 'None'))
def get_similar_tracks(mbid=None, name=None, artist=None, pages=1): # TODO: explain in documentation - pages (total #) vs page query_d = { "method": "track.getsimilar", "api_key": API_KEY, "format": "json", "limit": 10 * pages } if mbid is not None: query_d["mbid"] = mbid elif name is not None and artist is not None: query_d["track"] = name query_d["artist"] = artist # User-agent header should be default res = rest.get(API_ROOT, query_d) if res["type"] == "HTTPError" or res["type"] == "URLError": # returning (status, result); error already sent to console return ("Error", []) res_dict = json.loads(res["object"].read()) if "error" in res_dict: print build_API_error(res_dict, "get_similar_tracks", mbid=mbid, name=name) # returning (status, result); error already sent to console return ("Error", []) return ("OK", build_similar_tracks_list(res_dict["similartracks"]["track"]))
def get_artist_top_albums(mbid=None, name=None, pages=1): # pages actually functions as a limit multiplier, so pages=2 -> 20 results query_d = { "method": "artist.gettopalbums", "api_key": API_KEY, "format": "json", "limit": 10, "page": pages } if mbid is not None: query_d["mbid"] = mbid elif name is not None: query_d["artist"] = name # User-agent header should be default res = rest.get(API_ROOT, query_d) if res["type"] == "HTTPError" or res["type"] == "URLError": # returning (status, result); error already sent to console return ("Error", []) res_dict = json.loads(res["object"].read()) if "error" in res_dict: print build_API_error(res_dict, "get_artist_top_albums", mbid=mbid, name=name) # returning (status, result); error already sent to console return ("Error", []) return ("OK", build_artist_top_item_list(res_dict["topalbums"]["album"]))
def check_cloud_state(expected_status='CLOUD_STATE_PLACEMENT_READY', **kwargs): cloud_name = kwargs.get('cloud_name', None) # config = get_config() # ctrl_clouds = kwargs.get('clouds', config.testbed[config.site_name].cloud) asleep(msg='waiting for cloud state', delay=10) status_code, resp_json = get('cloud-inventory') #resp_json = resp.json() #if len(ctrl_clouds) != int(resp_json['count']): # error("Number of Configured Clouds not as Received. Configured=%s Received=%s" %(len(ctrl_clouds), resp_json['count'])) for cloud_obj in resp_json['results']: if cloud_name and cloud_name != cloud_obj['config']['name']: continue if 'error' in cloud_obj['status']: error('Received Error in cloud status %s' % cloud_obj['status']['error']) cloud_status = cloud_obj['status']['state'] last_reason = cloud_obj['status'].get('reason', '') # To handle special? case where cloud is up but about to be reconfigured # REVIEW any other reasons that we need to account for? if cloud_status != expected_status or 'Pending re-config' in last_reason: if cloud_obj['config']['vtype'] == 'CLOUD_AWS': asleep("additional delay for AWS cloud", delay=30) error( 'Cloud Status is not as expected or reason not null. Expected=%s Received=%s, reason = %s', expected_status, cloud_status, last_reason) return True
def get(self): criteria = { 'symbol': self.symbol, 'exp_date': self.exp_date } return rest.get(TABLE_NAME, criteria)
def run(self, exchange, symbol): self.load_job(exchange, symbol) self.limiter() trd, self.job = rest.get(self.job) if self.job['auto_since'] == 'yes' \ and len(trd.index) > 0: self.auto_since(trd) self.job['stmt'] = rest.build_stmt(self.job) return trd
def get(self): criteria = { "symbol": self.symbol, "exp_date": self.exp_date, "strike": self.strike, "call_or_put": self.call_or_put, } return rest.get(TABLE_NAME, criteria)
def wait_for_vcenter_state(cloud_name, exp_states): status_code, data = get('vimgrvcenterruntime') logger.debug('wait_for_vcenter_state data: %s' % data) if data['count'] == 0: error('Inventory is not complete') state = data['results'][0]['inventory_state'] if state in exp_states: return True else: error('Check for state %s one more time got %s' % (exp_states, state))
def get_node_config_retries(**kwargs): config = AviConfig.get_instance() mode = config.get_mode() rsp = None try: st, rsp = get('cluster', check_status_code=False) except Exception as ex: fail("get_node_config: sdk-exception %s" % str(ex)) logger.info('get_node_config: Got cluster nodes %s' % str(rsp)) if re.search('Authentication credentials were not provided.', str(rsp)): fail('Controller %s is not running basic auth!', force=True) if re.search('Invalid username', str(rsp)): logger.info( 'get_node_config: invalid username/password admin/%s.Try admin/admin' % config.password) config.switch_mode(user='******', password='******') st, rsp = get('cluster', check_status_code=False) if st != 200: fail("Non 200 status code received %s %s" % (st, rsp)) config.switch_mode(**mode) return rsp['nodes']
def run(self, exchange, symbol, type): #|Load job and run "limiter" function to pause in order not to exceed jobs per second rate self.load_job(exchange, symbol, type) self.limiter() #|Send GET API request and return DataFrame and updated 'job' dictionary df, self.job = rest.get(self.job) #|Run trade history jobs if self.job['type'] == 'trades': if self.job['auto_since'] == 'yes' \ and len(df.index) > 0: self.auto_since(df) self.job['stmt'] = rest.build_stmt(self.job) return df
def getResponse(self, reqURL): ''' method calling REST GET request :param reqURL: REST resource url :type reqURL: string :return: dictionary containing returnCode and buffer :rtype: dictionary ''' if self.verbose: print 'Req URL: ', reqURL res = rest.get(self.switch_ip, reqURL, self.verbose) if isinstance(res["response"], urllib2.URLError): return (555, res.reason) else: try: return (res["response"].status, res["data"]) except: return (res["response"].status, res["response"].reason)
def search(q_type, name, artist=None): """ Get 1st search result for album, artist, or track. Args: q_type (str): item type - can be 'album', 'artist', or 'track' name (str): name of the item artist (str, optional): item's artist. REQUIRED IF q_type IS NOT ARTIST Returns: tuple: first element is a string status ("Error" or "OK"). Second element is a dictionary containing the name, type, and URI of the item, in addition to the artist (if the item isn't an artist). The keys to the fields are "name", "type", "uri", and "artist". """ url = API_ROOT + "/search" # auth_hdr = {"Authorization": "Bearer " + ACCESS_TOKEN} query_d = {"type": q_type, "limit": 1} # Removed "market": "from_token" for testing if q_type == "artist": q_str = 'artist:"' + name + '"' elif q_type == "album": if artist is None: print "ERROR: spotify.search() not given artist for album search" return ("Error", {}) q_str = 'album:"' + name + '" artist:"' + artist + '"' elif q_type == "track": if artist is None: print "ERROR: spotify.search() not given artist for track search" return ("Error", {}) q_str = 'track:"' + name + '" artist:"' + artist + '"' else: print "ERROR: spotify.search() not given proper q_type" return ("Error", {}) query_d["q"] = q_str # resp = rest.get(url, query_d, auth_hdr) resp = rest.get(url, query_d) if resp["type"] == "HTTPError" or resp["type"] == "URLError": # returning (status, result); error already sent to console return ("Error", {}) resp_dict = json.loads(resp["object"].read()) return ("OK", build_result_dict(q_type, resp_dict[q_type + "s"]["items"]))
def getAlbumInfo(mbid=None, name=None, artist=None): query_d = {"method": "album.getinfo", "api_key": API_KEY, "format": "json"} if mbid is not None: query_d["mbid"] = mbid elif name is not None and artist is not None: query_d["album"] = name query_d["artist"] = artist # User-agent header should be default res = rest.get(API_ROOT, query_d) if res["type"] == "HTTPError" or res["type"] == "URLError": # returning (status, result); error already sent to console return ("Error", {}) res_dict = json.loads(res["object"].read()) if "error" in res_dict: print build_API_error(res_dict, "getAlbumInfo", mbid=mbid, name=name) # returning (status, result); error already sent to console return ("Error", {}) if res_dict["album"]["name"] == "None": # Aka API returned default NONE album return ("OK", {}) return ("OK", build_album_info_dict(res_dict["album"]))
def getSongInfo(mbid=None, name=None, artist=None): query_d = {"method": "track.getinfo", "api_key": API_KEY, "format": "json"} if mbid is not None: query_d["mbid"] = mbid elif name is not None and artist is not None: query_d["track"] = name query_d["artist"] = artist # User-agent header should be default res = rest.get(API_ROOT, query_d) if res["type"] == "HTTPError" or res["type"] == "URLError": # returning (status, result); error already sent to console return ("Error", {}) res_dict = json.loads(res["object"].read()) if "error" in res_dict: print build_API_error(res_dict, "getSongInfo", mbid=mbid, name=name) # returning (status, result); error already sent to console return ("Error", {}) if res_dict["track"]["name"] == "None": return ("OK", {}) song_info_dict = build_song_info_dict(res_dict["track"]) similar_list = get_similar_tracks(mbid, name, artist) if similar_list[0] == "OK" and len(similar_list[1]) > 0: song_info_dict["similar"] = similar_list return ("OK", song_info_dict)
def getArtistInfo(mbid=None, name=None): # TODO: Add getTopTags, getTopAlbums, and getSimilar query_d = { "method": "artist.getinfo", "api_key": API_KEY, "format": "json" } if mbid is not None: query_d["mbid"] = mbid elif name is not None: query_d["artist"] = name # User-agent header should be default res = rest.get(API_ROOT, query_d) if res["type"] == "HTTPError" or res["type"] == "URLError": # returning (status, result); error already sent to console return ("Error", {}) res_dict = json.loads(res["object"].read()) if "error" in res_dict: print build_API_error(res_dict, "getArtistInfo", mbid=mbid, name=name) # returning (status, result); error already sent to console return ("Error", {}) if res_dict["artist"]["name"] == "None": return ("OK", {}) return ("OK", build_artist_info_dict(res_dict["artist"]))
# load data from local json file with open('json/apilist.json') as f: json_data = json.load(f) # print(apilist) # print(apilist["urls"][0]["name"]) # Pretty Printing JSON string back # print(json.dumps(apilist, indent = 4, sort_keys=True)) # loop json data for myapilist in json_data: # print(myapilist) # for each api for api in json_data[myapilist]: # get api url url = api["url"] # for each of the verbs for verbs in api["verbs"]: # get the parameters if any name = verbs["name"] verbs = verbs["params"] print(name, verbs) # test if params null if verbs is None: print('verbs is None') rest.get(url)
def get_se_vm(se_name=None, se_uuid=None, state=None, **kwargs): """ Helps to the SE VM Object """ mgmt_addr_type = kwargs.get('mgmt_addr_type', 'V4') if se_name or se_uuid: status_code, resp = get('serviceengine', name=se_name, uuid=se_uuid) resp = [resp] else: status_code, resp = get('serviceengine') if not resp: logger.debug('No SEs found') return [] resp = resp['results'] vms = [] for each_se in resp: stc, res = get('serviceengine', uuid=each_se['uuid'], path='runtime') if state and state != res["oper_status"]["state"]: logger.debug("SE found but not in expected state") continue se_kwargs = {} #Get IP ip = "" if 'vnic_networks' not in each_se['mgmt_vnic']: name = each_se['name'] oper_state = res['oper_status']['state'] logger.warning( 'Cannot retrieve mgmt details for SE name=%s oper_state=%s', name, oper_state) continue for vnic in each_se['mgmt_vnic']['vnic_networks']: if vnic['mode'] in ['DHCP', "STATIC"]: if mgmt_addr_type == vnic['ip']['ip_addr']['type']: ip = vnic['ip']['ip_addr']['addr'] if not ip: fail("Could not retrieve Management IP of Se") name = each_se['name'] #Get Deployment if each_se['container_mode']: deployment = 'docker' se_kwargs['user'] = '******' else: deployment = 'vm' #Get Platform cloud_ref = each_se['cloud_ref'] url_parsed = urlparse(cloud_ref) cloud_uuid = url_parsed.path.split('/')[-1] status_code, cloud_details = get('cloud', uuid=cloud_uuid) vtype = cloud_details['vtype'] if vtype == 'CLOUD_VCENTER': platform = 'vcenter' elif vtype == 'CLOUD_OPENSTACK': platform = 'openstack' elif vtype == 'CLOUD_AWS': platform = 'aws' elif vtype == 'CLOUD_MESOS': platform = 'mesos' elif vtype == 'CLOUD_LINUXSERVER': if 'ipam_provider_ref' in cloud_details: ipam_ref = cloud_details['ipam_provider_ref'] url_parsed = urlparse(ipam_ref) ipam_uuid = url_parsed.path.split('/')[-1] status_code, ipam_details = get('ipamdnsproviderprofile', uuid=ipam_uuid) if ipam_details['type'] == 'IPAMDNS_TYPE_AZURE': platform = 'azure' elif ipam_details['type'] == 'IPAMDNS_TYPE_GCP': platform = 'gcp' else: platform = 'baremetal' else: platform = None vms.append( Se(ip=ip, name=name, deployment=deployment, platform=platform, **se_kwargs)) return vms
def delete_all_sensors(api): sensors = get(api + "/sensors") for k, v in ((k, v) for k, v in sensors.iteritems() if v["manufacturername"] == "ErikGroeneveld"): print "Deleting sensor", k delete(api + "/sensors/%s" % k)
def get_rules(API): return get(LOCAL_HUE_API + "/rules")
def get(url, pk, *args, **kwargs): return json.loads(rest.get(url, klass, pk))
def info(self): return get(self.url())
def albumSearch(name, page=1): """Search for an album by album name. Employs the lastFm album.search endpoint to provide a formatted list of albums related to the query, ordered by relevance. Returns a complex object. Args: name (str): name of the album page (int, optional): page of results to be retrieved, in increments of 30 results. Defaults to 1. Returns: tuple: a complex object, whose first element is a status set to 'Error' or 'OK', depending on the success of the operation. The second element of the tuple is a list of dictionaries, where each dictionary corresponds to one result. If the list is empty, no results were found for the given query. Each dictionary contains the 'name' and 'mbid' properties. If the result included images, an image url will be given as 'image', and the image size will be provided as 'image_size'. Images can be of size 'medium', 'large', or 'small', in that order of preference. If no image was found, the 'image' key will not be present in the result dict. If an error occurs during the request, the status (first) element of the returned tuple will be "Error", and a detailed error message will be logged in the console. The returned object structure is as follows:: ("<OK | ERROR>", [ { "name":"<ALBUM NAME>", "mbid":"<MBID>", "image":"<IMAGE URL>", "image_size":"<IMAGE SIZE>" }, { "name":"<ALBUM NAME>", "mbid":"<MBID>" } ] ) """ query_d = { "method": "album.search", "album": name, "api_key": API_KEY, "format": "json", "page": page, "limit": 30 } # User-agent header should be default res = rest.get(API_ROOT, query_d) if res["type"] == "HTTPError" or res["type"] == "URLError": # returning (status, result); error already sent to console return ("Error", []) res_dict = json.loads(res["object"].read()) if "error" in res_dict: print build_API_error(res_dict, "albumSearch", name=name) # returning (status, result); error already sent to console return ("Error", []) results_list = [] for album in res_dict["results"]["albummatches"]["album"]: if album["mbid"] != "": results_list.append(build_track_album_dict(album)) return ("OK", results_list)
def getChronometerByName(self, name): "This gets a Chronometer by name and returns a json" url = self.restApiUrl + '/name?name=' + name response = get(url) return response
import rest ## test creating and deleting a service print rest.delete('localhost', '/myservice', 8080) print rest.get('localhost', '/myservice', 8080) print rest.put('localhost', '/myservice', 8080) print rest.get('localhost', '/myservice', 8080) print rest.delete('localhost', '/myservice', 8080)
import rest ## test creating and deleting a service print rest.delete('localhost','/myservice', 8080) print rest.get('localhost','/myservice', 8080) print rest.put('localhost', '/myservice', 8080) print rest.get('localhost','/myservice', 8080) print rest.delete('localhost','/myservice', 8080)
def read(self): return get(self.url())
def configure_cluster_vip(ipaddr): st, rsp = get('cluster') virtual_ip = {"addr": ipaddr, "type": 1} rsp['virtual_ip'] = {"addr": ipaddr, "type": 1} st, rsp = put('cluster', data=json.dumps(rsp))
def getBeaconByNumber(self, number): "This gets a Beacon by number and returns a json" url = self.restApiUrl + '/number/' + str(number) response = get(url) return response
def remove_cluster_vip(): st, rsp = get('cluster') rsp.pop('virtual_ip') st, rsp = put('cluster', data=json.dumps(rsp))