Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ username=MyUserName
password=MyPassword
```

If you are using an `https://` URL and want to disable certificate
verification, you can add:

```
verify=false
```


## Development

To install with dependancies for testing.
Expand Down
72 changes: 33 additions & 39 deletions channelfinder/ChannelFinderClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ class ChannelFinderClient(object):
__propertiesResource = "/resources/properties"
__tagsResource = "/resources/tags"

def __init__(self, BaseURL=None, username=None, password=None):
def __init__(self, BaseURL=None, username=None, password=None, verify=None):
"""
Channel finder client object. It provides a connection object to perform the following operations:
- find: find all channels satisfying given searching criteria
- set: add channel into service
- update: update channel information
- delete: delete channel from service

If any of the arguments is ``None``, the respective value from the
configuration file is used.

:param BaseURL: the url of the channel finder service
:param username: user name authorized by channel finder service
:param password: password for the authorized user
:param verify: verify the peer TLS certificate
"""
self.__baseURL = self.__getDefaultConfig("BaseURL", BaseURL)
self.__userName = self.__getDefaultConfig("username", username)
Expand All @@ -49,17 +53,33 @@ def __init__(self, BaseURL=None, username=None, password=None):
self.__auth = None
self.__session = requests.Session()
self.__session.mount(self.__baseURL, HTTPAdapter())
if verify is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anderslindho I think this satisfies some of your annoyances?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parts thereof, yes, but not my main concern. This works functionally, but it still leaves us with an ambiguous public API. Using a private sentinel would give us clear semantics and avoids optional booleans entirely.

But I can concede that further changes are out of scope, so if both you and @tynanford are fine with this, feel free to proceed. There is (unfortunately...) already an established pattern of using None this way in this code base.

self.__session.verify = verify
else:
verify_str = self.__getDefaultConfig("verify", None)
if verify_str is None:
self.__session.verify = True
elif verify_str.lower() in ("1", "true", "on", "yes"):
self.__session.verify = verify = True
elif verify_str.lower() in ("0", "false", "off", "no"):
self.__session.verify = verify = False
else:
# The verify option can also be a path to the trust store, so
# if it does not match any of the special strings, we assume
# that the string should be used as-is.
self.__session.verify = verify_str

def __getDefaultConfig(self, key, ref):
def __getDefaultConfig(self, key, override):
"""
Get default configuration for given name and section.

:param key: key word
:param ref: reference value
:return: result if key word is configured or ref is not None, otherwise None
:param override: override value
:return: ``override`` if not ``None``, else the configuration value
associated with ``key`` if present, otherwise ``None``.
"""
result = ref
if ref is None:
result = override
if override is None:
result = basecfg["DEFAULT"].get(key, None)
return result

