print("Running " + str(q["name"]) + " on " + str(endpoint["url"]) + ": [", end="") start = time.time() for v_id in q["values"][endpoint["name"]]: v_q = q["query"].replace("$$id$$", str(v_id)) for i in range(n_runs): start_q = time.time() results.append({ "json": client.execute(v_q), "query": q["name"], "dataset": endpoint["name"], "v_id": v_id }) delta_q = time.time() - start_q csv += str(endpoint["name"]) + "," + str( q["name"]) + "," + str(v_id) + "," + str(delta_q) + "\n" print(".", end="", flush=True) print("|", end="", flush=True) delta = time.time() - start
class GqlApi(object): _valid_schemas = None _queried_schemas = set() 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 query(self, query, variables=None, skip_validation=False): 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) # show schemas if log level is debug query_schemas = result.get('extensions', {}).get('schemas', []) self._queried_schemas.update(query_schemas) for s in query_schemas: logging.debug(['schema', s]) if self.validate_schemas and not skip_validation: forbidden_schemas = [ schema for schema in query_schemas if schema not in self._valid_schemas ] if forbidden_schemas: raise GqlApiErrorForbiddenSchema(forbidden_schemas) 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 as e: if '409' in str(e): raise e raise GqlGetResourceError(path, 'Resource not found.') if len(resources) != 1: raise GqlGetResourceError(path, 'Expecting one and only one resource.') return resources[0] def get_queried_schemas(self): return list(self._queried_schemas)
'https://api.thegraph.com/subgraphs/name/graphprotocol/uniswap') #DAI/WETH pair result = client1.execute(''' { pairDayDatas(first:1000, orderBy: date, orderDirection: desc, where: { pairAddress: "0x6b175474e89094c44da98b954eedeac495271d0f", } ) { id date token0{ symbol } token1{ symbol } dailyVolumeToken0 dailyVolumeToken1 dailyVolumeUSD dailyTxns reserve0 reserve1 reserveUSD } } ''') print(result) l = [(pair['id'], pair['date'], pair['token0']['symbol'],