Skip to content

refacto: Inheritance refactor #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 27 additions & 46 deletions meilisearch/_httprequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,53 @@

class HttpRequests:

@staticmethod
def get(config, path):
config = None
headers = {}

def __init__(self, config):
self.config = config
self.headers = {
'X-Meili-Api-Key': self.config.apikey,
'Content-Type': 'application/json'
}


def get(self, path):
request = requests.get(
config.url + '/' + path,
headers={
'X-Meili-Api-Key': config.apikey,
'Content-Type': 'application/json'
}
self.config.url + '/' + path,
headers=self.headers,
)
return HttpRequests.__validate(request)
return self.__validate(request)

@staticmethod
def post(config, path, body=None):
def post(self, path, body=None):
if body is None:
body = {}
request = requests.post(
config.url + '/' + path,
headers={
'x-meili-api-key': config.apikey,
'content-type': 'application/json'
},
self.config.url + '/' + path,
headers=self.headers,
json=body
)
return HttpRequests.__validate(request)
return self.__validate(request)

@staticmethod
def put(config, path, body=None):
def put(self, path, body=None):
if body is None:
body = {}
request = requests.put(
config.url + '/' + path,
headers={
'x-meili-api-key': config.apikey,
'content-type': 'application/json'
},
self.config.url + '/' + path,
headers=self.headers,
json=body
)
return HttpRequests.__validate(request)
return self.__validate(request)

@staticmethod
def patch(config, path, body=None):
if body is None:
body = {}
request = requests.patch(
config.url + '/' + path,
headers={
'x-meili-api-key': config.apikey,
'content-type': 'application/json'
},
json=body
)
return HttpRequests.__validate(request)

@staticmethod
def delete(config, path, body=None):
def delete(self, path, body=None):
if body is None:
body = {}
request = requests.delete(
config.url + '/' + path,
headers={
'x-meili-api-key': config.apikey,
'content-type': 'application/json'
},
self.config.url + '/' + path,
headers=self.headers,
json=body
)
return HttpRequests.__validate(request)
return self.__validate(request)

@staticmethod
def __to_json(request):
Expand Down
105 changes: 86 additions & 19 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
from meilisearch.index import Index
from meilisearch.health import Health
from meilisearch.key import Key
from meilisearch.config import Config
from meilisearch.sys_info import SysInfo
from meilisearch.stat import Stat
from meilisearch.version import Version
from meilisearch._httprequests import HttpRequests

class Client(Health, Key, SysInfo, Version):
class Client():
"""
A client for the MeiliSearch API

A client instance is needed for every MeiliSearch API method to know the location of
MeiliSearch and its permissions.
"""

config = None
http = None

def __init__(self, url, apiKey=None):
"""
Parameters
Expand All @@ -23,12 +22,8 @@ def __init__(self, url, apiKey=None):
apiKey : str
The optional API key for MeiliSearch
"""
config = Config(url, apiKey)
Health.__init__(self, config)
Key.__init__(self, config)
SysInfo.__init__(self, config)
Version.__init__(self, config)
self.config = config
self.config = Config(url, apiKey)
self.http = HttpRequests(self.config)

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

def get_indexes(self):
"""Get all indexes.
Expand Down Expand Up @@ -86,11 +82,82 @@ def get_index(self, uid):
return Index.get_index(self.config, uid=uid)

def get_all_stats(self):
"""Get statistics about indexes, database size and update date.
"""Get all stats of MeiliSearch

Get information about database size and all indexes
https://docs.meilisearch.com/references/stats.html
Returns
-------
stats : dict
Dictionnary with information about indexes, database size and update date.
----------
stats: `dict`
Dictionnary containing stats about your MeiliSearch instance
"""
return self.http.get(self.config.paths.stat)

def health(self):
"""Get health of MeiliSearch

`204` HTTP status response when MeiliSearch is healthy.

Raises
----------
HTTPError
If MeiliSearch is not healthy
"""
return self.http.get(self.config.paths.health)

def update_health(self, health):
"""Update health of meilisearch

Update health of MeiliSearch to true or false.

Parameters
----------
health: bool
Boolean representing the health status of MeiliSearch. True for healthy.
"""
return self.http.put(self.config.paths.health, {'health': health})

def get_keys(self):
"""Get all keys created

Get list of all the keys that were created and all their related information.

Returns
----------
keys: list
List of keys and their information.
https://docs.meilisearch.com/references/keys.html#get-keys
"""
return self.http.get(self.config.paths.keys)

def get_sys_info(self):
"""Get system information of MeiliSearch

Get information about memory usage and processor usage.

Returns
----------
sys_info: dict
Information about memory and processor usage.
"""
return self.http.get(self.config.paths.sys_info)

def get_version(self):
"""Get version MeiliSearch

Returns
----------
version: dict
Information about the version of MeiliSearch.
"""
return self.http.get(self.config.paths.version)

def version(self):
"""Alias for get_version

Returns
----------
version: dict
Information about the version of MeiliSearch.
"""
return Stat.get_all_stats(self.config)
return self.get_version()
20 changes: 20 additions & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@ class Config:
A client's credentials and configuration parameters
"""

class Paths():
health = "health"
keys = 'keys'
sys_info = 'sys-info'
version = 'version'
index = 'indexes'
update = 'updates'
stat = 'stats'
search = 'search'
document = 'documents'
setting = 'settings'
ranking_rules = 'ranking-rules'
distinct_attribute = 'distinct-attribute'
searchable_attributes = 'searchable-attributes'
displayed_attributes = 'displayed-attributes'
stop_words = 'stop-words'
synonyms = 'synonyms'
accept_new_fields = 'accept-new-fields'

def __init__(self, url, apikey=None):
"""
Parameters
Expand All @@ -15,3 +34,4 @@ def __init__(self, url, apikey=None):

self.url = url
self.apikey = apikey
self.paths = self.Paths()
Loading