Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[snapshot]
  • Loading branch information
Jean-Baptiste Besselat
Jean-Baptiste Besselat committed Sep 23, 2019
commit 081bdc0fe835ebe6648aaf881faa5c96c598a7cd
3 changes: 2 additions & 1 deletion consul/aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ def __init__(self, *args, loop=None, **kwargs):
def _request(self, callback, method, uri, data=None):
resp = yield from self._session.request(method, uri, data=data)
body = yield from resp.text(encoding='utf-8')
content = yield from resp.read()
if resp.status == 599:
raise base.Timeout
r = base.Response(resp.status, resp.headers, body)
r = base.Response(resp.status, resp.headers, body, content)
return callback(r)

# python prior 3.4.1 does not play nice with __del__ method
Expand Down
32 changes: 31 additions & 1 deletion consul/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def _compat(
return ret


Response = collections.namedtuple('Response', ['code', 'headers', 'body'])
Response = collections.namedtuple('Response', ['code', 'headers', 'body', 'content'])


#
Expand Down Expand Up @@ -244,6 +244,16 @@ def cb(response):
return data
return cb

@classmethod
def binary(klass):
"""
This method simply returns response body, usefull for snapshot
"""
def cb(response):
CB._status(response)
return response.content
return cb


class HTTPClient(six.with_metaclass(abc.ABCMeta, object)):
def __init__(self, host='127.0.0.1', port=8500, scheme='http',
Expand Down Expand Up @@ -343,6 +353,7 @@ def __init__(
self.query = Consul.Query(self)
self.coordinate = Consul.Coordinate(self)
self.operator = Consul.Operator(self)
self.snapshot = Consul.Snapshot(self)

class Event(object):
"""
Expand Down Expand Up @@ -2434,3 +2445,22 @@ def raft_config(self):
"""
return self.agent.http.get(
CB.json(), '/v1/operator/raft/configuration')

class Snapshot(object):
def __init__(self,agent):
self.agent = agent

def get(self):
"""
Returns gzipped snapshot of current consul cluster
"""
return self.agent.http.get(
CB.binary(),'/v1/snapshot')

def save(self, file_path):
"""
Backup snapshot in a file
"""
backup_file = open(file_path, 'w+b')
backup_file.write(self.get())
backup_file.close()
2 changes: 1 addition & 1 deletion consul/std.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, *args, **kwargs):
def response(self, response):
response.encoding = 'utf-8'
return base.Response(
response.status_code, response.headers, response.text)
response.status_code, response.headers, response.text, response.content)

def get(self, callback, path, params=None):
uri = self.uri(path, params)
Expand Down
2 changes: 1 addition & 1 deletion consul/tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, *args, **kwargs):

def response(self, response):
return base.Response(
response.code, response.headers, response.body.decode('utf-8'))
response.code, response.headers, response.body.decode('utf-8'), response.body)

@gen.coroutine
def _request(self, callback, request):
Expand Down
7 changes: 4 additions & 3 deletions consul/twisted.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def __init__(self, contextFactory, *args, **kwargs):
self.client = TreqHTTPClient(Agent(**agent_kwargs))

@staticmethod
def response(code, headers, text):
return base.Response(code, headers, text)
def response(code, headers, text, content):
return base.Response(code, headers, text, content)

@staticmethod
def compat_string(value):
Expand All @@ -70,7 +70,8 @@ def _get_resp(self, response):
for k, v in dict(response.headers.getAllRawHeaders()).items()
])
body = yield response.text(encoding='utf-8')
returnValue((response.code, headers, body))
content = yield response.content()
returnValue((response.code, headers, body, content))

@inlineCallbacks
def request(self, callback, method, url, **kwargs):
Expand Down