Skip to content

Commit a85433a

Browse files
author
Gianpaolo Caprara
committed
Fix package name and requirements
1 parent 8163188 commit a85433a

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Version of the RobotFrameworkElasticSearchLibrary package
2+
__version__ = "0.0.1"
3+
4+
from typing import Dict, Optional
5+
6+
from elasticsearch import Elasticsearch, NotFoundError
7+
from robot.api import logger
8+
from robot.api.deco import keyword
9+
from robot.utils import ConnectionCache
10+
11+
12+
class RobotFrameworkElasticSearchLibrary(object):
13+
14+
def __init__(self) -> None:
15+
self._connection: Optional[Elasticsearch] = None
16+
self._cache = ConnectionCache()
17+
18+
@property
19+
def connection(self) -> Elasticsearch:
20+
if self._connection is None:
21+
raise RuntimeError('There is no open connection to Elasticsearch.')
22+
return self._connection
23+
24+
@keyword("Connect Elasticsearch")
25+
def connect_elasticsearch(self, url: str, alias: str):
26+
try:
27+
self._connection = Elasticsearch([url])
28+
return self._cache.register(self._connection, alias=alias)
29+
except Exception as e:
30+
raise Exception(f'Connect to Elasticsearch error: {e}')
31+
32+
@keyword("Disconnect From Elasticsearch")
33+
def disconnect_from_elasticsearch(self, alias: str = None) -> None:
34+
if alias:
35+
self._connection = self._cache.switch(alias)
36+
self._connection = None
37+
self._connection = None
38+
39+
@keyword("Save Document To Elasticsearch")
40+
def save_document_to_elasticsearch(self, index: str, doc_type: str, _id: str, body: dict) -> None:
41+
self.connection.index(index=index, doc_type=doc_type, id=_id, body=body)
42+
43+
@keyword("Get Document By Id From Elasticsearch")
44+
def get_document_by_id_from_elasticsearch(self, index: str, doc_type: str, _id: str) -> Dict:
45+
try:
46+
data = self.connection.get(index=index, doc_type=doc_type, id=_id)
47+
return data
48+
except NotFoundError as e:
49+
logger.debug(f"Not found document from ElasticSearch with _id: {_id}")
50+
return {}
51+
except Exception as e:
52+
logger.debug(f"Exception {e} raised working with Elasticsearch on "
53+
f"{self.connection.host} and {self.connection.port}")
54+
raise
55+
56+
@keyword("Get Document By Filter From Elasticsearch")
57+
def get_document_by_filter_from_elasticsearch(self, index: str, body: str) -> Dict:
58+
results = self.search_from_elasticsearch(index=index, body=body)
59+
if results:
60+
return results[0]
61+
return {}
62+
63+
@keyword("Search From Elasticsearch")
64+
def search_from_elasticsearch(self, index: str, body: str) -> Dict:
65+
try:
66+
result = self.connection.search(index=index, body=body)
67+
return self._get_hits(result)
68+
except Exception as e:
69+
logger.debug(f"Exception {e} raised working with Elasticsearch on "
70+
f"{self.connection.host} and {self.connection.port}")
71+
raise
72+
73+
@keyword("Drop From Elasticsearch By Id")
74+
def drop_from_elasticsearch_by_id(self, index: str, doc_type: str, _id: str):
75+
try:
76+
return self.connection.delete(index=index, doc_type=doc_type, id=_id)
77+
except NotFoundError as e:
78+
return None
79+
except Exception as e:
80+
logger.debug(f"Exception {e} raised working with Elasticsearch on "
81+
f"{self.connection.host} and {self.connection.port}")
82+
raise
83+
84+
@keyword("Drop From Elasticsearch By Query")
85+
def drop_from_elasticsearch_by_query(self, index: str, body: str):
86+
try:
87+
return self.connection.delete_by_query(index=index, body=body)
88+
except NotFoundError as e:
89+
return None
90+
except Exception as e:
91+
logger.debug(f"Exception {e} raised working with Elasticsearch on "
92+
f"{self.connection.host} and {self.connection.port}")
93+
raise
94+
95+
@keyword("Create Term Query Filter")
96+
def create_term_query_filter(self, field: str, value: str) -> dict:
97+
query = {
98+
"query": self.create_term_filter(field=field, value=value)
99+
}
100+
return query
101+
102+
@keyword("Create Must Query Filter")
103+
def create_must_query_filter(self, dictionary: dict) -> dict:
104+
_must = []
105+
for key, value in dictionary.items():
106+
_must.append(self.create_term_filter(field=key, value=value))
107+
query = {
108+
"query": {
109+
"bool": {
110+
"must": _must
111+
}
112+
}
113+
}
114+
return query
115+
116+
@keyword("Create Term Filter")
117+
def create_term_filter(self, field: str, value: str):
118+
return {
119+
"term": {
120+
f'{field}.keyword': value
121+
}
122+
}
123+
124+
@classmethod
125+
def _get_hits(cls, response: dict):
126+
if response:
127+
hits = response.get('hits', {})
128+
if hits:
129+
return hits.get('hits', [])
130+
return []

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
robotframework==3.1.2
2+
elasticsearch==6.4.0

robotframework-elasticsearch/__init__.py

Whitespace-only changes.

setup.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pathlib
2+
from setuptools import setup
3+
4+
# The directory containing this file
5+
HERE = pathlib.Path(__file__).parent
6+
7+
# The text of the README file
8+
README = (HERE / "README.md").read_text()
9+
10+
with open('requirements.txt') as f:
11+
requirements = f.read().splitlines()
12+
13+
# This call to setup() does all the work
14+
setup(
15+
name="RobotFrameworkElasticSearchLibrary",
16+
version="0.0.1",
17+
description="Robot Framework ElasticSearch library.",
18+
long_description=README,
19+
long_description_content_type="text/markdown",
20+
url="https://github.com/gianpaolocaprara/robotframework-elasticsearch",
21+
author="Gianpaolo Caprara",
22+
author_email="gianpaolo.caprara@gmail.com",
23+
license="MIT",
24+
classifiers=[
25+
"License :: OSI Approved :: MIT License",
26+
"Programming Language :: Python :: 3",
27+
"Programming Language :: Python :: 3.7",
28+
],
29+
packages=["RobotFrameworkElasticSearchLibrary"],
30+
include_package_data=True,
31+
install_requires=requirements,
32+
)

0 commit comments

Comments
 (0)