1+ # coding=utf-8
12"""
23Misc utility functions and constants
34
45"""
6+ import re
57import sys
68try :
79 from urllib .parse import quote
1012
1113from consulate import exceptions
1214
15+ DURATION_PATTERN = re .compile (r'^(?:(?:-|)(?:\d+|\d+\.\d+)(?:µs|ms|s|m|h))+$' )
1316PYTHON3 = True if sys .version_info > (3 , 0 , 0 ) else False
1417
1518
@@ -41,12 +44,27 @@ def maybe_encode(value):
4144
4245
4346def _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
4958def 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
0 commit comments