-
Notifications
You must be signed in to change notification settings - Fork 385
Adding helper functions for convenient PUT and PATCH on SDK #586
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: develop
Are you sure you want to change the base?
Conversation
|
Dismissed as stale. Please tell us if the issue persists! |
|
This is still very much sought after by my team. Let me know if you need anything for this to be merged. |
|
Thanks @seiimonn, we'll take a look and get back to you! |
|
Hi @Ickerday - anything I can do to support on this? |
|
Hi We plan to give this a look, we are going to figure out which way is the best to implement this (we might go with this change) but in the meantime can you rebase this on top of the develop branch? |
|
Thanks, once again i would ask you to rebase this, since i would like to see this being also tested with the tests i have introduced for #683. Please add in |
| message = {"method": method, "headers": all_headers} | ||
| path = path + UrlEncoded('?' + body, skip_encode=True) | ||
| message = {'method': method, | ||
| 'headers': all_headers} |
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.
Drop that change. It seems to only make the code non-formatted.
| **Example**:: | ||
| c = binding.connect(...) | ||
| c.post('saved/searches', name='boris', | ||
| search='search * earliest=-1m | head 1') == \\ | ||
| {'body': ...a response reader object..., | ||
| 'headers': [('content-length', '10455'), | ||
| ('expires', 'Fri, 30 Oct 1998 00:00:00 GMT'), | ||
| ('server', 'Splunkd'), | ||
| ('connection', 'close'), | ||
| ('cache-control', 'no-store, max-age=0, must-revalidate, no-cache'), | ||
| ('date', 'Fri, 11 May 2012 16:46:06 GMT'), | ||
| ('content-type', 'text/xml; charset=utf-8')], | ||
| 'reason': 'Created', | ||
| 'status': 201} | ||
| c.post('nonexistant/path') # raises HTTPError | ||
| c.logout() | ||
| # raises AuthenticationError: | ||
| c.put('saved/searches/boris', | ||
| search='search * earliest=-1m | head 1') | ||
| """ |
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.
I think we should change this example, splunk does not really support PUT/PATCH in its endpoints see: https://help.splunk.com/en/splunk-enterprise/leverage-rest-apis/rest-api-reference/10.0/introduction/endpoints-reference-list (that is the reason there were no such methods in the first place)
I think we should have an example that shows that it is used with custom rest endpoints, something like:
# Call an HTTP endpoint, exposed as Custom Rest Endpoint in a Splunk App.
# PUT /servicesNS/-/app_name/custom_rest_endpoint
service.put(
app="app_name",
path_segment="custom_rest_endpoint",
body=json.dumps({"key": "val"}),
headers=[("Content-Type", "application/json")],
)| c = binding.connect(...) | ||
| c.post('saved/searches', name='boris', | ||
| search='search * earliest=-1m | head 1') == \\ | ||
| {'body': ...a response reader object..., | ||
| 'headers': [('content-length', '10455'), | ||
| ('expires', 'Fri, 30 Oct 1998 00:00:00 GMT'), | ||
| ('server', 'Splunkd'), | ||
| ('connection', 'close'), | ||
| ('cache-control', 'no-store, max-age=0, must-revalidate, no-cache'), | ||
| ('date', 'Fri, 11 May 2012 16:46:06 GMT'), | ||
| ('content-type', 'text/xml; charset=utf-8')], | ||
| 'reason': 'Created', | ||
| 'status': 201} | ||
| c.post('nonexistant/path') # raises HTTPError | ||
| c.logout() | ||
| # raises AuthenticationError: | ||
| c.patch('saved/searches/boris', | ||
| search='search * earliest=-1m | head 1') |
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.
Same here, lets change it, as above.
| :type headers: ``list`` of 2-tuples. | ||
| :param query: All other keyword arguments, which are used as query | ||
| parameters. | ||
| :param body: Parameters to be used in the post body. If specified, |
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.
| :param body: Parameters to be used in the post body. If specified, | |
| :param body: Parameters to be used in the put body. If specified, |
| :type headers: ``list`` of 2-tuples. | ||
| :param query: All other keyword arguments, which are used as query | ||
| parameters. | ||
| :param body: Parameters to be used in the post body. If specified, |
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.
| :param body: Parameters to be used in the post body. If specified, | |
| :param body: Parameters to be used in the patch body. If specified, |
| Some of Splunk's endpoints, such as ``receivers/simple`` and | ||
| ``receivers/stream``, require unstructured data in the PUT body | ||
| and all metadata passed as GET-style arguments. If you provide | ||
| a ``body`` argument to ``put``, it will be used as the PUT | ||
| body, and all other keyword arguments will be passed as | ||
| GET-style arguments in the URL. |
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.
Splunk does not support PUT method in its endpoints, so lets not mention that.
| Some of Splunk's endpoints, such as ``receivers/simple`` and | |
| ``receivers/stream``, require unstructured data in the PUT body | |
| and all metadata passed as GET-style arguments. If you provide | |
| a ``body`` argument to ``put``, it will be used as the PUT | |
| body, and all other keyword arguments will be passed as | |
| GET-style arguments in the URL. | |
| If you provide a ``body`` argument to ``put``, | |
| it will be used as the PUT body, and all other | |
| keyword arguments will be passed as GET-style | |
| arguments in the URL. |
| Some of Splunk's endpoints, such as ``receivers/simple`` and | ||
| ``receivers/stream``, require unstructured data in the PATCH body | ||
| and all metadata passed as GET-style arguments. If you provide | ||
| a ``body`` argument to ``patch``, it will be used as the PATCH | ||
| body, and all other keyword arguments will be passed as | ||
| GET-style arguments in the URL. |
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.
Same here.
The current SDK does not provide dedicated functions for sending PUT and PATCH requests to endpoints in Splunk. These operations were only possible using the generic request method, which had issues correctly formatting a body payload, especially for form-encoded data.
This change introduces helper functions for PUT and POST requests, along with a new _prepare_request_body_and_url function to streamline body and URL preparation logic. This ensures consistency, reduces redundancy, and resolves issues with incorrect payload formatting when making these requests.
Changes
Benefits