Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit 140a204

Browse files
author
Samuel Hassine
committed
[client] Add pagination + examples
1 parent 2fa8a8b commit 140a204

27 files changed

+168
-45
lines changed

examples/get_all_indicators.py

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# coding: utf-8
2+
3+
from pycti import OpenCTIApiClient
4+
5+
# Variables
6+
api_url = "https://demo.opencti.io"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
8+
9+
# OpenCTI initialization
10+
opencti_api_client = OpenCTIApiClient(api_url, api_token)
11+
12+
# Get all reports using the pagination
13+
custom_attributes = """
14+
id
15+
indicator_pattern
16+
created
17+
"""
18+
19+
final_indicators = []
20+
data = opencti_api_client.indicator.list(
21+
first=50, customAttributes=custom_attributes, withPagination=True
22+
)
23+
final_indicators = final_indicators + data["entities"]
24+
25+
while data["pagination"]["hasNextPage"]:
26+
after = data["pagination"]["endCursor"]
27+
print("Listing reports after " + after)
28+
data = opencti_api_client.indicator.list(
29+
first=50, after=after, customAttributes=custom_attributes, withPagination=True
30+
)
31+
final_indicators = final_indicators + data["entities"]
32+
33+
# Print
34+
for indicator in final_indicators:
35+
print("[" + indicator["created"] + "] " + indicator["indicator_pattern"])
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# coding: utf-8
2+
3+
from pycti import OpenCTIApiClient
4+
5+
# Variables
6+
api_url = "https://demo.opencti.io"
7+
api_token = "2b4f29e3-5ea8-4890-8cf5-a76f61f1e2b2"
8+
9+
# OpenCTI initialization
10+
opencti_api_client = OpenCTIApiClient(api_url, api_token)
11+
12+
# Get all reports using the pagination
13+
custom_attributes = """
14+
id
15+
name
16+
published
17+
description
18+
"""
19+
20+
final_reports = []
21+
data = opencti_api_client.report.list(
22+
first=50, customAttributes=custom_attributes, withPagination=True
23+
)
24+
final_reports = final_reports + data["entities"]
25+
26+
while data["pagination"]["hasNextPage"]:
27+
after = data["pagination"]["endCursor"]
28+
print("Listing reports after " + after)
29+
data = opencti_api_client.report.list(
30+
first=50, after=after, customAttributes=custom_attributes, withPagination=True
31+
)
32+
final_reports = final_reports + data["entities"]
33+
34+
# Print
35+
for report in final_reports:
36+
print("[" + report["published"] + "] " + report["name"])

pycti/api/opencti_api_client.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,11 @@ def not_empty(self, value):
278278
else:
279279
return False
280280

281-
def process_multiple(self, data):
282-
result = []
281+
def process_multiple(self, data, with_pagination=False):
282+
if with_pagination:
283+
result = {"entities": [], "pagination": {}}
284+
else:
285+
result = []
283286
if data is None:
284287
return result
285288
for edge in (
@@ -293,7 +296,13 @@ def process_multiple(self, data):
293296
and "id" in edge["relation"]
294297
):
295298
row["remote_relation_id"] = edge["relation"]["id"]
296-
result.append(self.process_multiple_fields(row))
299+
if with_pagination:
300+
result["entities"].append(self.process_multiple_fields(row))
301+
else:
302+
result.append(self.process_multiple_fields(row))
303+
304+
if with_pagination and "pageInfo" in data:
305+
result["pagination"] = data["pageInfo"]
297306
return result
298307

299308
def process_multiple_ids(self, data):

pycti/entities/opencti_attack_pattern.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def list(self, **kwargs):
133133
order_mode = kwargs.get("orderMode", None)
134134
custom_attributes = kwargs.get("customAttributes", None)
135135
get_all = kwargs.get("getAll", False)
136+
with_pagination = kwargs.get("withPagination", False)
136137
if get_all:
137138
first = 500
138139

