Skip to content

Commit 0e90e64

Browse files
authored
Merge pull request #76 from meilisearch/inheritance_refactor
refacto: Inheritance refactor
2 parents d4eddf3 + 3b3ffaf commit 0e90e64

File tree

13 files changed

+793
-1111
lines changed

13 files changed

+793
-1111
lines changed

meilisearch/_httprequests.py

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,53 @@
22

33
class HttpRequests:
44

5-
@staticmethod
6-
def get(config, path):
5+
config = None
6+
headers = {}
7+
8+
def __init__(self, config):
9+
self.config = config
10+
self.headers = {
11+
'X-Meili-Api-Key': self.config.apikey,
12+
'Content-Type': 'application/json'
13+
}
14+
15+
16+
def get(self, path):
717
request = requests.get(
8-
config.url + '/' + path,
9-
headers={
10-
'X-Meili-Api-Key': config.apikey,
11-
'Content-Type': 'application/json'
12-
}
18+
self.config.url + '/' + path,
19+
headers=self.headers,
1320
)
14-
return HttpRequests.__validate(request)
21+
return self.__validate(request)
1522

16-
@staticmethod
17-
def post(config, path, body=None):
23+
def post(self, path, body=None):
1824
if body is None:
1925
body = {}
2026
request = requests.post(
21-
config.url + '/' + path,
22-
headers={
23-
'x-meili-api-key': config.apikey,
24-
'content-type': 'application/json'
25-
},
27+
self.config.url + '/' + path,
28+
headers=self.headers,
2629
json=body
2730
)
28-
return HttpRequests.__validate(request)
31+
return self.__validate(request)
2932

30-
@staticmethod
31-
def put(config, path, body=None):
33+
def put(self, path, body=None):
3234
if body is None:
3335
body = {}
3436
request = requests.put(
35-
config.url + '/' + path,
36-
headers={
37-
'x-meili-api-key': config.apikey,
38-
'content-type': 'application/json'
39-
},
37+
self.config.url + '/' + path,
38+
headers=self.headers,
4039
json=body
4140
)
42-
return HttpRequests.__validate(request)
41+
return self.__validate(request)
4342

44-
@staticmethod
45-
def patch(config, path, body=None):
46-
if body is None:
47-
body = {}
48-
request = requests.patch(
49-
config.url + '/' + path,
50-
headers={
51-
'x-meili-api-key': config.apikey,
52-
'content-type': 'application/json'
53-
},
54-
json=body
55-
)
56-
return HttpRequests.__validate(request)
57-
58-
@staticmethod
59-
def delete(config, path, body=None):
43+
def delete(self, path, body=None):
6044
if body is None:
6145
body = {}
6246
request = requests.delete(
63-
config.url + '/' + path,
64-
headers={
65-
'x-meili-api-key': config.apikey,
66-
'content-type': 'application/json'
67-
},
47+
self.config.url + '/' + path,
48+
headers=self.headers,
6849
json=body
6950
)
70-
return HttpRequests.__validate(request)
51+
return self.__validate(request)
7152

7253
@staticmethod
7354
def __to_json(request):

meilisearch/client.py

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
from meilisearch.index import Index
2-
from meilisearch.health import Health
3-
from meilisearch.key import Key
42
from meilisearch.config import Config
5-
from meilisearch.sys_info import SysInfo
6-
from meilisearch.stat import Stat
7-
from meilisearch.version import Version
3+
from meilisearch._httprequests import HttpRequests
84

9-
class Client(Health, Key, SysInfo, Version):
5+
class Client():
106
"""
117
A client for the MeiliSearch API
128
139
A client instance is needed for every MeiliSearch API method to know the location of
1410
MeiliSearch and its permissions.
1511
"""
1612

13+
config = None
14+
http = None
15+
1716
def __init__(self, url, apiKey=None):
1817
"""
1918
Parameters
@@ -23,12 +22,8 @@ def __init__(self, url, apiKey=None):
2322
apiKey : str
2423
The optional API key for MeiliSearch
2524
"""
26-
config = Config(url, apiKey)
27-
Health.__init__(self, config)
28-
Key.__init__(self, config)
29-
SysInfo.__init__(self, config)
30-
Version.__init__(self, config)
31-
self.config = config
25+
self.config = Config(url, apiKey)
26+
self.http = HttpRequests(self.config)
3227

