Skip to content

Commit d040e1f

Browse files
bors[bot]curquiza
andauthored
Merge #176
176: Restructure code r=curquiza a=curquiza ⚠️ Should be merged after the merge of #175 Breaking change: - Make the internal method `Index.create(...)` a `@classmethod` instead of a `@staticmethod`. Why? Put the logic related to an index in the `Index` class instead of the `Client` class. See the difference between static and class methods in Python: https://stackabuse.com/pythons-classmethod-and-staticmethod-explained/. - Remove the internal method `Index.get_indexes()`. Since it's not related to only one index, this method should not be present in the `Index` class. - Make the `update()` method returns an `Index` object instead of a `dict` because it's more convenient to manipulate object instead of dict. This is consitent with `client.index('movies').fetch_info()` and `client.create_index('movies)` and `client.index('movies')` Changes: - Check the type of the responses more accurately: `assert isinstance(response, Index)` Co-authored-by: Clementine Urquizar <[email protected]>
2 parents 8f57cf0 + a85c1e6 commit d040e1f

File tree

3 files changed

+16
-30
lines changed

3 files changed

+16
-30
lines changed

meilisearch/client.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ def create_index(self, uid, options=None):
4545
HTTPError
4646
In case of any other error found here https://docs.meilisearch.com/references/#errors-status-code
4747
"""
48-
index_dict = Index.create(self.config, uid, options)
49-
return Index(self.config, index_dict['uid'], index_dict['primaryKey'])
48+
return Index.create(self.config, uid, options)
5049

5150
def get_indexes(self):
5251
"""Get all indexes.
@@ -60,7 +59,7 @@ def get_indexes(self):
6059
list
6160
List of indexes in dictionnary format. (e.g [{ 'uid': 'movies' 'primaryKey': 'objectID' }])
6261
"""
63-
return Index.get_indexes(self.config)
62+
return self.http.get(self.config.paths.index)
6463

6564
def get_index(self, uid):
6665
"""Get the index.

meilisearch/index.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def update(self, **body):
6464
payload['primaryKey'] = primary_key
6565
response = self.http.put('{}/{}'.format(self.config.paths.index, self.uid), payload)
6666
self.primary_key = response['primaryKey']
67-
return response
67+
return self
6868

6969
def fetch_info(self):
7070
"""Fetch the information of the index.
@@ -88,8 +88,8 @@ def get_primary_key(self):
8888
"""
8989
return self.fetch_info().primary_key
9090

91-
@staticmethod
92-
def create(config, uid, options=None):
91+
@classmethod
92+
def create(cls, config, uid, options=None):
9393
"""Create the index.
9494
9595
Parameters
@@ -112,22 +112,8 @@ def create(config, uid, options=None):
112112
if options is None:
113113
options = {}
114114
payload = {**options, 'uid': uid}
115-
return HttpRequests(config).post(config.paths.index, payload)
116-
117-
@staticmethod
118-
def get_indexes(config):
119-
"""Get all indexes from meilisearch.
120-
121-
Returns
122-
-------
123-
indexes : list
124-
List of indexes (dict)
125-
Raises
126-
------
127-
HTTPError
128-
In case of any error found here https://docs.meilisearch.com/references/#errors-status-code
129-
"""
130-
return HttpRequests(config).get(config.paths.index)
115+
index_dict = HttpRequests(config).post(config.paths.index, payload)
116+
return cls(config, index_dict['uid'], index_dict['primaryKey'])
131117

132118
def get_all_update_status(self):
133119
"""Get all update status from MeiliSearch

meilisearch/tests/index/test_index.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
import meilisearch
3+
from meilisearch.index import Index
34
from meilisearch.tests import BASE_URL, MASTER_KEY, clear_all_indexes
45

56
class TestIndex:
@@ -18,23 +19,23 @@ def setup_class(self):
1819
def test_create_index(self):
1920
"""Tests creating an index"""
2021
index = self.client.create_index(uid=self.index_uid)
21-
assert isinstance(index, object)
22+
assert isinstance(index, Index)
2223
assert index.uid == self.index_uid
2324
assert index.primary_key is None
2425
assert index.get_primary_key() is None
2526

2627
def test_create_index_with_primary_key(self):
2728
"""Tests creating an index with a primary key"""
2829
index = self.client.create_index(uid=self.index_uid2, options={'primaryKey': 'book_id'})
29-
assert isinstance(index, object)
30+
assert isinstance(index, Index)
3031
assert index.uid == self.index_uid2
3132
assert index.primary_key == 'book_id'
3233
assert index.get_primary_key() == 'book_id'
3334

3435
def test_create_index_with_uid_in_options(self):
3536
"""Tests creating an index with a primary key"""
3637
index = self.client.create_index(uid=self.index_uid3, options={'uid': 'wrong', 'primaryKey': 'book_id'})
37-
assert isinstance(index, object)
38+
assert isinstance(index, Index)
3839
assert index.uid == self.index_uid3
3940
assert index.primary_key == 'book_id'
4041
assert index.get_primary_key() == 'book_id'
@@ -51,7 +52,7 @@ def test_get_indexes(self):
5152

5253
def test_index_with_any_uid(self):
5354
index = self.client.index('anyUID')
54-
assert isinstance(index, object)
55+
assert isinstance(index, Index)
5556
assert index.uid == 'anyUID'
5657
assert index.primary_key is None
5758
assert index.config is not None
@@ -64,7 +65,7 @@ def test_index_with_none_uid(self):
6465
def test_get_index_with_valid_uid(self):
6566
"""Tests getting one index with uid"""
6667
response = self.client.get_index(uid=self.index_uid)
67-
assert isinstance(response, object)
68+
assert isinstance(response, Index)
6869
assert response.uid == self.index_uid
6970

7071
def test_get_index_with_none_uid(self):
@@ -109,7 +110,7 @@ def test_index_fetch_info(self):
109110
"""Tests getting the index info"""
110111
index = self.client.index(uid=self.index_uid)
111112
response = index.fetch_info()
112-
assert isinstance(response, object)
113+
assert isinstance(response, Index)
113114
assert response.uid == self.index_uid
114115
assert response.primary_key is None
115116
assert response.primary_key == index.primary_key
@@ -119,7 +120,7 @@ def test_index_fetch_info_containing_primary_key(self):
119120
"""Tests getting the index info"""
120121
index = self.client.index(uid=self.index_uid3)
121122
response = index.fetch_info()
122-
assert isinstance(response, object)
123+
assert isinstance(response, Index)
123124
assert response.uid == self.index_uid3
124125
assert response.primary_key == 'book_id'
125126
assert response.primary_key == index.primary_key
@@ -138,7 +139,7 @@ def test_update_index(self):
138139
"""Tests updating an index"""
139140
index = self.client.index(uid=self.index_uid)
140141
response = index.update(primaryKey='objectID')
141-
assert isinstance(response, object)
142+
assert isinstance(response, Index)
142143
assert index.primary_key == 'objectID'
143144
assert index.get_primary_key() == 'objectID'
144145

0 commit comments

Comments
 (0)