Expand Down Expand Up @@ -153,7 +173,6 @@ def __handleSingleAddParameter(self, **kwds):
+ kwds["channel"]["name"],
data=JSONEncoder().encode(kwds["channel"]),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
)
r.raise_for_status()
Expand All @@ -162,7 +181,6 @@ def __handleSingleAddParameter(self, **kwds):
self.__baseURL + self.__channelsResource,
data=JSONEncoder().encode(kwds["channels"]),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
)
r.raise_for_status()
Expand All @@ -171,7 +189,6 @@ def __handleSingleAddParameter(self, **kwds):
self.__baseURL + self.__tagsResource + "/" + kwds["tag"]["name"],
data=JSONEncoder().encode(kwds["tag"]),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
)
r.raise_for_status()
Expand All @@ -181,7 +198,6 @@ def __handleSingleAddParameter(self, **kwds):
self.__baseURL + self.__tagsResource,
data=data,
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
)
r.raise_for_status()
Expand All @@ -193,7 +209,6 @@ def __handleSingleAddParameter(self, **kwds):
+ kwds["property"]["name"],
data=JSONEncoder().encode(kwds["property"]),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
)
r.raise_for_status()
Expand All @@ -204,7 +219,6 @@ def __handleSingleAddParameter(self, **kwds):
self.__baseURL + self.__propertiesResource,
data=data,
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
)
r.raise_for_status()
Expand All @@ -230,7 +244,6 @@ def __handleMultipleAddParameters(self, **kwds):
self.__baseURL + self.__tagsResource + "/" + kwds["tag"]["name"],
data=JSONEncoder().encode(data),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
).raise_for_status()
elif "tag" in kwds and "channelNames" in kwds:
Expand All @@ -243,7 +256,6 @@ def __handleMultipleAddParameters(self, **kwds):
self.__baseURL + self.__tagsResource + "/" + kwds["tag"]["name"],
data=JSONEncoder().encode(data),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
).raise_for_status()
elif "property" in kwds and "channels" in kwds:
Expand All @@ -256,7 +268,6 @@ def __handleMultipleAddParameters(self, **kwds):
+ kwds["property"]["name"],
data=JSONEncoder().encode(data),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
).raise_for_status()
else:
Expand Down Expand Up @@ -357,7 +368,6 @@ def findByArgs(self, args):
url,
params=args,
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
)
try:
Expand All @@ -377,9 +387,7 @@ def findTag(self, tagname):
:return: Tag object if found, otherwise None
"""
url = self.__baseURL + self.__tagsResource + "/" + tagname
r = self.__session.get(
url, headers=copy(self.__jsonheader), verify=False, auth=self.__auth
)
r = self.__session.get(url, headers=copy(self.__jsonheader), auth=self.__auth)
try:
r.raise_for_status()
return r.json()
Expand All @@ -397,7 +405,7 @@ def findProperty(self, propertyname):
:return: Property object if found, otherwise None
"""
url = self.__baseURL + self.__propertiesResource + "/" + propertyname
r = self.__session.get(url, headers=copy(self.__jsonheader), verify=False)
r = self.__session.get(url, headers=copy(self.__jsonheader))
try:
r.raise_for_status()
return r.json()
Expand All @@ -414,7 +422,7 @@ def getAllTags(self):
:return: list of all the Tag objects present, otherwise None.
"""
url = self.__baseURL + self.__tagsResource
r = self.__session.get(url, headers=copy(self.__jsonheader), verify=False)
r = self.__session.get(url, headers=copy(self.__jsonheader))
try:
r.raise_for_status()
return r.json()
Expand All @@ -431,7 +439,7 @@ def getAllProperties(self):
:return: list of the Property objects present, otherwise None
"""
url = self.__baseURL + self.__propertiesResource
r = self.__session.get(url, headers=copy(self.__jsonheader), verify=False)
r = self.__session.get(url, headers=copy(self.__jsonheader))
try:
r.raise_for_status()
return r.json()
Expand Down Expand Up @@ -496,12 +504,12 @@ def __handleSingleDeleteParameter(self, **kwds):
+ kwds["channelName"].strip()
)
self.__session.delete(
url, headers=copy(self.__jsonheader), verify=False, auth=self.__auth
url, headers=copy(self.__jsonheader), auth=self.__auth
).raise_for_status()
elif "tagName" in kwds:
url = self.__baseURL + self.__tagsResource + "/" + kwds["tagName"].strip()
self.__session.delete(
url, verify=False, headers=copy(self.__jsonheader), auth=self.__auth
url, headers=copy(self.__jsonheader), auth=self.__auth
).raise_for_status()
elif "propertyName" in kwds:
url = (
Expand All @@ -511,7 +519,7 @@ def __handleSingleDeleteParameter(self, **kwds):
+ kwds["propertyName"].strip()
)
self.__session.delete(
url, headers=copy(self.__jsonheader), verify=False, auth=self.__auth
url, headers=copy(self.__jsonheader), auth=self.__auth
).raise_for_status()
else:
raise RuntimeError(
Expand All @@ -537,7 +545,6 @@ def __handleMultipleDeleteParameters(self, **kwds):
+ "/"
+ kwds["channelName"].strip(),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
).raise_for_status()
elif "tag" in kwds and "channelNames" in kwds:
Expand All @@ -560,7 +567,6 @@ def __handleMultipleDeleteParameters(self, **kwds):
+ "/"
+ kwds["channelName"],
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
).raise_for_status()
elif "property" in kwds and "channelNames" in kwds:
Expand Down Expand Up @@ -666,7 +672,6 @@ def __handleSingleUpdateParameter(self, **kwds):
self.__baseURL + self.__channelsResource + "/" + ch["name"],
data=JSONEncoder().encode(ch),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
)
r.raise_for_status()
Expand All @@ -676,7 +681,6 @@ def __handleSingleUpdateParameter(self, **kwds):
self.__baseURL + self.__channelsResource,
data=JSONEncoder().encode(chs),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
)
r.raise_for_status()
Expand All @@ -686,7 +690,6 @@ def __handleSingleUpdateParameter(self, **kwds):
self.__baseURL + self.__propertiesResource + "/" + property["name"],
data=JSONEncoder().encode(property),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
)
r.raise_for_status()
Expand All @@ -696,7 +699,6 @@ def __handleSingleUpdateParameter(self, **kwds):
self.__baseURL + self.__tagsResource + "/" + tag["name"],
data=JSONEncoder().encode(tag),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
)
r.raise_for_status()
Expand All @@ -705,7 +707,6 @@ def __handleSingleUpdateParameter(self, **kwds):
self.__baseURL + self.__tagsResource,
data=JSONEncoder().encode(kwds["tags"]),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
)
r.raise_for_status()
Expand Down Expand Up @@ -742,7 +743,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
self.__baseURL + self.__tagsResource + "/" + tag["name"],
data=JSONEncoder().encode(tag),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
).raise_for_status()
elif "tag" in kwds and "channelNames" in kwds:
Expand All @@ -759,7 +759,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
self.__baseURL + self.__tagsResource + "/" + tag["name"],
data=JSONEncoder().encode(tag),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
).raise_for_status()
elif "property" in kwds and "channelName" in kwds:
Expand All @@ -778,7 +777,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
self.__baseURL + self.__propertiesResource + "/" + property["name"],
data=JSONEncoder().encode(property),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
).raise_for_status()
elif "property" in kwds and "channelNames" in kwds:
Expand All @@ -799,7 +797,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
self.__baseURL + self.__propertiesResource + "/" + property["name"],
data=JSONEncoder().encode(property),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
).raise_for_status()
elif "originalChannelName" in kwds and "channel" in kwds:
Expand All @@ -809,7 +806,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
self.__baseURL + self.__channelsResource + "/" + channelName,
data=JSONEncoder().encode(ch),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
).raise_for_status()
elif "originalPropertyName" in kwds and "property" in kwds:
Expand All @@ -819,7 +815,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
self.__baseURL + self.__propertiesResource + "/" + propName,
data=JSONEncoder().encode(prop),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
).raise_for_status()
elif "originalTagName" in kwds and "tag" in kwds:
Expand All @@ -829,7 +824,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
self.__baseURL + self.__tagsResource + "/" + tagName,
data=JSONEncoder().encode(tag),
headers=copy(self.__jsonheader),
verify=False,
auth=self.__auth,
).raise_for_status()
else:
Expand Down
Loading