Skip to content

Commit

Permalink
refactor: is gql endpoint accept either headers or session
Browse files Browse the repository at this point in the history
  • Loading branch information
nullswan committed Sep 9, 2022
1 parent fe3b0b9 commit 2d3e040
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
22 changes: 20 additions & 2 deletions graphinder/pool/detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
2 changes: 1 addition & 1 deletion graphinder/pool/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "graphinder"
version = "1.10.0"
version = "1.11.0"
description = "Escape Graphinder"
authors = ["Escape Technologies SAS <ping@escape.tech>"]
maintainers = [
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/pool/test_detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit 2d3e040

Please sign in to comment.