2019-11-21 12:15:34 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
import requests
|
|
|
|
import base64
|
|
|
|
|
|
|
|
class ElasticsearchApi:
|
|
|
|
def __init__(self, url, user, password):
|
|
|
|
self.url = url
|
2023-09-29 13:54:34 +00:00
|
|
|
self.basic = None
|
2019-11-21 12:15:34 +00:00
|
|
|
if user and password:
|
2023-09-29 13:54:34 +00:00
|
|
|
self.basic = requests.auth.HTTPBasicAuth(user, password)
|
2019-11-21 12:15:34 +00:00
|
|
|
|
|
|
|
def get(self, path):
|
|
|
|
r = requests.get(
|
|
|
|
'{}/{}'.format(self.url, path),
|
2023-09-29 13:54:34 +00:00
|
|
|
auth=self.basic
|
2019-11-21 12:15:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if r.status_code == 500:
|
|
|
|
raise Exception('Server return 500 error: {}'.format(r.text))
|
|
|
|
|
|
|
|
return r.status_code, r.json()
|
|
|
|
|
2023-10-17 07:47:10 +00:00
|
|
|
def post(self, path, data):
|
|
|
|
r = requests.post(
|
|
|
|
'{}/{}'.format(self.url, path),
|
|
|
|
auth=self.basic,
|
|
|
|
json=data
|
|
|
|
)
|
|
|
|
|
|
|
|
if r.status_code == 500:
|
|
|
|
raise Exception('Server return 500 error: {}'.format(r.text))
|
|
|
|
elif r.status_code == 401:
|
|
|
|
raise Exception('Authentification has failed')
|
|
|
|
elif r.status_code != 200:
|
|
|
|
raise Exception('Server return an unknown error: {}'.format(r.text))
|
|
|
|
|
2019-11-21 12:15:34 +00:00
|
|
|
def put(self, path, data):
|
|
|
|
r = requests.put(
|
|
|
|
'{}/{}'.format(self.url, path),
|
2023-09-29 13:54:34 +00:00
|
|
|
auth=self.basic,
|
2019-11-21 12:15:34 +00:00
|
|
|
json=data
|
|
|
|
)
|
|
|
|
|
|
|
|
if r.status_code == 500:
|
|
|
|
raise Exception('Server return 500 error: {}'.format(r.text))
|
|
|
|
elif r.status_code == 401:
|
|
|
|
raise Exception('Authentification has failed')
|
|
|
|
elif r.status_code != 200:
|
|
|
|
raise Exception('Server return an unknown error: {}'.format(r.text))
|