@@ -194,7 +195,9 @@ def list(self, **kwargs):
194195
final_data = final_data + data
195196
return final_data
196197
else:
197-
return self.opencti.process_multiple(result["data"]["attackPatterns"])
198+
return self.opencti.process_multiple(
199+
result["data"]["attackPatterns"], with_pagination
200+
)
198201

199202
"""
200203
Read a Attack-Pattern object

pycti/entities/opencti_campaign.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def list(self, **kwargs):
116116
order_mode = kwargs.get("orderMode", None)
117117
custom_attributes = kwargs.get("customAttributes", None)
118118
get_all = kwargs.get("getAll", False)
119+
with_pagination = kwargs.get("withPagination", False)
119120
if get_all:
120121
first = 500
121122

@@ -155,7 +156,9 @@ def list(self, **kwargs):
155156
"orderMode": order_mode,
156157
},
157158
)
158-
return self.opencti.process_multiple(result["data"]["campaigns"])
159+
return self.opencti.process_multiple(
160+
result["data"]["campaigns"], with_pagination
161+
)
159162

160163
"""
161164
Read a Campaign object

pycti/entities/opencti_course_of_action.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def list(self, **kwargs):
113113
order_mode = kwargs.get("orderMode", None)
114114
custom_attributes = kwargs.get("customAttributes", None)
115115
get_all = kwargs.get("getAll", False)
116+
with_pagination = kwargs.get("withPagination", False)
116117
if get_all:
117118
first = 500
118119

@@ -153,7 +154,9 @@ def list(self, **kwargs):
153154
"orderMode": order_mode,
154155
},
155156
)
156-
return self.opencti.process_multiple(result["data"]["courseOfActions"])
157+
return self.opencti.process_multiple(
158+
result["data"]["courseOfActions"], with_pagination
159+
)
157160

158161
"""
159162
Read a Course-Of-Action object

pycti/entities/opencti_external_reference.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def list(self, **kwargs):
3737
order_by = kwargs.get("orderBy", None)
3838
order_mode = kwargs.get("orderMode", None)
3939
get_all = kwargs.get("getAll", False)
40+
with_pagination = kwargs.get("withPagination", False)
4041
if get_all:
4142
first = 500
4243

@@ -76,7 +77,9 @@ def list(self, **kwargs):
7677
"orderMode": order_mode,
7778
},
7879
)
79-
return self.opencti.process_multiple(result["data"]["externalReferences"])
80+
return self.opencti.process_multiple(
81+
result["data"]["externalReferences"], with_pagination
82+
)
8083

8184
"""
8285
Read a External-Reference object

pycti/entities/opencti_identity.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def list(self, **kwargs):
116116
order_mode = kwargs.get("orderMode", None)
117117
custom_attributes = kwargs.get("customAttributes", None)
118118
get_all = kwargs.get("getAll", False)
119+
with_pagination = kwargs.get("withPagination", False)
119120
if get_all:
120121
first = 500
121122

@@ -156,7 +157,9 @@ def list(self, **kwargs):
156157
"orderMode": order_mode,
157158
},
158159
)
159-
return self.opencti.process_multiple(result["data"]["identities"])
160+
return self.opencti.process_multiple(
161+
result["data"]["identities"], with_pagination
162+
)
160163

161164
"""
162165
Read a Identity object

pycti/entities/opencti_incident.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def list(self, **kwargs):
125125
order_mode = kwargs.get("orderMode", None)
126126
custom_attributes = kwargs.get("customAttributes", None)
127127
get_all = kwargs.get("getAll", False)
128+
with_pagination = kwargs.get("withPagination", False)
128129
if get_all:
129130
first = 500
130131

@@ -164,7 +165,9 @@ def list(self, **kwargs):
164165
"orderMode": order_mode,
165166
},
166167
)
167-
return self.opencti.process_multiple(result["data"]["incidents"])
168+
return self.opencti.process_multiple(
169+
result["data"]["incidents"], with_pagination
170+
)
168171

169172
"""
170173
Read a Incident object

0 commit comments

Comments
 (0)