CodingAleCR / http_interceptor

A lightweight, simple plugin that allows you to intercept request and response objects and modify them if desired.
MIT License
133 stars 67 forks source link

How to use with WebSockets #125

Closed matiasmarcodelpont closed 1 year ago

matiasmarcodelpont commented 1 year ago

My http client authenticates with the server using a bearer token sent on an Authentication header. I have implemented an interceptor and a retry policy to handle the logic when the token must be refreshed.

Now I want to use te same InterceptedClient to create a WebSocket with the same server. The request uses the same Authentication header and must be updated in the same manner.

Is this possible?

CodingAleCR commented 1 year ago

I don't think it is possible to do so since HTTP (https://) and WebSocket (ws://) are two different protocols.

WebSocket is a framed and bidirectional protocol. On the contrary, to this, HTTP is a unidirectional protocol functioning above the TCP protocol.

That said, this package is built on top of http which was not thought for handling websockets. I think your best approach might be to abstract the behavior for token usage to a separate class and use/reuse the same instance in both http and websocket calls or try to find an alternative and more robust package that handles both scenarios (sad but true 😞 )