request-boost icon indicating copy to clipboard operation
request-boost copied to clipboard

Added param response_process (callback) to process response object

Open sohang3112 opened this issue 2 years ago • 20 comments

Requests to some urls return binary data which cannot be decoded (eg. image data). In these cases, param parse_bytes can be set to False.

sohang3112 avatar Feb 15 '23 08:02 sohang3112

@singhsidhukuldeep Please review my pull request, and let me know if something can be improved. Thanks in advance 🙂

sohang3112 avatar Aug 06 '23 07:08 sohang3112

Please resolve conflicts

singhsidhukuldeep avatar Aug 08 '23 03:08 singhsidhukuldeep

@singhsidhukuldeep Resolved the conflicts - they were because of the commits in main branch since February.

sohang3112 avatar Aug 09 '23 02:08 sohang3112

@singhsidhukuldeep Did you have a chance to review the PR yet?

sohang3112 avatar Aug 30 '23 04:08 sohang3112

I don't think there is any need for this, you can set parse_json as False

This will give you the raw response and later you can process it as you want

singhsidhukuldeep avatar Aug 30 '23 09:08 singhsidhukuldeep

you can set parse_json as False

@singhsidhukuldeep Setting parse_json to False does not help, because the request bytes are always decoded irrespective of parse_json. In the following code from __init__.py:

data = response.read()
encoding = response.info().get_content_charset("utf-8")
decoded_data = data.decode(encoding)                      # THE BYTES ARE ALWAYS DECODED TO STRING FIRST
self.results[loc] = (
    json.loads(
        decoded_data) if self.parse_json else decoded_data
)
self.queue.task_done()

Decoding the bytes is always done (data.decode(encoding)). This is not desirable for binary files like images, videos, etc.

sohang3112 avatar Aug 30 '23 09:08 sohang3112

Makes sense, let's make it parse JSON or parse nothing and return the Request object itself

singhsidhukuldeep avatar Aug 30 '23 09:08 singhsidhukuldeep

Makes sense, let's make it parse JSON or parse nothing and return the Request object itself

That sounds good. But this will break compatibility for existing users - when parse_json=False, they would expect the response text, but would instead get the response object.

sohang3112 avatar Aug 30 '23 11:08 sohang3112

@sohang3112 need to rethink this; people will start adding specific post processes for their own use cases

We need to keep it light-weight and extendable

one idea is to allow the user to pass their own post-processing logic as a callable function (post_process_func), We will pass the request object through that, this can overwrite parse_json when not None

singhsidhukuldeep avatar Sep 02 '23 20:09 singhsidhukuldeep

@singhsidhukuldeep Yes this sounds like a good idea - if a callable is passed, then pass it the request object and use its result. Otherwise do existing processing.

sohang3112 avatar Sep 03 '23 01:09 sohang3112

Sorry closed accidentally 😞 Re-opening..

sohang3112 avatar Sep 03 '23 01:09 sohang3112

if a callable is passed, then pass it the request object and use its result. Otherwise do existing processing.

@singhsidhukuldeep Should I modify my PR to do this??

sohang3112 avatar Sep 12 '23 10:09 sohang3112

Yes please

On Tue, 12 Sept 2023, 4:11 pm Sohang Chopra, @.***> wrote:

if a callable is passed, then pass it the request object and use its result. Otherwise do existing processing.

@singhsidhukuldeep https://github.com/singhsidhukuldeep Should I modify my PR to do this??

— Reply to this email directly, view it on GitHub https://github.com/singhsidhukuldeep/request-boost/pull/10#issuecomment-1715480680, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACOBEA4PXPSSAGTLFVJWJOLX2A36PANCNFSM6AAAAAAU4Q2YRI . You are receiving this because you were mentioned.Message ID: @.***>

singhsidhukuldeep avatar Sep 12 '23 10:09 singhsidhukuldeep

@singhsidhukuldeep I updated the PR to accept optional callback response_process which is used like this:

response_ok, data = response_process(response)

data is used if response_ok is true, otherwise request will be retried.

sohang3112 avatar Sep 13 '23 02:09 sohang3112

@singhsidhukuldeep Please review and merge my PR. Let me know if any further changes are needed.

sohang3112 avatar Sep 24 '23 08:09 sohang3112

@singhsidhukuldeep Please merge my PR. Let me know if any further changes are required from my side.

sohang3112 avatar Oct 19 '23 14:10 sohang3112

@singhsidhukuldeep Did you have a chance to review the PR yet? As I mentioned previously, I had modified the PR to include the changes we discussed.

sohang3112 avatar Nov 27 '23 14:11 sohang3112

@singhsidhukuldeep Hi. Did you have a look at the PR yet? As I mentioned before, I modified the PR to include the changes we discussed.

sohang3112 avatar Dec 04 '23 13:12 sohang3112

Hi @sohang3112 I am occupied with few things, will close this shortly

singhsidhukuldeep avatar Dec 13 '23 09:12 singhsidhukuldeep

@singhsidhukuldeep Please review & merge this PR now.

sohang3112 avatar May 21 '24 02:05 sohang3112