Better UX - similar to python-etcd3

This commit is contained in:
Davanum Srinivas 2017-03-28 15:48:34 -04:00
parent 83f9dfa0cc
commit 2039bfc01c
5 changed files with 40 additions and 8 deletions

View File

@ -12,7 +12,22 @@
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import absolute_import
import pbr.version
from etcd3gw.client import client
from etcd3gw.client import Etcd3Client
from etcd3gw.lease import Lease
from etcd3gw.lock import Lock
from etcd3gw import utils
__version__ = pbr.version.VersionInfo(
'etcd3gw').version_string()
__all__ = (
'Etcd3Client',
'Lease',
'Lock',
'client',
'utils',
)

View File

@ -39,8 +39,9 @@ _EXCEPTIONS_BY_CODE = {
}
class Client(object):
def __init__(self, host="localhost", port=2379, protocol="http"):
class Etcd3Client(object):
def __init__(self, host='localhost', port=2379, protocol="http",
ca_cert=None, cert_key=None, cert_cert=None, timeout=None):
"""Construct an client to talk to etcd3's grpc-gateway's /v3alpha HTTP API
:param host:
@ -51,6 +52,11 @@ class Client(object):
self.port = port
self.protocol = protocol
self.session = requests.Session()
self.kwargs = {
"timeout": timeout,
"verify": ca_cert,
"cert": (cert_cert, cert_key)
}
def get_url(self, path):
"""Construct a full url to the v3alpha API given a specific path
@ -341,3 +347,14 @@ class Client(object):
kwargs['range_end'] = \
_increment_last_byte(_encode(key_prefix))
return self.watch_once(key_prefix, timeout=timeout, **kwargs)
def client(host='localhost', port=2379,
ca_cert=None, cert_key=None, cert_cert=None, timeout=None):
"""Return an instance of an Etcd3Client."""
return Etcd3Client(host=host,
port=port,
ca_cert=ca_cert,
cert_key=cert_key,
cert_cert=cert_cert,
timeout=timeout)

View File

@ -12,12 +12,12 @@
import time
from etcd3gw.client import Client
from etcd3gw.client import Etcd3Client
from etcd3gw.lock import Lock
def main():
client = Client()
client = Etcd3Client()
print('>>>> Status')
result = client.status()

View File

@ -10,11 +10,11 @@
# License for the specific language governing permissions and limitations
# under the License.
from etcd3gw.client import Client
from etcd3gw.client import Etcd3Client
def main():
client = Client()
client = Etcd3Client()
events, cancel = client.watch('foo')
for event in events:
print(">>>> event : %r", event)

View File

@ -24,7 +24,7 @@ import uuid
from testtools.testcase import unittest
import urllib3
from etcd3gw.client import Client
from etcd3gw.client import Etcd3Client
from etcd3gw import exceptions
from etcd3gw.tests import base
@ -40,7 +40,7 @@ def _is_etcd3_running():
class TestEtcd3Gateway(base.TestCase):
@classmethod
def setUpClass(cls):
cls.client = Client()
cls.client = Etcd3Client()
@unittest.skipUnless(
_is_etcd3_running(), "etcd3 is not available")