Skip to content

Commit

Permalink
feat: add methods to send api requests to OS (#70)
Browse files Browse the repository at this point in the history
* feat: add methods to send api requests to OS

* chore: set version for opensearch-py in setup.py

* chore: increase version opensearch-py

* style: improve styling

* chore: add pre-commit to requirements

* chore: update pre-commit config flake8

* chore: update python to 3.10

* chore: update python

* chore: update certifi

* chore: update urllib3

* chore: update urllib3

* chore: downgrade importlib-metadata

* chore: change revs in pre-commit yml

* fix: remove python styleguide make

* style: flake8

* style: flake8

* fix: replace curly bruckets in isinstance
  • Loading branch information
niek-mereu authored Jul 24, 2024
1 parent d5ae411 commit 01e7b2c
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 21 deletions.
13 changes: 5 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.0.1
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand All @@ -14,18 +11,18 @@ repos:
- id: debug-statements

- repo: https://github.com/psf/black
rev: 22.6.0
rev: 23.1.0
hooks:
- id: black

- repo: https://github.com/pycqa/isort
rev: 5.12.0
rev: 5.11.5
hooks:
- id: isort

- repo: https://github.com/pycqa/flake8
rev: 4.0.1
rev: 7.1.0
hooks:
- id: flake8
additional_dependencies:
- wemake-python-styleguide==0.16.1
- wemake-python-styleguide==0.18.0
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.8.12-buster AS python-base
FROM python:3.10.12-buster AS python-base
MAINTAINER STRV DS Department

RUN apt-get --allow-releaseinfo-change update && apt-get install -y unixodbc-dev
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ services:
# The following configuration is based on the OpenSearch documentation:
# https://opensearch.org/docs/latest/opensearch/install/docker/
opensearch-node:
image: opensearchproject/opensearch:2.3.0
image: opensearchproject/opensearch:2.15.0
container_name: opensearch-node
environment:
cluster.name: opensearch-cluster
Expand Down Expand Up @@ -87,7 +87,7 @@ services:
- opensearch-net

opensearch-dashboards:
image: opensearchproject/opensearch-dashboards:2.3.0
image: opensearchproject/opensearch-dashboards:2.15.0
container_name: opensearch-dashboards
ports:
- 5601:5601
Expand Down
130 changes: 126 additions & 4 deletions osman/osman.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,6 @@ def upload_search_template(

# if script exists in os, compare it with the local script
if script_os_res["found"]:

diffs = _compare_scripts(
json.dumps(source), script_os_res["script"]["source"]
)
Expand Down Expand Up @@ -486,7 +485,6 @@ def debug_search_template(
ids = [hit.get("_id") for hit in hits]

if expected_ids is not None:

assert set(ids) == set(expected_ids)

return hits
Expand Down Expand Up @@ -555,6 +553,34 @@ def upload_painless_script(self, source: dict, name: str) -> dict:
logging.info("Template updated!")
return res

def update_cluster_settings(self, settings: dict) -> dict:
"""
Update cluster settings.
Parameters
----------
settings: dict
A dictionary containing the cluster settings to update. This can include
'persistent' and 'transient' settings.
Returns
-------
dict
Dictionary with the response from the OpenSearch cluster.
Raises
------
RuntimeError
If the update fails or OpenSearch returns an error.
"""
try: # noqa: WPS229
response = self.client.cluster.put_settings(body=settings)
logging.info("Cluster settings updated successfully.")
return response
except exceptions.OpenSearchException as e:
logging.error("Failed to update cluster settings: %s", e)
raise RuntimeError(f"Failed to update cluster settings: {e}") from e

def debug_painless_script(
self,
source: dict,
Expand Down Expand Up @@ -590,13 +616,13 @@ def debug_painless_script(
dictionary with response
"""
if context_type == "score":
if type(expected_result) not in {float, int}:
if not isinstance(expected_result, (float, int)):
logging.warning(
"context_type 'score' requires 'expected_result' float or int"
)
return {"acknowledged": False}
elif context_type == "filter":
if type(expected_result) != bool:
if not isinstance(expected_result, (bool)):
logging.warning(
"context_type 'filter' requires 'expected_result' bool"
)
Expand Down Expand Up @@ -630,3 +656,99 @@ def debug_painless_script(
assert res["result"] == expected_result

return res

def send_post_request(self, endpoint: str, payload: dict) -> dict:
"""
Send a POST request to a specified endpoint in OpenSearch.
Parameters
----------
endpoint : str
The API endpoint to which the POST request will be sent.
payload : dict
The payload for the POST request, structured as a dictionary.
Returns
-------
dict
Dictionary containing the response from the OpenSearch server.
Raises
------
RuntimeError
If the POST request fails or OpenSearch returns an error.
"""
try: # noqa: WPS229
response = self.client.transport.perform_request(
"POST", endpoint, body=json.dumps(payload)
)
logging.info(f"POST request to {endpoint} successful.")
return response
except exceptions.OpenSearchException as e:
logging.error(f"Failed to send POST request to {endpoint}: {e}")
raise RuntimeError(
f"Failed to send POST request to {endpoint}: {e}"
) from e

def send_get_request(self, endpoint: str) -> dict:
"""
Send a GET request to a specified endpoint in OpenSearch.
Parameters
----------
endpoint : str
The API endpoint to which the GET request will be sent.
Returns
-------
dict
Dictionary containing the response from the OpenSearch server.
Raises
------
RuntimeError
If the GET request fails or OpenSearch returns an error.
"""
try: # noqa: WPS229
response = self.client.transport.perform_request("GET", endpoint)
logging.info(f"GET request to {endpoint} successful.")
return response
except exceptions.OpenSearchException as e:
logging.error(f"Failed to send GET request to {endpoint}: {e}")
raise RuntimeError(
f"Failed to send GET request to {endpoint}: {e}"
) from e

def send_put_request(self, endpoint: str, payload: dict) -> dict:
"""
Send a PUT request to a specified endpoint in OpenSearch.
Parameters
----------
endpoint : str
The API endpoint to which the PUT request will be sent.
payload : dict
The payload for the PUT request, structured as a dictionary.
Returns
-------
dict
Dictionary containing the response from the OpenSearch server.
Raises
------
RuntimeError
If the PUT request fails or OpenSearch returns an error.
"""
try: # noqa: WPS229
json_payload = json.dumps(payload)
response = self.client.transport.perform_request(
"PUT", endpoint, body=json_payload
)
logging.info(f"PUT request to {endpoint} successful.")
return response
except exceptions.OpenSearchException as e:
logging.error(f"Failed to send PUT request to {endpoint}: {e}")
raise RuntimeError(
f"Failed to send PUT request to {endpoint}: {e}"
) from e
8 changes: 5 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
attrs==22.1.0
certifi==2022.9.24
certifi==2024.7.4
charset-normalizer==2.1.1
click==8.1.3
idna==3.4
iniconfig==1.1.1
opensearch-py==2.0.0
opensearch-py==2.6.0
packaging==21.3
parameterized==0.8.1
pluggy==1.0.0
Expand All @@ -13,6 +13,8 @@ pyparsing==3.0.9
pytest==7.1.3
requests==2.28.1
tomli==2.0.1
urllib3==1.26.12
urllib3
requests-aws4auth==1.1.2
deepdiff==6.2.1
pre-commit==3.6.2
importlib-metadata==4.8.3
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"click>=8.1",
"idna>=3.4",
"iniconfig>=1.1",
"opensearch-py>=2.0",
"opensearch-py==2.6.0",
"packaging>=21.3",
"parameterized>=0.8.1",
"pluggy>=1.0",
Expand All @@ -48,6 +48,6 @@
"requests-aws4auth>=1.1",
"deepdiff>=6.2",
],
python_requires=">=3.9",
python_requires=">=3.10",
include_package_data=True,
)
1 change: 0 additions & 1 deletion tests/osman/test_osman.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ def test_template_comparison(

# when differences are present, test if correct
if "differences" in res:

assert (
str(res.get("differences", {}).get("dictionary_item_added"))
== expected_differences[0]
Expand Down

0 comments on commit 01e7b2c

Please sign in to comment.