def graphql_connection(): def check_env(variables: List[str]): for variable in variables: if os.getenv(variable) is None: raise EnvironmentError(f"Variable {variable} isn't set.") check_env(["ENDPOINT", "TOKEN"]) url = os.getenv("ENDPOINT") token = os.getenv("TOKEN") headers = {"Authorization": f"bearer {token}"} transport = AIOHTTPTransport(url=url, headers=headers) return Client(transport=transport, fetch_schema_from_transport=True)
async def query_data(url, gql_query): transport = AIOHTTPTransport(url=url) async with Client( transport=transport, fetch_schema_from_transport=True, ) as session: # Execute single query query = gql(gql_query) result = await session.execute(query) return result
async def query_http(query, variable_values=None, with_fragments=True, execute_timeout=10): transport = AIOHTTPTransport( url="http://localhost:4000/", headers={"authorization": f"Bearer {GQLService.make_token()}"}) client = Client(transport=transport, fetch_schema_from_transport=True, execute_timeout=execute_timeout) return await client.execute_async(GQLService.make_query( query, with_fragments=with_fragments), variable_values=variable_values)
async def plot(request): try: async with aiohttp.ClientSession() as session: req_param = request.rel_url.query['symbol'].split( ',') if request.rel_url.query else [ "1INCHUSDT", "1INCHDOWNUSDT", "1INCHBUSD", "1INCHUSDT" ] req = await request.json() if request.body_exists else { "symbol": req_param } print("*************", req["symbol"]) transport = AIOHTTPTransport( url='https://dbschool.alcyone.life/graphql') client = Client(transport=transport, fetch_schema_from_transport=True) listofdf = [] for symbol in req["symbol"]: query = gql("""query { tickers(where: { symbol_contains: "%s" }) { price created_at } }""" % symbol) result = await client.execute_async(query) histprices = result['tickers'] histpricesdf = pd.DataFrame.from_dict(histprices) histpricesdf = histpricesdf.rename({'price': symbol}, axis=1) listofdf.append(histpricesdf) dfs = [df.set_index('created_at') for df in listofdf] histpriceconcat = pd.concat(dfs, axis=1) for i, col in enumerate(histpriceconcat.columns): histpriceconcat[col].plot() plt.title('Price Evolution Comparison') plt.xticks(rotation=70) plt.legend(histpriceconcat.columns) filename = '.'.join(['-'.join(req["symbol"]), 'png']) file_path = f"./tick/plots/{filename}" plt.savefig(file_path, bbox_inches='tight') return aiohttp.web.FileResponse(f'./{file_path}') # return aiohttp.web.json_response(dict(json=result)) except Exception as e: print(e) raise e
def uniswap_query_cycle_by_generic_number_field( event_name, field, generic_number_start, custom_where_field="") -> object: _transport = AIOHTTPTransport( url="https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2") _client = Client(transport=_transport, fetch_schema_from_transport=True) first = 1000 skip = 0 generic_number = generic_number_start query = query_constructor_generic_number(first, skip, event_name, generic_number, field, custom_where_field) while True: try: call_data = _client.execute(query) except: continue break result_array = call_data[event_name] latest_generic_number = find_latest_generic_result(result_array, field) generic_number = latest_generic_number + 1 query = query_constructor_generic_number(first, skip, event_name, generic_number, field, custom_where_field) while len(call_data[event_name]) > 0: try: while True: try: call_data = _client.execute(query) except: continue break for event in call_data[event_name]: # print(event) result_array.append(event) latest_generic_number = find_latest_generic_result( call_data[event_name], field) generic_number = latest_generic_number + 1 # print(generic_number) query = query_constructor_generic_number(first, skip, event_name, generic_number, field, custom_where_field) except Exception as e: time.sleep(1) print(e) return result_array
async def getLanplayStatus(url: str) -> Dict: """ { room { hostPlayerName\n nodeCountMax\n nodeCount\n nodes { playerName }\n contentId }\n serverInfo { online\n idle } } """ transport = AIOHTTPTransport(url=url) # Using `async with` on the client will start a connection on the transport # and provide a `session` variable to execute queries on this connection async with Client( transport=transport, fetch_schema_from_transport=True, ) as session: # Execute single query query = gql(""" query getUsers { room { hostPlayerName nodeCountMax nodeCount nodes { playerName } contentId } serverInfo { online idle } } """) result = await session.execute(query) return result
def query_ghad(gh_token: str) -> dict[str, list[Vulnerability]]: """Queries the GitHub Advisory Database for vulnerabilities reported for Node's dependencies. The database supports querying by package name in the NPM ecosystem, so we only send queries for the dependencies that are also NPM packages. """ deps_in_npm = { name: dep for name, dep in dependencies.items() if dep.npm_name is not None } transport = AIOHTTPTransport( url="https://api.github.com/graphql", headers={"Authorization": f"bearer {gh_token}"}, ) client = Client( transport=transport, fetch_schema_from_transport=True, serialize_variables=True, parse_results=True, ) found_vulnerabilities: dict[str, list[Vulnerability]] = defaultdict(list) for name, dep in deps_in_npm.items(): variables_package = { "package_name": dep.npm_name, } result = client.execute( github_vulnerabilities_query, variable_values=variables_package ) matching_vulns = [ v for v in result["securityVulnerabilities"]["nodes"] if v["advisory"]["withdrawnAt"] is None and dep.version in SpecifierSet(v["vulnerableVersionRange"]) and v["advisory"]["ghsaId"] not in ignore_list ] if matching_vulns: found_vulnerabilities[name].extend( [ Vulnerability( id=vuln["advisory"]["ghsaId"], url=vuln["advisory"]["permalink"] ) for vuln in matching_vulns ] ) return found_vulnerabilities
async def test_aiohttp_cannot_execute_if_not_connected(event_loop, aiohttp_server): async def handler(request): return web.Response(text=query1_server_answer, content_type="application/json") app = web.Application() app.router.add_route("POST", "/", handler) server = await aiohttp_server(app) url = server.make_url("/") sample_transport = AIOHTTPTransport(url=url, timeout=10) query = gql(query1_str) with pytest.raises(TransportClosed): await sample_transport.execute(query)
async def test_aiohttp_cannot_connect_twice(event_loop, aiohttp_server): async def handler(request): return web.Response(text=query1_server_answer, content_type="application/json") app = web.Application() app.router.add_route("POST", "/", handler) server = await aiohttp_server(app) url = server.make_url("/") sample_transport = AIOHTTPTransport(url=url, timeout=10) async with Client(transport=sample_transport,) as session: with pytest.raises(TransportAlreadyConnected): await session.transport.connect()
async def plot_by_list(request): try: async with aiohttp.ClientSession() as session: params = request.rel_url.query['symbol'].split(',') if request.rel_url.query['symbol'] else ["BTCUSDT"] transport = AIOHTTPTransport(url='https://dbschool.alcyone.life/graphql') async with Client(transport=transport, fetch_schema_from_transport=True) as session: listofdf = [] for symbol in params: # Execute single query query = gql( """ query { tickers(where: { symbol_contains: "%s" }) { price created_at } } """ % symbol ) result = await session.execute(query) print(result) histprices = result['tickers'] histpricesdf = pd.DataFrame.from_dict(histprices) histpricesdf = histpricesdf.rename({'price': symbol}, axis=1) listofdf.append(histpricesdf) dfs = [df.set_index('created_at') for df in listofdf] histpriceconcat = pd.concat(dfs,axis=1) print(histpriceconcat) for i, col in enumerate(histpriceconcat.columns): histpriceconcat[col].plot() plt.title('Price Evolution Comparison') plt.xticks(rotation=70) plt.legend(histpriceconcat.columns) file_path = f"./tick/plots/{symbol}.png" # Saving the graph into a JPG file plt.savefig(file_path, bbox_inches='tight') return aiohttp.web.FileResponse(f'./{file_path}') except Exception as exp: raise exp
async def main(): transport = AIOHTTPTransport( url="https://countries.trevorblades.com/graphql") client = Client(transport=transport, fetch_schema_from_transport=True) # Using `async with` on the client will start a connection on the transport # and provide a `session` variable to execute queries on this connection. # Because we requested to fetch the schema from the transport, # GQL will fetch the schema just after the establishment of the first session async with client as session: # Instanciate the root of the DSL Schema as ds ds = DSLSchema(client.schema) # Create the query using dynamically generated attributes from ds query = dsl_gql( DSLQuery( ds.Query.continents(filter={ "code": { "eq": "EU" } }).select(ds.Continent.code, ds.Continent.name))) result = await session.execute(query) print(result) # This can also be written as: # I want to query the continents query_continents = ds.Query.continents # I want to get only the continents with code equal to "EU" query_continents(filter={"code": {"eq": "EU"}}) # I want this query to return the code and name fields query_continents.select(ds.Continent.code) query_continents.select(ds.Continent.name) # I generate a document from my query to be able to execute it query = dsl_gql(DSLQuery(query_continents)) # Execute the query result = await session.execute(query) print(result)
def test_code(): sample_transport = AIOHTTPTransport(url=url) client = Client(transport=sample_transport) query = gql(query1_str) # Note: subscriptions are not supported on the aiohttp transport # But we add this test in order to have 100% code coverage # It is to check that we will correctly set an event loop # in the subscribe function if there is none (in a Thread for example) # We cannot test this with the websockets transport because # the websockets transport will set an event loop in its init with pytest.raises(NotImplementedError): for result in client.subscribe(query): pass
def __init__(self, url: str, token: str): """ url: str The url of the server. token: str The token to access the server. """ graphqlUrl = f"{url}/graphql" headers = None if token == '' else dict(authorization=token) transport = AIOHTTPTransport(url=graphqlUrl, headers=headers) self._client = Client(transport=transport, fetch_schema_from_transport=True) self._url = url
async def test_aiohttp_invalid_query(event_loop): sample_transport = AIOHTTPTransport( url="https://countries.trevorblades.com/graphql") async with Client(transport=sample_transport) as session: query = gql(""" query getContinents { continents { code bloh } } """) with pytest.raises(TransportQueryError): await session.execute(query)
async def test_aiohttp_invalid_protocol(event_loop, aiohttp_server, response): async def handler(request): return web.Response(text=response, content_type="application/json") app = web.Application() app.router.add_route("POST", "/", handler) server = await aiohttp_server(app) url = server.make_url("/") sample_transport = AIOHTTPTransport(url=url) async with Client(transport=sample_transport,) as session: query = gql(query1_str) with pytest.raises(TransportProtocolError): await session.execute(query)
async def query(): api_key = "6pbvkg-r5El8vFNEci4AF7MPBRUTrG-BQ-gqhwwdQgWeFPBGbUWCXUdZaqULhTBJeCcLk2d1e3vjP_A3BXFVoPHRSrn6D3jEvHZRwKgIdz1Ct6QSPBUhkBanOXtvYHYx" # define our authentication process. header = { 'Authorization': 'bearer {}'.format(api_key), 'Content-Type': "application/json" } transport = AIOHTTPTransport(url="https://api.yelp.com/v3/graphql", headers=header) # Using `async with` on the client will start a connection on the transport # and provide a `session` variable to execute queries on this connection async with Client( transport=transport, fetch_schema_from_transport=True, ) as session: # Execute single query place = '''zion''' interest = '''food''' query = gql('''{ search(term:"''' + interest + '''", location:"''' + place + '''", radius: 40000, limit: 50) { total business { name alias reviews { text rating } } } } ''') start = time.time() result = await session.execute(query) end = time.time() print(result) print(end - start)
async def test_aiohttp_error_code_500(event_loop, aiohttp_server): async def handler(request): # Will generate http error code 500 raise Exception("Server error") app = web.Application() app.router.add_route("POST", "/", handler) server = await aiohttp_server(app) url = server.make_url("/") sample_transport = AIOHTTPTransport(url=url) async with Client(transport=sample_transport,) as session: query = gql(query1_str) with pytest.raises(TransportServerError): await session.execute(query)
async def test_aiohttp_subscribe_not_supported(event_loop, aiohttp_server): async def handler(request): return web.Response(text="does not matter", content_type="application/json") app = web.Application() app.router.add_route("POST", "/", handler) server = await aiohttp_server(app) url = server.make_url("/") sample_transport = AIOHTTPTransport(url=url) async with Client(transport=sample_transport,) as session: query = gql(query1_str) with pytest.raises(NotImplementedError): async for result in session.subscribe(query): pass
async def test_async_transport_close_on_schema_retrieval_failure(): """ Ensure that the transport session is closed if an error occurs when entering the context manager (e.g., because schema retrieval fails) """ from gql.transport.aiohttp import AIOHTTPTransport transport = AIOHTTPTransport(url="http://localhost/") client = Client(transport=transport, fetch_schema_from_transport=True) try: async with client: pass except Exception: # we don't care what exception is thrown, we just want to check if the # transport is closed afterwards pass assert client.transport.session is None
async def test_aiohttp_error_fetching_schema(event_loop, aiohttp_server): from aiohttp import web from gql.transport.aiohttp import AIOHTTPTransport error_answer = """ { "errors": [ { "errorType": "UnauthorizedException", "message": "Permission denied" } ] } """ async def handler(request): return web.Response( text=error_answer, content_type="application/json", ) app = web.Application() app.router.add_route("POST", "/", handler) server = await aiohttp_server(app) url = server.make_url("/") transport = AIOHTTPTransport(url=url, timeout=10) with pytest.raises(TransportQueryError) as exc_info: async with Client(transport=transport, fetch_schema_from_transport=True): pass expected_error = ( "Error while fetching schema: " "{'errorType': 'UnauthorizedException', 'message': 'Permission denied'}" ) assert expected_error in str(exc_info.value) assert transport.session is None
async def test_async_client_async_transport( event_loop, protocol, fetch_schema_from_transport ): from gql.transport.aiohttp import AIOHTTPTransport # Create http or https url url = f"{protocol}://countries.trevorblades.com/graphql" # Get async transport sample_transport = AIOHTTPTransport(url=url) # Instantiate client async with Client( transport=sample_transport, fetch_schema_from_transport=fetch_schema_from_transport, ) as session: query = gql( """ query getContinents { continents { code name } } """ ) # Execute query result = await session.execute(query) continents = result["continents"] africa = continents[0] assert africa["code"] == "AF" if fetch_schema_from_transport: assert session.client.schema is not None
async def test_aiohttp_error_code(event_loop, aiohttp_server): from aiohttp import web from gql.transport.aiohttp import AIOHTTPTransport async def handler(request): return web.Response(text=query1_server_error_answer, content_type="application/json") app = web.Application() app.router.add_route("POST", "/", handler) server = await aiohttp_server(app) url = server.make_url("/") transport = AIOHTTPTransport(url=url) async with Client(transport=transport) as session: query = gql(query1_str) with pytest.raises(TransportQueryError): await session.execute(query)
def test_code(): sample_transport = AIOHTTPTransport(url=url, timeout=10) with TemporaryFile(file_1_content) as test_file: client = Client(transport=sample_transport, ) query = gql(file_upload_mutation_1) file_path = test_file.filename with open(file_path, "rb") as f: params = {"file": f, "other_var": 42} result = client.execute(query, variable_values=params, upload_files=True) success = result["success"] assert success
async def main(): # Should look like: # https://XXXXXXXXXXXXXXXXXXXXXXXXXX.appsync-api.REGION.amazonaws.com/graphql url = os.environ.get("AWS_GRAPHQL_API_ENDPOINT") api_key = os.environ.get("AWS_GRAPHQL_API_KEY") if url is None or api_key is None: print("Missing environment variables") sys.exit() # Extract host from url host = str(urlparse(url).netloc) auth = AppSyncApiKeyAuthentication(host=host, api_key=api_key) transport = AIOHTTPTransport(url=url, auth=auth) async with Client( transport=transport, fetch_schema_from_transport=False, ) as session: query = gql( """ mutation createMessage($message: String!) { createMessage(input: {message: $message}) { id message createdAt } }""" ) variable_values = {"message": "Hello world!"} result = await session.execute(query, variable_values=variable_values) print(result)
async def test_aiohttp_extra_args(event_loop, aiohttp_server): from aiohttp import web from gql.transport.aiohttp import AIOHTTPTransport async def handler(request): return web.Response(text=query1_server_answer, content_type="application/json") app = web.Application() app.router.add_route("POST", "/", handler) server = await aiohttp_server(app) url = server.make_url("/") # passing extra arguments to aiohttp.ClientSession from aiohttp import DummyCookieJar jar = DummyCookieJar() transport = AIOHTTPTransport(url=url, timeout=10, client_session_args={ "version": "1.1", "cookie_jar": jar }) async with Client(transport=transport) as session: query = gql(query1_str) # Passing extra arguments to the post method of aiohttp result = await session.execute(query, extra_args={"allow_redirects": False}) continents = result["continents"] africa = continents[0] assert africa["code"] == "AF"
async def _tick_(request): try: async with aiohttp.ClientSession() as session: async with session.get('http://51.15.17.205:9000/tick/Mohamed') as response: json = await response.json() transport = AIOHTTPTransport(url="https://dbschool.alcyone.life/graphql") # Create a GraphQL client using the defined transport async with Client( transport=transport, fetch_schema_from_transport=True, ) as session: for value in json['data']: # Execute single query query = gql( """ mutation { createTicker(input: { data: { symbol: "%s", price: %.2f } }) { ticker { symbol price } } } """ % (value['symbol'], float(value['price'])) ) result = await session.execute(query) print(result) # return aiohttp.web.json_response(dict(json=json)) # Select your transport with a defined url endpoint # $> except Exception as exp: # <! # !0 return what's wrong in string and the type of the exception should be enough to understand where you're wrong noobs return aiohttp.web.json_response({'err':{'str':str(exp),'typ':str(type(exp))}}, status=500)
async def test_aiohttp_simple_query(event_loop, protocol): from gql.transport.aiohttp import AIOHTTPTransport # Create http or https url url = f"{protocol}://countries.trevorblades.com/graphql" # Get transport sample_transport = AIOHTTPTransport(url=url) # Instanciate client async with Client(transport=sample_transport) as session: query = gql(""" query getContinents { continents { code name } } """) # Fetch schema await session.fetch_schema() # Execute query result = await session.execute(query) # Verify result assert isinstance(result, Dict) print(result) continents = result["continents"] africa = continents[0] assert africa["code"] == "AF"
async def test_aiohttp_async_generator_upload(event_loop, aiohttp_server): import aiofiles from aiohttp import web from gql.transport.aiohttp import AIOHTTPTransport app = web.Application() app.router.add_route("POST", "/", binary_upload_handler) server = await aiohttp_server(app) url = server.make_url("/") transport = AIOHTTPTransport(url=url, timeout=10) with TemporaryFile(binary_file_content) as test_file: async with Client(transport=transport) as session: query = gql(file_upload_mutation_1) file_path = test_file.filename async def file_sender(file_name): async with aiofiles.open(file_name, "rb") as f: chunk = await f.read(64 * 1024) while chunk: yield chunk chunk = await f.read(64 * 1024) params = {"file": file_sender(file_path), "other_var": 42} # Execute query asynchronously result = await session.execute(query, variable_values=params, upload_files=True) success = result["success"] assert success
async def getQuotes(quotes): time = datetime.now(timezone('CET')).isoformat() async with aiohttp.ClientSession() as session: tasks = get_tasks(session) quote = await asyncio.gather(*tasks) for q in quote: data = await q.json() quotes.append({ "symbol": data["symbol"], "name": data["name"], "high": data["high"], "low": data["low"], "open": data["open"], "close": data["close"], "volume": data["volume"], "change": data["change"], "change_percent": data['percent_change'], "time": time }) transport = AIOHTTPTransport(url=gql_url, headers={ 'x-hasura-admin-secret': hasura_secret }) async with Client(transport=transport, fetch_schema_from_transport=True) as session: query = gql( """ mutation insertStockQuotes($objects: [quotes_insert_input!]!) { insert_quotes(objects: $objects) { returning { id } } } """ ) await session.execute(query, variable_values={ "objects": quotes }) quotes = []
def __init__(self): env = os.environ if not ('UTC_HOST' in env and 'UTC_USER' in env and 'UTC_PASS' in env and 'BACKEND_URL' in env): raise RuntimeError('Missing variables') self.__utc_host = env['UTC_HOST'] self.__utc_user = env['UTC_USER'] self.__utc_passwd = env['UTC_PASS'] self.__backend = env['BACKEND_URL'] self.__read_remote_sleep = 30 self.__read_seed_sleep = 0.35 self.__execution_date = datetime.now() self.__re_ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|[0-9]|\[[0-?]*[ -/]*[@-~])|\r|\n") self.__re_ctrl_type = re.compile(r'Controller Type\s+:\s\[(?P<ctrl_type>.*)]') self.__re_intergreens_table = re.compile(r'\s(?P<phase_name>[A-Z])\s+(?P<is_demand>[NY])\s+(?P<min_time>\d+)\s+(?P<max_time>\d+)\s+(?P<intergreens>((X|\d+)\s+)+(X|\d+))') self.__re_plan = re.compile(r'^Plan\s+(?P<id>\d+)\s(?P<junction>J\d{6}).*(?P<cycle>CY\d{3})\s(?P<phases>[A-Z0-9\s,!*]+)$') self.__re_extract_phases = re.compile(r"\s[A-Z]\s\d+") self.__re_extract_sequence = re.compile(r'Cyclic Check Sequence\s+:\s\[(?P<sequence>[A-Z]+)') self.__re_program_hour = re.compile(r"(?P<hour>\d{2}:\d{2}:\d{2}).*$") self.__re_program = re.compile(r"(?P<hour>(\d{2}:\d{2}:\d{2})?)(\d{3})?\s+PLAN\s+(?P<junction>[J|A]\d{6})\s+(?P<plan>(\d+|[A-Z]{1,2}))\s+TIMETABLE$") transport = AIOHTTPTransport(url=self.__backend) self.__api = Client(transport=transport, fetch_schema_from_transport=True) logger.warning('Using a {}s sleep call to wait for buffers from remote'.format(self.__read_remote_sleep)) logger.info('Using {} as API Endpoint'.format(self.__backend))