Skip to content

Commit d053e9f

Browse files
committed
Add resource query to indexes
1 parent a572124 commit d053e9f

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

meilisearch/client.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import hmac
44
import json
55
import datetime
6+
from urllib import parse
67
from typing import Any, Dict, List, Optional, Union
78
from meilisearch.index import Index
89
from meilisearch.config import Config
@@ -78,9 +79,14 @@ def delete_index(self, uid: str) -> Dict[str, Any]:
7879

7980
return self.http.delete(f'{self.config.paths.index}/{uid}')
8081

81-
def get_indexes(self) -> List[Index]:
82+
def get_indexes(self, resource_query: Optional[Dict[str, Any]] = None) -> Dict[str, List[Index]]:
8283
"""Get all indexes.
8384
85+
Parameters
86+
----------
87+
resource_query (optional):
88+
resource_query accepted by the get indexes route: https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
89+
8490
Returns
8591
-------
8692
indexes:
@@ -91,10 +97,12 @@ def get_indexes(self) -> List[Index]:
9197
MeiliSearchApiError
9298
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
9399
"""
94-
response = self.http.get(self.config.paths.index)
95-
96-
return {
97-
'results' : [
100+
if resource_query is None:
101+
resource_query = {}
102+
response = self.http.get(
103+
f'{self.config.paths.index}?{parse.urlencode(resource_query)}'
104+
)
105+
response['results'] = [
98106
Index(
99107
self.config,
100108
index["uid"],
@@ -104,11 +112,16 @@ def get_indexes(self) -> List[Index]:
104112
)
105113
for index in response['results']
106114
]
107-
}
115+
return response
108116

109-
def get_raw_indexes(self) -> List[Dict[str, Any]]:
117+
def get_raw_indexes(self, resource_query: Optional[Dict[str, Any]] = None) -> List[Dict[str, Any]]:
110118
"""Get all indexes in dictionary format.
111119
120+
Parameters
121+
----------
122+
resource_query (optional):
123+
resource_query accepted by the get indexes route: https://docs.meilisearch.com/reference/api/indexes.html#list-all-indexes
124+
112125
Returns
113126
-------
114127
indexes:
@@ -119,7 +132,11 @@ def get_raw_indexes(self) -> List[Dict[str, Any]]:
119132
MeiliSearchApiError
120133
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
121134
"""
122-
return self.http.get(self.config.paths.index)
135+
if resource_query is None:
136+
resource_query = {}
137+
return self.http.get(
138+
f'{self.config.paths.index}?{parse.urlencode(resource_query)}'
139+
)
123140

124141
def get_index(self, uid: str) -> Index:
125142
"""Get the index.

tests/index/test_index.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,33 @@ def test_get_indexes(client):
4141
"""Tests getting all indexes."""
4242
response = client.get_indexes()
4343
uids = [index.uid for index in response['results']]
44-
assert isinstance(response, list)
44+
assert isinstance(response['results'], list)
4545
assert common.INDEX_UID in uids
4646
assert common.INDEX_UID2 in uids
4747
assert common.INDEX_UID3 in uids
48-
assert len(response) == 3
48+
assert len(response['results']) == 3
49+
50+
@pytest.mark.usefixtures("indexes_sample")
51+
def test_get_indexes_with_resource_query(client):
52+
"""Tests getting all indexes."""
53+
response = client.get_indexes(resource_query={'limit':1, 'offset': 1})
54+
assert len(response['results']) == 1
4955

5056
@pytest.mark.usefixtures("indexes_sample")
5157
def test_get_raw_indexes(client):
52-
response = client.get_raw_indexes()['results']
53-
uids = [index['uid'] for index in response]
54-
assert isinstance(response, list)
58+
response = client.get_raw_indexes()
59+
uids = [index['uid'] for index in response['results']]
60+
assert isinstance(response['results'], list)
5561
assert common.INDEX_UID in uids
5662
assert common.INDEX_UID2 in uids
5763
assert common.INDEX_UID3 in uids
58-
assert len(response) == 3
64+
assert len(response['results']) == 3
65+
66+
@pytest.mark.usefixtures("indexes_sample")
67+
def test_get_raw_indexeswith_resource_query(client):
68+
response = client.get_raw_indexes(resource_query={'limit':1, 'offset': 1})
69+
assert isinstance(response['results'], list)
70+
assert len(response['results']) == 1
5971

6072
def test_index_with_any_uid(client):
6173
index = client.index('anyUID')

0 commit comments

Comments
 (0)