-
Notifications
You must be signed in to change notification settings - Fork 340
fix: Avoid sending request body with GET (issue #318) #538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -279,9 +279,16 @@ def __init__( | |
| self.always_commit = always_commit | ||
|
|
||
| def get_session(self): | ||
| """ | ||
| Returns a requests Session object to use for sending requests to Solr. | ||
|
|
||
| The session is created lazily on first call to this method, and is | ||
| reused for all subsequent requests. | ||
|
|
||
| :return: requests.Session instance | ||
| """ | ||
| if self.session is None: | ||
| self.session = requests.Session() | ||
| self.session.stream = False | ||
| self.session.verify = self.verify | ||
| return self.session | ||
|
|
||
|
|
@@ -1250,18 +1257,71 @@ class SolrCoreAdmin(object): | |
| 8. LOAD (not currently implemented) | ||
| """ | ||
|
|
||
| def __init__(self, url, *args, **kwargs): | ||
| super(SolrCoreAdmin, self).__init__(*args, **kwargs) | ||
| def __init__(self, url, timeout=60, auth=None, verify=True, session=None): | ||
| self.url = url | ||
| self.timeout = timeout | ||
| self.log = self._get_log() | ||
| self.auth = auth | ||
| self.verify = verify | ||
| self.session = session | ||
|
|
||
| def get_session(self): | ||
| """ | ||
| Returns a requests Session object to use for sending requests to Solr. | ||
|
|
||
| The session is created lazily on first call to this method, and is | ||
| reused for all subsequent requests. | ||
|
|
||
| :return: requests.Session instance | ||
| """ | ||
| if self.session is None: | ||
| self.session = requests.Session() | ||
| self.session.verify = self.verify | ||
| return self.session | ||
|
|
||
| def _get_url(self, url, params=None, headers=None): | ||
| def _get_log(self): | ||
| return LOG | ||
|
|
||
| def _send_request(self, url, params=None, headers=None): | ||
| """ | ||
| Internal method to send a GET request to Solr. | ||
|
|
||
| :param url: Full URL to query | ||
| :param params: Dictionary of query parameters | ||
| :param headers: Dictionary of HTTP headers | ||
| :return: JSON response from Solr | ||
| :raises SolrError: if the request fails or the JSON response cannot be decoded | ||
| """ | ||
| if params is None: | ||
| params = {} | ||
| if headers is None: | ||
| headers = {"Content-Type": "application/x-www-form-urlencoded"} | ||
| headers = {} | ||
|
|
||
| resp = requests.get(url, data=safe_urlencode(params), headers=headers) | ||
| return force_unicode(resp.content) | ||
| session = self.get_session() | ||
|
|
||
| self.log.debug( | ||
| "Starting Solr admin request to '%s' with params %s", | ||
| url, | ||
| params, | ||
| ) | ||
|
|
||
| try: | ||
| resp = session.get( | ||
| url, | ||
| params=params, | ||
| headers=headers, | ||
| auth=self.auth, | ||
| ) | ||
| resp.raise_for_status() | ||
| return resp.json() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @acdha The previous implementation also didn't have this feature. |
||
| except requests.exceptions.JSONDecodeError as e: | ||
| self.log.exception("Failed to decode JSON response from Solr at %s", url) | ||
| raise SolrError( | ||
| f"Failed to decode JSON response: {e}. Response text: {resp.text}" | ||
| ) | ||
| except requests.exceptions.RequestException as e: | ||
| self.log.exception("Request to Solr failed for URL %s", url) | ||
| raise SolrError(f"Request failed: {e}") | ||
|
|
||
| def status(self, core=None): | ||
| """ | ||
|
|
@@ -1274,7 +1334,7 @@ def status(self, core=None): | |
| if core is not None: | ||
| params.update(core=core) | ||
|
|
||
| return self._get_url(self.url, params=params) | ||
| return self._send_request(self.url, params=params) | ||
|
|
||
| def create( | ||
| self, name, instance_dir=None, config="solrconfig.xml", schema="schema.xml" | ||
|
|
@@ -1291,7 +1351,7 @@ def create( | |
| else: | ||
| params.update(instanceDir=instance_dir) | ||
|
|
||
| return self._get_url(self.url, params=params) | ||
| return self._send_request(self.url, params=params) | ||
|
|
||
| def reload(self, core): # NOQA: A003 | ||
| """ | ||
|
|
@@ -1300,7 +1360,7 @@ def reload(self, core): # NOQA: A003 | |
| See https://wiki.apache.org/solr/CoreAdmin#RELOAD | ||
| """ | ||
| params = {"action": "RELOAD", "core": core} | ||
| return self._get_url(self.url, params=params) | ||
| return self._send_request(self.url, params=params) | ||
|
|
||
| def rename(self, core, other): | ||
| """ | ||
|
|
@@ -1309,7 +1369,7 @@ def rename(self, core, other): | |
| See http://wiki.apache.org/solr/CoreAdmin#RENAME | ||
| """ | ||
| params = {"action": "RENAME", "core": core, "other": other} | ||
| return self._get_url(self.url, params=params) | ||
| return self._send_request(self.url, params=params) | ||
|
|
||
| def swap(self, core, other): | ||
| """ | ||
|
|
@@ -1318,7 +1378,7 @@ def swap(self, core, other): | |
| See http://wiki.apache.org/solr/CoreAdmin#SWAP | ||
| """ | ||
| params = {"action": "SWAP", "core": core, "other": other} | ||
| return self._get_url(self.url, params=params) | ||
| return self._send_request(self.url, params=params) | ||
|
|
||
| def unload(self, core): | ||
| """ | ||
|
|
@@ -1327,7 +1387,7 @@ def unload(self, core): | |
| See http://wiki.apache.org/solr/CoreAdmin#UNLOAD | ||
| """ | ||
| params = {"action": "UNLOAD", "core": core} | ||
| return self._get_url(self.url, params=params) | ||
| return self._send_request(self.url, params=params) | ||
|
|
||
| def load(self, core): | ||
| raise NotImplementedError("Solr 1.4 and below do not support this operation.") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix our tests to properly handle this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cclauss will do.