docker-py
docker-py copied to clipboard
Handle custom headers in post request
Issue
As of now, options such as stream, or timeout are not passed to the call to the docker API.
Cause
These options are circulated through the headers of the http query to the docker API. But recent modifications of the requests library state that adding custom headers to an http query should be handled by the code inheriting from "". See:
https://github.com/psf/requests/blob/786255613bd92f87c9c8f066c4271aab1b9eeaad/requests/adapters.py#L401-L413
Solution
Therefore, I think that a add_headers methods should be added to the UnixHTTPAdapter.
Here is my (particularly ugly) workaround:
def add_headers(self, request, **kwargs):
from requests.structures import CaseInsensitiveDict
new_head = dict(request.headers)
for key, val in kwargs.items():
new_head[key] = str(val)
request.headers = CaseInsensitiveDict(new_head)
cli = docker.APIClient()
if hasattr(cli, '_transport_adapter'):
cli._transport_adapter.add_headers = add_headers