-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.py
55 lines (41 loc) · 1.21 KB
/
lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#Self Made library by Neil Segard
import requests, json
class elastic():
def __init__(self, url):
self.url = url
def send(self, data):
headers = {'Content-Type': 'application/x-ndjson'}
try:
print("Sending ...")
res = requests.post(url=self.url, data=data, headers=headers)
status=res.status_code
if status!=200 :
print("Error:")
print(res.content.decode('ascii'))
return status
except:
print("Error making request")
return -1
def query(self, queryJson):
headers = {'Content-Type': 'application/json'}
data = json.dumps(queryJson)
try:
res = requests.get(url=self.url, data=data, headers=headers)
status=res.status_code
if status==200 :
response = res.content
response = response.decode('utf-8')
response = json.loads(response) #convert to json
print("Ok")
else:
response={}
print("Error with response")
return response, status
except:
print("Error making request")
return {},-1
def printResponse(response):
if response=={} :
print("No valid response to print")
else:
print(json.dumps(response, indent=2, sort_keys=True))