diff --git a/graphinder/pool/detectors.py b/graphinder/pool/detectors.py index 06b7ff5..dbc42ed 100644 --- a/graphinder/pool/detectors.py +++ b/graphinder/pool/detectors.py @@ -238,8 +238,9 @@ async def detect(self) -> Tuple[bool, bool]: async def is_gql_endpoint( - session: aiohttp.ClientSession, url: str, + session: Optional[aiohttp.ClientSession] = None, + headers: Optional[Dict] = None, logger: Optional[logging.Logger] = None, ) -> Tuple[bool, bool]: """Check if the given url seems to be GraphQL endpoint. @@ -254,9 +255,26 @@ async def is_gql_endpoint( bool: True if the authentication is valid, False otherwise. """ + assert url and url.startswith('http'), 'Only http(s) urls are supported' + + headers = headers or {} + + # Open new session if necessary + has_opened_new_session = False + if not session: + session = aiohttp.ClientSession(headers=headers) + has_opened_new_session = True + else: + assert isinstance(session, aiohttp.ClientSession), 'Valid session must be provided' + detector = GraphQLEndpointDetector( session, url, logger, ) - return await detector.detect() + + status = await detector.detect() + + if headers and has_opened_new_session: + await session.close() + return status diff --git a/graphinder/pool/domain.py b/graphinder/pool/domain.py index 4241839..940c07f 100644 --- a/graphinder/pool/domain.py +++ b/graphinder/pool/domain.py @@ -85,6 +85,6 @@ async def fetch_endpoint( self.logger.debug(f'fetching endpoint {url}...') - if (await is_gql_endpoint(self.session, url))[0]: + if (await is_gql_endpoint(url, session=self.session))[0]: self.logger.info(f'found GQL endpoint {url}.') self.results.add(Url(url)) diff --git a/pyproject.toml b/pyproject.toml index 107794e..bd51050 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "graphinder" -version = "1.10.0" +version = "1.11.0" description = "Escape Graphinder" authors = ["Escape Technologies SAS "] maintainers = [ diff --git a/tests/unit/pool/test_detectors.py b/tests/unit/pool/test_detectors.py index cb68b79..e43aecc 100644 --- a/tests/unit/pool/test_detectors.py +++ b/tests/unit/pool/test_detectors.py @@ -19,5 +19,11 @@ async def test_is_gql_endpoint() -> None: async with aiohttp.ClientSession() as session: - assert not (await is_gql_endpoint(session, 'https://example.com'))[0] - assert (await is_gql_endpoint(session, 'https://gontoz.escape.tech'))[0] + assert not (await is_gql_endpoint( + 'https://example.com', + session=session, + ))[0] + assert (await is_gql_endpoint( + 'https://gontoz.escape.tech', + session=session, + ))[0]