mirror of https://github.com/docker/docker-py.git
Add events endpoint to the Client
This is a bit of a hack; I added a method (and did a slight refactor of the old one) to handle getting the raw socket from the requests request. If the attach_sockets method isn't public, I'd suggest removing it entirely for a slightly more general one. This also needs some attention paid to validating legnth, and properly parsing the returned data by using the length. Signed-off-by: Paul Tagliamonte <tag@pault.ag>
This commit is contained in:
parent
3aa51d52d0
commit
9a98e7fc8c
|
|
@ -142,6 +142,19 @@ class Client(requests.Session):
|
||||||
kwargs['headers']['Content-Type'] = 'application/json'
|
kwargs['headers']['Content-Type'] = 'application/json'
|
||||||
return self.post(url, json.dumps(data2), **kwargs)
|
return self.post(url, json.dumps(data2), **kwargs)
|
||||||
|
|
||||||
|
def _socket_connection(self, url, method='post', *args, **kwargs):
|
||||||
|
try:
|
||||||
|
handler = {
|
||||||
|
"post": self.post,
|
||||||
|
"get": self.get
|
||||||
|
}[method.lower()]
|
||||||
|
except KeyError:
|
||||||
|
raise KeyError("No such method: `%s`" % (method))
|
||||||
|
|
||||||
|
res = handler(url, *args, **kwargs)
|
||||||
|
self._raise_for_status(res)
|
||||||
|
return res.raw._fp.fp._sock
|
||||||
|
|
||||||
def attach_socket(self, container, params=None):
|
def attach_socket(self, container, params=None):
|
||||||
if params is None:
|
if params is None:
|
||||||
params = {
|
params = {
|
||||||
|
|
@ -153,12 +166,23 @@ class Client(requests.Session):
|
||||||
container = container.get('Id')
|
container = container.get('Id')
|
||||||
|
|
||||||
u = self._url("/containers/{0}/attach".format(container))
|
u = self._url("/containers/{0}/attach".format(container))
|
||||||
res = self.post(u, None, params=params, stream=True)
|
return self._socket_connection(
|
||||||
self._raise_for_status(res)
|
u, None, method='post', params=params, stream=True)
|
||||||
# hijack the underlying socket from requests, icky
|
|
||||||
# but for some reason requests.iter_contents and ilk
|
def events(self):
|
||||||
# eventually block
|
u = self._url("/events")
|
||||||
return res.raw._fp.fp._sock
|
|
||||||
|
socket = self._socket_connection(u, method='get', stream=True)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
chunk = socket.recv(4096)
|
||||||
|
if chunk:
|
||||||
|
# Messages come in the format of length, data, newline.
|
||||||
|
length, data = chunk.split("\n", 1)
|
||||||
|
# XXX: Verify data length.
|
||||||
|
yield json.loads(data)
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
def attach(self, container):
|
def attach(self, container):
|
||||||
socket = self.attach_socket(container)
|
socket = self.attach_socket(container)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue