def assign_roles(url, server_description, roles): assert roles client = GraphQLClient('http://{}/admin/api'.format(url)) result = client.execute(''' mutation( $uri: String!, $roles: [String!] ) { createReplicasetResponse: join_server( uri: $uri roles: $roles ) } ''', variables={ **server_description._asdict(), 'roles': roles } ) data = json.loads(result) print(data) return ( 'data' in data and 'createReplicasetResponse' in data['data'] and 'errors' not in data, data )
def send_to_graphql(logger, studentID, roomID, token, score, classification): ENDPOINT = "http://learnlab-server.herokuapp.com/" #logger.info("sending via graphql") try: auth = "Bearer " + token client = GraphQLClient(ENDPOINT) client.inject_token(auth) query = '''mutation addEngagement($room_id:ID!, $student_id:ID!, $score:Int!, $classification:String!, $created_at:Date!) { upsertEngagementCurrent(room_id: $room_id, student_id: $student_id, score: $score, classification: $classification, created_at: $created_at) { success, message } }''' variables = { "room_id": roomID, "student_id": studentID, "score": score, "classification": classification, "created_at": datetime.today().strftime('%m/%d/%Y %H:%M:%S') } # logger.info("Executing room_id: {}, student_id: {}, score: {}, classification: {}".format(roomID, studentID, score, classification)) # logger.info("Query: ".format(query)) data = client.execute(query=query, variables=variables) # logger.info(data) except Exception as e: logger.info("ERROR: {}".format(e))
def lambda_handler(event, context): client = GraphQLClient("https://api.faros.ai/v0/graphql") client.inject_token("Bearer {}".format(event["farosToken"])) query = '''{ iam_userDetail { data { userId userName mfaDevices { data { serialNumber } } farosAccountId farosRegionId } } }''' response = client.execute(query) response_json = json.loads(response) users = response_json["data"]["iam_userDetail"]["data"] return [{ "name": u["userName"], "id": u["userId"], "farosAccountId": u["farosAccountId"], "farosRegionId": u["farosRegionId"] } for u in users if not u["mfaDevices"]["data"]]
def all_historical_job_metric(graphql_host, graphql_port, graphql_auth): # graphql connection graphql_url = 'http://' + graphql_host + ':' + str( graphql_port) + '/graphql' url = os.environ.get('GRAPHQL_URL', graphql_url) client = GraphQLClient(url) client.inject_token(os.environ.get('GRAPHQL_AUTH', graphql_auth)) output = graphql_queries.query_AllHistoricaJobs(client) df = pd.DataFrame() allJobs = json.loads(output).get('data', {}).get('allHistoricalJobs', {}) jobs = allJobs.get('HistoricalJobs', []) for j in jobs: cols = [1, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 44, 45, 58] #print(j['jobid']) df_temp = pd.DataFrame(json_normalize(j)) #df.drop(df.columns[cols],axis=1,inplace=True) df_temp['submission_time_ms'] = j['submission_time_ms'] df_temp['slots'] = j['slots'] df = df.append(df_temp, sort=True) #pd.to_datetime(df['time'].values, unit='ms', utc=True) df.set_index('submission_time_ms', append=False, inplace=True) #df.index = df.index.to_datetime() # Use the following for values like 3.5G. Will remove the 'G' at the end. # pd.to_numeric(df['m_mem_used'].str.replace('[^\d.]', ''), errors='coerce') return (df[[ 'job_number', 'owner', 'slots', 'qname', 'project', 'usage.ru_wallclock', 'usage.cpu', 'hostname' ]])
def getRaspiConfig(): client = GraphQLClient(GRAPHQL_URL) result = None config = None try: result = client.execute( """query{ ReadRasppiConfig(type: {id:9}){ id, gpsInterval } }""" ) except urllib2.URLError as err: if err.reason.strerror == 'nodename nor servname provided, or not known': config = json.loads(r.get('gpsconfig')) pass elif err.reason.message == 'timed out': config = json.loads(r.get('gpsconfig')) pass elif err.reason.errno == 51: config = json.loads(r.get('gpsconfig')) pass print(err.reason) logger.error(err.reason) if result is not None: config = json.loads(result)["data"]["ReadRasppiConfig"] if len(config) != 0: config = config[0] r.set('gpsconfig', json.dumps(config)) else: config = r.get('gpsconfig') return config
def upload_retry(request): try: client = GraphQLClient(GRAPHQL_URL) result = None result = client.execute( request ) except urllib2.URLError as err: logger.error(err.reason) print err.reason if err.reason.strerror == 'nodename nor servname provided, or not known': failed.append(request) pass elif err.reason.message == 'timed out': failed.append(request) pass elif err.reason.errno == 51: failed.append(request) pass else: failed.append(request) pass if result is not None: logger.debug(result) print(result)
def get_github_client(version): """ Create a Github client for the given Github API version using credentials provided as environment variables """ if version == 3: token = os.getenv('GITHUB_TOKEN') client_id = os.getenv('GITHUB_CLIENT_ID') client_secret = os.getenv('GITHUB_CLIENT_SECRET') if token is None or token == '': raise Exception('You need to set GITHUB_TOKEN using environment variables') if client_id is None or client_id == '': raise Exception('You need to set GITHUB_CLIENT_ID using environment variables') if client_secret is None or client_secret == '': raise Exception('You need to set GITHUB_CLIENT_SECRET using environment variables') gh = Github(login_or_token=token, client_id=client_id, client_secret=client_secret, per_page=100) #gh._Github__requester = CachingRequester(gh._Github__requester) return gh elif version == 4: github_token = os.getenv('GITHUB_TOKEN') if github_token is None or github_token == '': raise Exception('You need to set GITHUB_TOKEN using environment variables') graph = GraphQLClient('https://api.github.com/graphql') graph.inject_token(github_token) return graph else: raise ValueError('Invalid API version {}, choose between 3 and 4'.format(version))
def lambda_handler(event, context): client = GraphQLClient("https://api.faros.ai/v0/graphql") client.inject_token("Bearer {}".format(event["farosToken"])) query = '''{ lambda_functionConfiguration { data { functionArn functionName vpcConfig { subnetIds } farosAccountId farosRegionId } } }''' response = client.execute(query) response_json = json.loads(response) functions = response_json["data"]["lambda_functionConfiguration"]["data"] return [ f for f in functions if not f["vpcConfig"] or not f["vpcConfig"]["subnetIds"] ]
def lambda_handler(event, context): client = GraphQLClient("https://api.faros.ai/v0/graphql") client.inject_token("Bearer {}".format(event["farosToken"])) query = '''{ ec2_instance { data { instanceId state { name } launchTime farosAccountId farosRegionId } } }''' response = client.execute(query) response_json = json.loads(response) instances = response_json["data"]["ec2_instance"]["data"] cutoff = int(event["params"]["num_days"]) return [ i for i in instances if i["state"]["name"] == "running" and days_diff(i["launchTime"]) > cutoff ]
def client(self): if not self._client: self._client = GraphQLClient("{}/graphql".format( settings.WIKIJS_URL)) self._client.inject_token("Bearer {}".format( settings.WIKIJS_API_KEY)) return self._client
def lambda_handler(event, context): client = GraphQLClient("https://api.faros.ai/v0/graphql") client.inject_token("Bearer {}".format(event["farosToken"])) query = '''{ ec2_instance { data { instanceId tags { key } farosAccountId farosRegionId } } }''' response = client.execute(query) response_json = json.loads(response) instances = response_json["data"]["ec2_instance"]["data"] required_keys = frozenset(event["params"]["keys"].split(",")) tagless_instances = [{ "instance": i, "missingKeys": missing_tags(required_keys, frozenset([t["key"] for t in i["tags"]])) } for i in instances] tagless_instances = [i for i in tagless_instances if i["missingKeys"]] return tagless_instances
def run_simulation(config): session_id = config[SESSION_ID] app_state = get_app_state(session_id) app_state[CONFIG] = config set_status(session_id, STARTING) env = try_make_env(config[ENVIRONMENT_ID]) if (env == None): set_status(ERROR, ["Can't load environment: " + config[ENVIRONMENT_ID]]) return app_state[STATUS] app_state[ENVIRONMENT] = env agents = config[AGENTS] uri = agents[0][URI] token = agents[0][TOKEN] client = GraphQLClient(uri) client.inject_token("Bearer " + token) app_state[CLIENT] = client thread = threading.Thread(target=run_episodes, args=( session_id, 99, )) print("thread: " + repr(thread)) app_state[THREAD] = thread thread.start() return app_state[STATUS]
def cloud_accounts(): client = GraphQLClient(graphql_url) # This can be minimized result = client.execute(''' query { allCloudAccounts { edges { node { id uid name customer { id uid name } cloudProvider { id uid name abbreviation } } } } } ''') return render_template( "cloud_accounts.jade", cloud_accounts=json.loads(result)["data"]["allCloudAccounts"]["edges"])
def get_client(self): token = self.refresh_token() if token: client = GraphQLClient('{}/graphql/{}'.format(self._url, self._path)) client.inject_token(token) return client return None
def __init__(self, url, token=None, int_name=None, validate_schemas=False): self.url = url self.token = token self.integration = int_name self.validate_schemas = validate_schemas self.client = GraphQLClient(self.url) if validate_schemas and not int_name: raise Exception( "Cannot validate schemas if integration name " "is not supplied" ) if token: self.client.inject_token(token) if int_name: integrations = self.query(INTEGRATIONS_QUERY, skip_validation=True) for integration in integrations["integrations"]: if integration["name"] == int_name: self._valid_schemas = integration["schemas"] break if not self._valid_schemas: raise GqlApiIntegrationNotFound(int_name)
def run_server(network_params, oracle_id, oracle_private_key, handler): signing_key = nacl.signing.SigningKey(oracle_private_key) while True: seqno, last_known = get_oracle_data() query = query_template % {'contract_addr': network_params["hub"], 'oracle_addr': prepare_oracle_addr(oracle_id), 'last_known': last_known} client = GraphQLClient(resolve_redirect(network_params["endpoint"], '/graphql')) result = json.loads(client.execute(query)) if 'data' in result and 'messages' in result['data']: if len(result['data']['messages']): for m in result['data']['messages']: body = m['body'] created_at = m['created_at'] body = codecs.decode(codecs.encode(body, 'utf8'), 'base64') request = deserialize_boc(body) query_id, request.data.data = request.data.data[:96], request.data.data[96:] #TODO bad practice result = handler(request) result_w_header = add_header_to_response(int.from_bytes(query_id, 'big'), oracle_id, seqno, result) seqno += 1 signed_result = sign_message(signing_key, result_w_header) out_msg = compose_message(network_params["hub"],signed_result) send_boc(client, out_msg.serialize_boc(has_idx=False)) set_oracle_data(seqno, created_at) print("Processed query with id %s"%query_id.tobytes()) sleep(delay)
def __init__(self, cache, api_cache=None, **kwargs): super(USDataProvider, self).__init__(**kwargs) self._cache = cache self._geocoder = Geocoder(country='US') self._openstates = GraphQLClient('https://openstates.org/graphql') self._openstates.inject_token(os.environ.get('OPENSTATES_API_KEY'), 'x-api-key')
def get_servers(url): client = GraphQLClient('http://{}/admin/api'.format(url)) result = client.execute(''' query { serverList: servers { uuid alias uri status message replicaset { uuid } } } ''') data = json.loads(result) servers = list(map( parse_server_description, data['data']['serverList'] )) return servers
class GithubClient: def __init__(self, api_token, api_server_url='https://api.github.com/graphql'): """Client to interact with github graphql API Parameters ---------- api_token : str Github API token of user api_server_url : str Github API server url (default is 'https://api.github.com/graphql') """ self._api_token = api_token self._client = GraphQLClient(api_server_url) if api_token: self._client.inject_token('bearer ' + api_token) def get(self, query): """Make graphql query to github API Parameters ---------- query : str graphql query Returns ------- response_data : dict or list json decoded reponse of api call """ result = self._client.execute(query) data = json.loads(result) return data
def open(): access_token = "7e0118b3603a7c3a54435db7" bearer_token = 'Bearer ' + access_token client = GraphQLClient("https://wip.chat/graphql") client.inject_token(bearer_token) result = client.execute(''' query{ user(id: "73") { username products { name id } } } ''') products = json.loads(result) return render_template('open.html', data=products)
def get_interval_config(): client = GraphQLClient(GRAPHQL_URL) try: result = client.execute("""query{ReadIntervalConfig(type:{id: 2}){ id, deviceId, startMethod, startTimeOfDay, startCountdown, stopMethod, stopTimeOfDay, startCountdown, interval }} """) except urllib2.URLError as err: if err.reason.strerror == 'nodename nor servname provided, or not known': config = json.loads(r.get('config')) pass elif err.reason.message == 'timed out': config = json.loads(r.get('config')) pass elif err.reason.errno == 51: config = json.loads(r.get('config')) pass print(err.reason) if result is not None: config = json.loads(result)["data"]["ReadIntervalConfig"] if len(config) != 0: config = config[0] r.set('config', json.dumps(config)) else: config = r.get('config') return config
def __init__(self): self.HASURA_GRAPHQL_URL = os.environ['HASURA_GRAPHQL_URL'] self.HASURA_GRAPHQL_ADMIN_SECRET = os.environ['HASURA_GRAPHQL_ADMIN_SECRET'] self.graphql_client = GraphQLClient(self.HASURA_GRAPHQL_URL) self.graphql_client.inject_token( self.HASURA_GRAPHQL_ADMIN_SECRET, 'x-hasura-admin-secret' )
def get_cdps(self): client = GraphQLClient(self.graphql_url) result = client.execute(QUERY.replace('XXX', f'{self.top}')) data = json.loads(result) cdps = [] for cdp in data["data"]["allCups"]["nodes"]: cdps.append(cdp["id"]) return cdps
def __init__(self, url, token=None): self.url = url self.token = token self.client = GraphQLClient(self.url) if token: self.client.inject_token(token)
def __init__(self, bot): self.bot = bot self.playerLists = dict() self.splitter = "," self.client = GraphQLClient(GigiCog.url) self.client.inject_token("Bearer" + GigiCog.API_Key) self.load_serial_list() self.auto_queue.start()
def get_client(client_url=None,api_key=None,cfig=None): if not (client_url and api_key): if not cfig: cfig=config() client_url=cfig['client'] api_key=cfig['api_key'] client=GraphQLClient(client_url) client.inject_token(f'Bearer {api_key}') return client
class GqlApi(object): def __init__(self, url, token=None): self.url = url self.token = token self.client = GraphQLClient(self.url) if token: self.client.inject_token(token) def query(self, query, variables=None): try: # supress print on HTTP error # https://github.com/prisma-labs/python-graphql-client # /blob/master/graphqlclient/client.py#L32-L33 with open(os.devnull, 'w') as f, contextlib.redirect_stdout(f): result_json = self.client.execute(query, variables) except Exception as e: raise GqlApiError( 'Could not connect to GraphQL server ({})'.format(e)) result = json.loads(result_json) if 'errors' in result: raise GqlApiError(result['errors']) if 'data' not in result: raise GqlApiError(( "`data` field missing from GraphQL" "server response.")) return result['data'] def get_resource(self, path): query = """ query Resource($path: String) { resources: resources_v1 (path: $path) { path content sha256sum } } """ try: resources = self.query(query, {'path': path})['resources'] except GqlApiError: raise GqlGetResourceError( path, 'Resource not found.') if len(resources) != 1: raise GqlGetResourceError( path, 'Expecting one and only one resource.') return resources[0]
def __init__(self, furniture_height, config): self.furniture_height = furniture_height self.graphql_endpoint = config["API"]["host"] self.geometry_endpoint = config["Geometry"]["host"] self.graphql_client = GraphQLClient(self.graphql_endpoint) #self.kafka_consumer = self._setup_kafka_consumer(config["Kafka"]["host"]) self.kafka_producer = self._setup_kafka_producer( config["Kafka"]["host"]) self.canvas = go.Figure()
class GithubClient: def __init__(self, api_token): self.api_token = api_token self.client = GraphQLClient('https://api.github.com/graphql') self.client.inject_token('bearer ' + api_token) def get(self, query): result = self.client.execute(query) data = json.loads(result) return data
class HmrcReleaseSearch: def __init__(self): self.client = GraphQLClient('https://api.github.com/graphql') oauth_token = os.environ.get('GITOAUTH') # type: Optional[str] if not oauth_token: print("environment variable $GITOAUTH is not defined") exit(1) if not oauth_token.startswith("Bearer"): print( "environment variable $GITOAUTH does not start with 'Bearer'") exit(1) self.client.inject_token(oauth_token) # OauthToken 'bearer {token}' def graph_ql_search(self, lib_name): return self.client.execute(''' query { repository(owner:"hmrc", name:"''' + lib_name + '''") { releases(first: 100, orderBy: {direction: DESC, field: CREATED_AT}) { nodes { name } } } } ''') def fetch_release(self, sbt_version, lib): domain = lib.group(1) library_name = lib.group(2) current_version = lib.group(3) if domain == "uk.gov.hmrc": version_number = current_version.replace("-play-25", "").replace( "-play-26", "") for release in json.loads(self.graph_ql_search( library_name))["data"]["repository"]["releases"]["nodes"]: release_name = str(release["name"]) test = version_number release_number = str( release_name.replace("-play-25", "").replace( "-play-26", "").replace(",", "").split(" ")[0]) if test.split(".")[0] == release_number.split(".")[0]: if "-play-25" in release_name: if "2.5" in sbt_version: return str(release_number + "-play-25") else: return str(release_number + "-play-26") else: return str(release_number)