Skip to content

Commit 59ce825

Browse files
committed
Restore and deprecate client.tasks.get() without task_id
1 parent ae74e1d commit 59ce825

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

elasticsearch/client/tasks.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warnings
12
from .utils import NamespacedClient, query_params, _make_path, SKIP_IN_PATH
23

34

@@ -58,7 +59,7 @@ def cancel(self, task_id=None, params=None, headers=None):
5859
)
5960

6061
@query_params("timeout", "wait_for_completion")
61-
def get(self, task_id, params=None, headers=None):
62+
def get(self, task_id=None, params=None, headers=None):
6263
"""
6364
Returns information about a task.
6465
`<https://www.elastic.co/guide/en/elasticsearch/reference/master/tasks.html>`_
@@ -70,7 +71,11 @@ def get(self, task_id, params=None, headers=None):
7071
complete (default: false)
7172
"""
7273
if task_id in SKIP_IN_PATH:
73-
raise ValueError("Empty value passed for a required argument 'task_id'.")
74+
warnings.warn(
75+
"Calling client.tasks.get() without a task_id is deprecated "
76+
"and will be removed in v8.0. Use client.tasks.list() instead.",
77+
DeprecationWarning,
78+
)
7479

7580
return self.transport.perform_request(
7681
"GET", _make_path("_tasks", task_id), params=params, headers=headers

test_elasticsearch/test_client/__init__.py

+25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import unicode_literals
2+
import warnings
23

34
from elasticsearch.client import _normalize_hosts, Elasticsearch
45

@@ -110,3 +111,27 @@ def test_index_uses_put_if_id_is_not_empty(self):
110111
self.client.index(index="my-index", id=0, body={})
111112

112113
self.assert_url_called("PUT", "/my-index/_doc/0")
114+
115+
def test_tasks_get_without_task_id_deprecated(self):
116+
warnings.simplefilter("always", DeprecationWarning)
117+
with warnings.catch_warnings(record=True) as w:
118+
self.client.tasks.get()
119+
120+
self.assert_url_called("GET", "/_tasks")
121+
self.assertEquals(len(w), 1)
122+
self.assertIs(w[0].category, DeprecationWarning)
123+
self.assertEquals(
124+
str(w[0].message),
125+
"Calling client.tasks.get() without a task_id is deprecated "
126+
"and will be removed in v8.0. Use client.tasks.list() instead.",
127+
)
128+
129+
def test_tasks_get_with_task_id_not_deprecated(self):
130+
warnings.simplefilter("always", DeprecationWarning)
131+
with warnings.catch_warnings(record=True) as w:
132+
self.client.tasks.get("task-1")
133+
self.client.tasks.get(task_id="task-2")
134+
135+
self.assert_url_called("GET", "/_tasks/task-1")
136+
self.assert_url_called("GET", "/_tasks/task-2")
137+
self.assertEquals(len(w), 0)

utils/generate_api.py

+7
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ def all_parts(self):
139139
p in url.get("parts", {}) for url in self._def["url"]["paths"]
140140
)
141141

142+
# This piece of logic corresponds to calling
143+
# client.tasks.get() w/o a task_id which was erroneously
144+
# allowed in the 7.1 client library. This functionality
145+
# is deprecated and will be removed in 8.x.
146+
if self.namespace == "tasks" and self.name == "get":
147+
parts["task_id"]["required"] = False
148+
142149
for k, sub in SUBSTITUTIONS.items():
143150
if k in parts:
144151
parts[sub] = parts.pop(k)

utils/generate_examples.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import json
5+
import re
6+
from itertools import chain
7+
from subprocess import check_call
8+
9+
import black
10+
from click.testing import CliRunner
11+
from jinja2 import Environment, FileSystemLoader
12+
from pathlib import Path
13+
14+
15+
base_dir = Path(__file__).absolute().parent.parent
16+
asciidocs_dir = base_dir / "docs/asciidocs"
17+
flight_recorder_dir = base_dir.parent / "clients-flight-recorder"
18+
report_path = flight_recorder_dir / "recordings/docs/parsed-alternative-report.json"
19+
20+
asciidocs_template = """[source, python]
21+
----
22+
%s
23+
----
24+
"""
25+
26+
27+
def main():
28+
for filepath in asciidocs_dir.iterdir():
29+
if filepath.name.endswith(".asciidoc"):
30+
filepath.unlink()
31+
32+
if not flight_recorder_dir.exists() or not report_path.exists():
33+
raise RuntimeError(f"clients-flight-recorder repository not checked out at {flight_recorder_dir}")
34+
35+
with report_path.open() as f:
36+
report = json.loads(f.read())
37+
38+
39+
if __name__ == "__main__":
40+
main()

utils/templates/overrides/tasks/get

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% extends "base" %}
2+
{% block request %}
3+
if task_id in SKIP_IN_PATH:
4+
warnings.warn(
5+
"Calling client.tasks.get() without a task_id is deprecated "
6+
"and will be removed in v8.0. Use client.tasks.list() instead.",
7+
DeprecationWarning,
8+
)
9+
10+
{{ super()|trim }}
11+
{% endblock %}
12+

0 commit comments

Comments
 (0)