5 http = urllib3.PoolManager()
8 def _request(method, url, creds=None, body=None):
9 headers = urllib3.make_headers(basic_auth=creds)
10 return http.request(method, url, headers=headers, body=body)
13 def _post(url, creds=None, body=None):
14 return _request('POST', url, creds=creds, body=body)
17 def _get(url, creds=None, body=None):
18 return json.loads(_request('GET', url, creds=creds, body=body).data)
21 def delete_docs(url, creds=None, body=None):
22 return _request('DELETE', url, creds=creds, body=body)
25 def publish_docs(docs, creds, to):
26 json_docs = json.dumps(docs)
31 result = _post(to, creds=creds, body=json_docs)
32 return result.status, result.data
35 def _get_docs_nr(url, creds=None, body=None):
36 res_data = _get('{}/_search?size=0'.format(url), creds=creds, body=body)
37 print type(res_data), res_data
38 return res_data['hits']['total']
41 def get_docs(url, creds=None, body=None, field='_source'):
43 docs_nr = _get_docs_nr(url, creds=creds, body=body)
44 res_data = _get('{}/_search?size={}'.format(url, docs_nr),
45 creds=creds, body=body)
48 for hit in res_data['hits']['hits']:
49 docs.append(hit[field])