diff options
| author | Scott Bahling <sbahling@suse.de> | 2019-01-31 16:56:20 +0100 |
|---|---|---|
| committer | Scott Bahling <sbahling@suse.de> | 2019-01-31 16:56:20 +0100 |
| commit | 206f7e1a6ea9f398dafc31ab8e24fb85f045af2b (patch) | |
| tree | 98c387f56dabecfabd20d89744c44389501c91fa | |
| parent | f45e527f1960f2c81bda7cb6ba713991839cdba4 (diff) | |
| download | obsapi-206f7e1a6ea9f398dafc31ab8e24fb85f045af2b.tar.gz obsapi-206f7e1a6ea9f398dafc31ab8e24fb85f045af2b.tar.xz obsapi-206f7e1a6ea9f398dafc31ab8e24fb85f045af2b.zip | |
Validate the apiurl when setting it.
| -rw-r--r-- | obsapi/httpapi.py | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/obsapi/httpapi.py b/obsapi/httpapi.py index 6a207ec..517856d 100644 --- a/obsapi/httpapi.py +++ b/obsapi/httpapi.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # import requests +import re from logger import logger from requests.auth import HTTPBasicAuth from null import Null @@ -12,6 +13,13 @@ except Exception as e: DEFAULTAPIURL = 'https://api.opensuse.org' +url_validate = re.compile(r'^(?:http|ftp)s?://' # http:// or https:// + r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain... + r'localhost|' # localhost... + r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip + r'(?::\d+)?' # optional port + r'(?:/?|[/?]\S+)$', re.IGNORECASE).match + class ObsHttpApi(object): @@ -19,7 +27,8 @@ class ObsHttpApi(object): rootapi = '/' def __init__(self, apiurl=None): - self.apiurl = apiurl or DEFAULTAPIURL + self._apiurl = DEFAULTAPIURL + self.apiurl = apiurl self._auth = {} self._response = Null() self.retries = 3 @@ -98,6 +107,16 @@ class ObsHttpApi(object): return r @property + def apiurl(self): + return self._apiurl + + @apiurl.setter + def apiurl(self, url): + if url_validate(url) is None: + raise(Exception('Invalid URL: {}'.format(url))) + self._apiurl = url + + @property def response(self): '''Return requests response from last api query''' return self._response |
