Skip to content

Commit ac3307d

Browse files
authored
Merge pull request #21 from JupiterOne/ApplyInlineLimits
Do the Right Thing™ when a query has a LIMIT inline
2 parents 8a6a190 + fff6817 commit ac3307d

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

jupiterone/client.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from warnings import warn
77
from typing import Dict, List
88

9+
import re
910
import requests
1011
from requests.adapters import HTTPAdapter, Retry
1112
from retrying import retry
@@ -155,6 +156,15 @@ def _cursor_query(
155156
include_deleted (bool): Include recently deleted entities in query/search
156157
"""
157158

159+
# If the query itself includes a LIMIT then we must parse that and check if we've reached
160+
# or exceeded the required number of results.
161+
limit_match = re.search(r"(?i)LIMIT\s+(?P<inline_limit>\d+)", query)
162+
163+
if limit_match:
164+
result_limit = int(limit_match.group("inline_limit"))
165+
else:
166+
result_limit = False
167+
158168
results: List = []
159169
while True:
160170
variables = {"query": query, "includeDeleted": include_deleted}
@@ -171,14 +181,24 @@ def _cursor_query(
171181

172182
results.extend(data)
173183

174-
if (
184+
if result_limit and len(results) >= result_limit:
185+
# We can stop paginating if we've collected enough results based on the requested limit
186+
break
187+
elif (
175188
"cursor" in response["data"]["queryV1"]
176189
and response["data"]["queryV1"]["cursor"] is not None
177190
):
191+
# We got a cursor and haven't collected enough results
178192
cursor = response["data"]["queryV1"]["cursor"]
179193
else:
194+
# No cursor returned so we're done
180195
break
181196

197+
# If we detected an inline LIMIT make sure we only return that many results
198+
if result_limit:
199+
return {"data": results[:result_limit]}
200+
201+
# Return everything
182202
return {"data": results}
183203

184204
def _limit_and_skip_query(

setup.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
# Copyright (c) 2020-2025 JupiterOne
22
from setuptools import setup, find_packages
33

4-
install_reqs = [
5-
'requests',
6-
'retrying'
7-
]
4+
install_reqs = ["requests", "retrying"]
85

9-
setup(name='jupiterone',
10-
version='1.0.0',
11-
description='A Python client for the JupiterOne API',
12-
license='MIT License',
13-
author='JupiterOne',
14-
author_email='[email protected]',
15-
maintainer='JupiterOne',
16-
url='https://github.com/JupiterOne/jupiterone-api-client-python',
17-
install_requires=install_reqs,
18-
classifiers=[
19-
'Development Status :: 4 - Beta',
20-
'Intended Audience :: Developers',
21-
'Intended Audience :: Information Technology',
22-
'Intended Audience :: System Administrators',
23-
'License :: OSI Approved :: MIT License',
24-
'Natural Language :: English',
25-
'Operating System :: POSIX :: Linux',
26-
'Programming Language :: Python',
27-
'Topic :: Security',
28-
],
29-
packages=find_packages()
30-
)
6+
setup(
7+
name="jupiterone",
8+
version="1.0.1",
9+
description="A Python client for the JupiterOne API",
10+
license="MIT License",
11+
author="JupiterOne",
12+
author_email="[email protected]",
13+
maintainer="JupiterOne",
14+
url="https://github.com/JupiterOne/jupiterone-api-client-python",
15+
install_requires=install_reqs,
16+
classifiers=[
17+
"Development Status :: 4 - Beta",
18+
"Intended Audience :: Developers",
19+
"Intended Audience :: Information Technology",
20+
"Intended Audience :: System Administrators",
21+
"License :: OSI Approved :: MIT License",
22+
"Natural Language :: English",
23+
"Operating System :: POSIX :: Linux",
24+
"Programming Language :: Python",
25+
"Topic :: Security",
26+
],
27+
packages=find_packages(),
28+
)

0 commit comments

Comments
 (0)