diff options
Diffstat (limited to 'admin/Sources/Clients/ogagent/src/opengnsys/RESTApi.py')
-rw-r--r-- | admin/Sources/Clients/ogagent/src/opengnsys/RESTApi.py | 59 |
1 files changed, 33 insertions, 26 deletions
diff --git a/admin/Sources/Clients/ogagent/src/opengnsys/RESTApi.py b/admin/Sources/Clients/ogagent/src/opengnsys/RESTApi.py index 5caaf8c4..d785dfa7 100644 --- a/admin/Sources/Clients/ogagent/src/opengnsys/RESTApi.py +++ b/admin/Sources/Clients/ogagent/src/opengnsys/RESTApi.py @@ -26,9 +26,9 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -''' +""" @author: Adolfo Gómez, dkmaster at dkmon dot com -''' +""" # pylint: disable-msg=E1101,W0703 @@ -43,7 +43,8 @@ from .log import logger from .utils import exceptionToMessage -VERIFY_CERT = False +VERIFY_CERT = False # Do not check server certificate +TIMEOUT = 5 # Connection timout, in seconds class RESTError(Exception): @@ -66,30 +67,34 @@ try: except Exception: pass # In fact, isn't too important, but wil log warns to logging file + class REST(object): - ''' + """ Simple interface to remote REST apis. The constructor expects the "base url" as parameter, that is, the url that will be common on all REST requests Remember that this is a helper for "easy of use". You can provide your owns using requests lib for example. Examples: v = REST('https://example.com/rest/v1/') (Can omit trailing / if desired) v.sendMessage('hello?param1=1¶m2=2') - This will generate a GET message to https://example.com/rest/v1/hello?param1=1¶m2=2, and return the deserialized JSON result or an exception + This will generate a GET message to https://example.com/rest/v1/hello?param1=1¶m2=2, and return the + deserialized JSON result or an exception v.sendMessage('hello?param1=1¶m2=2', {'name': 'mario' }) - This will generate a POST message to https://example.com/rest/v1/hello?param1=1¶m2=2, with json encoded body {'name': 'mario' }, and also returns + This will generate a POST message to https://example.com/rest/v1/hello?param1=1¶m2=2, with json encoded + body {'name': 'mario' }, and also returns the deserialized JSON result or raises an exception in case of error - ''' + """ + def __init__(self, url): - ''' + """ Initializes the REST helper url is the full url of the REST API Base, as for example "https://example.com/rest/v1". @param url The url of the REST API Base. The trailing '/' can be included or omitted, as desired. - ''' + """ self.endpoint = url - + if self.endpoint[-1] != '/': self.endpoint += '/' - + # Some OSs ships very old python requests lib implementations, workaround them... try: self.newerRequestLib = requests.__version__.split('.')[0] >= '1' @@ -105,37 +110,39 @@ class REST(object): pass def _getUrl(self, method): - ''' + """ Internal method Composes the URL based on "method" @param method: Method to append to base url for composition - ''' + """ url = self.endpoint + method return url def _request(self, url, data=None): - ''' + """ Launches the request @param url: The url to obtain - @param data: if None, the request will be sent as a GET request. If != None, the request will be sent as a POST, with data serialized as JSON in the body. - ''' + @param data: if None, the request will be sent as a GET request. If != None, the request will be sent as a POST, + with data serialized as JSON in the body. + """ try: if data is None: logger.debug('Requesting using GET (no data provided) {}'.format(url)) - # Old requests version does not support verify, but they do not checks ssl certificate by default + # Old requests version does not support verify, but it do not checks ssl certificate by default if self.newerRequestLib: - r = requests.get(url, verify=VERIFY_CERT) + r = requests.get(url, verify=VERIFY_CERT, timeout=TIMEOUT) else: - r = requests.get(url) - else: # POST + r = requests.get(url) + else: # POST logger.debug('Requesting using POST {}, data: {}'.format(url, data)) if self.newerRequestLib: - r = requests.post(url, data=data, headers={'content-type': 'application/json'}, verify=VERIFY_CERT) + r = requests.post(url, data=data, headers={'content-type': 'application/json'}, + verify=VERIFY_CERT, timeout=TIMEOUT) else: r = requests.post(url, data=data, headers={'content-type': 'application/json'}) - r = json.loads(r.content) # Using instead of r.json() to make compatible with oooold rquests lib versions + r = json.loads(r.content) # Using instead of r.json() to make compatible with old requests lib versions except requests.exceptions.RequestException as e: raise ConnectionError(e) except Exception as e: @@ -144,17 +151,17 @@ class REST(object): return r def sendMessage(self, msg, data=None, processData=True): - ''' + """ Sends a message to remote REST server @param data: if None or omitted, message will be a GET, else it will send a POST @param processData: if True, data will be serialized to json before sending, else, data will be sent as "raw" - ''' + """ logger.debug('Invoking post message {} with data {}'.format(msg, data)) if processData and data is not None: data = json.dumps(data) - + url = self._getUrl(msg) logger.debug('Requesting {}'.format(url)) - + return self._request(url, data) |