3328
def create_index(self, uid, primary_key=None, name=None):
3429
"""Create an index.
@@ -53,8 +48,9 @@ def create_index(self, uid, primary_key=None, name=None):
5348
HTTPError
5449
In case of any other error found here https://docs.meilisearch.com/references/#errors-status-code
5550
"""
56-
index = Index.create(self.config, uid=uid, primary_key=primary_key, name=name)
57-
return Index(self.config, uid=index['uid'])
51+
index = Index(self.config, uid=uid)
52+
index.create(self.config, uid=uid, primary_key=primary_key, name=name)
53+
return index
5854

5955
def get_indexes(self):
6056
"""Get all indexes.
@@ -86,11 +82,82 @@ def get_index(self, uid):
8682
return Index.get_index(self.config, uid=uid)
8783

8884
def get_all_stats(self):
89-
"""Get statistics about indexes, database size and update date.
85+
"""Get all stats of MeiliSearch
9086
87+
Get information about database size and all indexes
88+
https://docs.meilisearch.com/references/stats.html
9189
Returns
92-
-------
93-
stats : dict
94-
Dictionnary with information about indexes, database size and update date.
90+
----------
91+
stats: `dict`
92+
Dictionnary containing stats about your MeiliSearch instance
93+
"""
94+
return self.http.get(self.config.paths.stat)
95+
96+
def health(self):
97+
"""Get health of MeiliSearch
98+
99+
`204` HTTP status response when MeiliSearch is healthy.
100+
101+
Raises
102+
----------
103+
HTTPError
104+
If MeiliSearch is not healthy
105+
"""
106+
return self.http.get(self.config.paths.health)
107+
108+
def update_health(self, health):
109+
"""Update health of meilisearch
110+
111+
Update health of MeiliSearch to true or false.
112+
113+
Parameters
114+
----------
115+
health: bool
116+
Boolean representing the health status of MeiliSearch. True for healthy.
117+
"""
118+
return self.http.put(self.config.paths.health, {'health': health})
119+
120+
def get_keys(self):
121+
"""Get all keys created
122+
123+
Get list of all the keys that were created and all their related information.
124+
125+
Returns
126+
----------
127+
keys: list
128+
List of keys and their information.
129+
https://docs.meilisearch.com/references/keys.html#get-keys
130+
"""
131+
return self.http.get(self.config.paths.keys)
132+
133+
def get_sys_info(self):
134+
"""Get system information of MeiliSearch
135+
136+
Get information about memory usage and processor usage.
137+
138+
Returns
139+
----------
140+
sys_info: dict
141+
Information about memory and processor usage.
142+
"""
143+
return self.http.get(self.config.paths.sys_info)
144+
145+
def get_version(self):
146+
"""Get version MeiliSearch
147+
148+
Returns
149+
----------
150+
version: dict
151+
Information about the version of MeiliSearch.
152+
"""
153+
return self.http.get(self.config.paths.version)
154+
155+
def version(self):
156+
"""Alias for get_version
157+
158+
Returns
159+
----------
160+
version: dict
161+
Information about the version of MeiliSearch.
95162
"""
96-
return Stat.get_all_stats(self.config)
163+
return self.get_version()

meilisearch/config.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@ class Config:
33
A client's credentials and configuration parameters
44
"""
55

6+
class Paths():
7+
health = "health"
8+
keys = 'keys'
9+
sys_info = 'sys-info'
10+
version = 'version'
11+
index = 'indexes'
12+
update = 'updates'
13+
stat = 'stats'
14+
search = 'search'
15+
document = 'documents'
16+
setting = 'settings'
17+
ranking_rules = 'ranking-rules'
18+
distinct_attribute = 'distinct-attribute'
19+
searchable_attributes = 'searchable-attributes'
20+
displayed_attributes = 'displayed-attributes'
21+
stop_words = 'stop-words'
22+
synonyms = 'synonyms'
23+
accept_new_fields = 'accept-new-fields'
24+
625
def __init__(self, url, apikey=None):
726
"""
827
Parameters
@@ -15,3 +34,4 @@ def __init__(self, url, apikey=None):
1534

1635
self.url = url
1736
self.apikey = apikey
37+
self.paths = self.Paths()

0 commit comments

Comments
 (0)