Skip to content

Commit 7756202

Browse files
committed
Paging
1 parent 9e7c6f3 commit 7756202

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ def example_get_service_filter_plugin():
4242
assert all((i.tags == ["ntlm"] for i in response.json()))
4343

4444

45+
def example_get_service_filter_plugin_with_pagination():
46+
"""
47+
Filter by fields. In this example, we want to have the NTLM services.
48+
A list of plugins can be found in leakix.plugin
49+
"""
50+
query_http_ntlm = MustQuery(field=PluginField(Plugin.HttpNTLM))
51+
response = CLIENT.get_service(queries=[query_http_ntlm], page=1)
52+
assert response.status_code() == 200
53+
# check we only get NTML related services
54+
assert all((i.tags == ["ntlm"] for i in response.json()))
55+
56+
4557
def example_get_leaks_filter_multiple_plugins():
4658
query_http_ntlm = MustQuery(field=PluginField(Plugin.HttpNTLM))
4759
query_country = MustQuery(field=CountryField("France"))

example/example_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ def example_get_service_filter_plugin():
2727
assert all((i.tags == ["ntlm"] for i in response.json()))
2828

2929

30+
def example_get_service_filter_plugin_with_pagination():
31+
"""
32+
Filter by fields. In this example, we want to have the NTLM services.
33+
A list of plugins can be found in leakix.plugin.
34+
Ask for page 1 (starts at 0)
35+
"""
36+
query_http_ntlm = MustQuery(field=PluginField(Plugin.HttpNTLM))
37+
response = CLIENT.get_service(queries=[query_http_ntlm], page=1)
38+
assert response.status_code() == 200
39+
# check we only get NTML related services
40+
assert all((i.tags == ["ntlm"] for i in response.json()))
41+
42+
3043
def example_get_leaks_filter_multiple_plugins():
3144
query_http_ntlm = MustQuery(field=PluginField(Plugin.HttpNTLM))
3245
query_country = MustQuery(field=CountryField("France"))
@@ -76,6 +89,7 @@ def example_get_plugins():
7689
if __name__ == "__main__":
7790
example_get_host_filter_plugin()
7891
example_get_service_filter_plugin()
92+
example_get_service_filter_plugin_with_pagination()
7993
example_get_leaks_filter_multiple_plugins()
8094
example_get_leaks_multiple_filter_plugins_must_not()
8195
example_get_leak_raw_query()

leakix/client.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class HostResult(Model):
2525

2626

2727
class Client:
28+
MAX_RESULTS_PER_PAGE = 20
29+
2830
def __init__(
2931
self,
3032
api_key: Optional[str] = None,
@@ -53,27 +55,31 @@ def __get(self, url, params):
5355
else:
5456
return ErrorResponse(response=r, response_json=r.json())
5557

56-
def get(self, scope: Scope, queries: Optional[List[Query]] = None):
58+
def get(self, scope: Scope, queries: Optional[List[Query]] = None, page: int = 0):
59+
if page < 0:
60+
raise ValueError("Page argument must be a positive integer")
5761
if queries is None or len(queries) == 0:
5862
serialized_query = EmptyQuery().serialize()
5963
else:
6064
serialized_query = [q.serialize() for q in queries]
6165
serialized_query = " ".join(serialized_query)
6266
serialized_query = "%s" % serialized_query
6367
url = "%s/search" % self.base_url
64-
r = self.__get(url=url, params={"scope": scope.value, "q": serialized_query})
68+
r = self.__get(
69+
url=url, params={"scope": scope.value, "q": serialized_query, "page": page}
70+
)
6571
return r
6672

67-
def get_service(self, queries: Optional[List[Query]] = None):
68-
r = self.get(Scope.SERVICE, queries=queries)
73+
def get_service(self, queries: Optional[List[Query]] = None, page: int = 0):
74+
r = self.get(Scope.SERVICE, queries=queries, page=page)
6975
if r.is_success():
7076
r.response_json = [
7177
l9format.L9Event.from_dict(res) for res in r.response_json
7278
]
7379
return r
7480

75-
def get_leak(self, queries: Optional[List[Query]] = None):
76-
r = self.get(Scope.LEAK, queries=queries)
81+
def get_leak(self, queries: Optional[List[Query]] = None, page: int = 0):
82+
r = self.get(Scope.LEAK, queries=queries, page=page)
7783
if r.is_success():
7884
r.response_json = [
7985
l9format.L9Event.from_dict(res) for res in r.response_json

0 commit comments

Comments
 (0)