Skip to content

Commit 6e26d73

Browse files
committed
Create J1QLdeferredResponse.py
1 parent 485e67a commit 6e26d73

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

examples/J1QLdeferredResponse.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import os
2+
import time
3+
import json
4+
import requests
5+
from requests.adapters import HTTPAdapter, Retry
6+
7+
# JupiterOne API creds
8+
acct = os.environ.get("JUPITERONE_ACCOUNT")
9+
token = os.environ.get("JUPITERONE_TOKEN")
10+
11+
# JupiterOne GraphQL API:
12+
j1_graphql_url = "https://graphql.dev.jupiterone.io"
13+
14+
# JupiterOne GraphQL API headers
15+
j1_graphql_headers = {
16+
'Content-Type': 'application/json',
17+
'Authorization': 'Bearer ' + token,
18+
'Jupiterone-Account': acct
19+
}
20+
21+
gql_query = """
22+
query J1QL(
23+
$query: String!
24+
$variables: JSON
25+
$cursor: String
26+
$deferredResponse: DeferredResponseOption
27+
) {
28+
queryV1(
29+
query: $query
30+
variables: $variables
31+
deferredResponse: $deferredResponse
32+
cursor: $cursor
33+
) {
34+
type
35+
url
36+
}
37+
}
38+
"""
39+
40+
gql_variables = {
41+
"query": "find (snyk_finding | snyk_finding_coordinate | insightvm_finding | github_finding | semgrep_finding)",
42+
"deferredResponse": "FORCE",
43+
"cursor": "",
44+
"flags": {
45+
"variableResultSize": True
46+
},
47+
}
48+
49+
payload = {
50+
"query": gql_query,
51+
"variables": gql_variables
52+
}
53+
all_query_results = []
54+
cursor = None
55+
56+
while True:
57+
58+
payload['variables']['cursor'] = cursor
59+
60+
s = requests.Session()
61+
retries = Retry(total=10, backoff_factor=2, status_forcelist=[502, 503, 504, 429])
62+
s.mount('https://', HTTPAdapter(max_retries=retries))
63+
url_response = s.post(j1_graphql_url, headers=j1_graphql_headers, json=payload)
64+
download_url = url_response.json()['data']['queryV1']['url']
65+
print(download_url)
66+
67+
download_response = s.get(download_url).json()
68+
69+
status = download_response['status']
70+
71+
while status == 'IN_PROGRESS':
72+
time.sleep(1) # Sleep 1 second between checking status
73+
74+
download_response = s.get(download_url).json() # fetch results data from download URL
75+
76+
status = download_response['status'] # update 'status' for next iteration
77+
78+
all_query_results.extend(download_response['data']) # add results to all results list
79+
print(len(download_response['data']))
80+
81+
# Update cursor from response
82+
if 'cursor' in download_response:
83+
cursor = download_response['cursor']
84+
print(cursor)
85+
86+
else:
87+
break
88+
89+
# print(all_query_results)
90+
print(len(all_query_results))

0 commit comments

Comments
 (0)