Skip to content

Commit 6f30098

Browse files
committed
Add go duration parsing
1 parent fd58876 commit 6f30098

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

consulate/utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
# coding=utf-8
12
"""
23
Misc utility functions and constants
34
45
"""
6+
import re
57
import sys
68
try:
79
from urllib.parse import quote
@@ -10,6 +12,7 @@
1012

1113
from consulate import exceptions
1214

15+
DURATION_PATTERN = re.compile(r'^(?:(?:-|)(?:\d+|\d+\.\d+)(?:µs|ms|s|m|h))+$')
1316
PYTHON3 = True if sys.version_info > (3, 0, 0) else False
1417

1518

@@ -41,12 +44,27 @@ def maybe_encode(value):
4144

4245

4346
def _response_error(response):
47+
"""Return the decoded response error or status code if no content exists.
48+
49+
:param requests.response response: The HTTP response
50+
:rtype: str
51+
52+
"""
4453
return (response.body.decode('utf-8')
4554
if hasattr(response, 'body') and response.body
4655
else str(response.status_code))
4756

4857

4958
def response_ok(response, raise_on_404=False):
59+
"""Evaluate the HTTP response and raise the appropriate exception if
60+
required.
61+
62+
:param requests.response response: The HTTP response
63+
:param bool raise_on_404: Raise an exception on 404 error
64+
:rtype: bool
65+
:raises: consulate.exceptions.ConsulateException
66+
67+
"""
5068
if response.status_code == 200:
5169
return True
5270
elif response.status_code == 400:
@@ -60,3 +78,14 @@ def response_ok(response, raise_on_404=False):
6078
elif response.status_code == 500:
6179
raise exceptions.ServerError(_response_error(response))
6280
return False
81+
82+
83+
def validate_go_interval(value):
84+
"""Validate the value passed in returning :data:`True` if it is a Go
85+
Duration value.
86+
87+
:param str value: The string to check
88+
:rtype: bool
89+
90+
"""
91+
return DURATION_PATTERN.match(value) is not None

tests/utils_tests.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1+
# coding=utf-8
12
import unittest
23

34
from consulate import utils
45

56

6-
class TestQuote(unittest.TestCase):
7+
class QuoteTestCase(unittest.TestCase):
78
def urlencode_test(self):
89
self.assertEqual("%2Ffoo%40bar", utils.quote("/foo@bar", ""))
910

1011

11-
class TestMaybeEncode(unittest.TestCase):
12+
class MaybeEncodeTestCase(unittest.TestCase):
1213
@unittest.skipUnless(utils.PYTHON3, 'Python3 Only')
1314
def str_test(self):
1415
self.assertEqual(utils.maybe_encode('foo'), b'foo')
1516

1617
@unittest.skipUnless(utils.PYTHON3, 'Python3 Only')
1718
def byte_test(self):
1819
self.assertEqual(utils.maybe_encode(b'bar'), b'bar')
20+
21+
22+
class ValidateGoDurationTestCase(unittest.TestCase):
23+
24+
def test_valid_values(self):
25+
for value in {'5µs', '300ms', '-1.5h', '2h45m', '5m', '30s'}:
26+
print('Testing {}'.format(value))
27+
self.assertTrue(utils.validate_go_interval(value))
28+
29+
def test_invalid_values(self):
30+
for value in {'100', '1 year', '5M', '30S'}:
31+
print('Testing {}'.format(value))
32+
self.assertFalse(utils.validate_go_interval(value))

0 commit comments

Comments
 (0)