diff --git a/pysnow/__init__.py b/pysnow/__init__.py index 4516671..5e27b65 100644 --- a/pysnow/__init__.py +++ b/pysnow/__init__.py @@ -6,4 +6,4 @@ from pysnow.exceptions import * __author__ = "Robert Wikman " -__version__ = "0.4.8" +__version__ = "0.4.9" diff --git a/pysnow/client.py b/pysnow/client.py index e787f46..3cb1dbe 100644 --- a/pysnow/client.py +++ b/pysnow/client.py @@ -17,6 +17,7 @@ def __init__(self, raise_on_empty=True, default_payload=None, request_params=None, + use_ssl=True, session=None): """Sets configuration and creates a session object used in `Request` later on @@ -34,18 +35,28 @@ def __init__(self, :param session: a requests session object """ + # We allow either host or instance, not both if (host and instance) is not None: - raise InvalidUsage("Arguments 'host' and 'instance' are mutually exclusive, you cannot use both.") + raise InvalidUsage("Got both 'instance' and 'host'. Panic.") if ((not (user and password)) and not session) or ((user or password) and session): raise InvalidUsage("You must either provide username and password or a session") + # Check if host or instance was passed if instance: self.host = "%s.service-now.com" % instance elif host: self.host = host else: - raise InvalidUsage("You must pass either 'instance' or 'host' to connect") + raise InvalidUsage("You must pass 'instance' or 'host' to create a client") + + # Check if SSL was requested + if use_ssl is True: + self.base_url = "https://%s" % self.host + elif use_ssl is False: + self.base_url = "http://%s" % self.host + else: + raise InvalidUsage("use_ssl: boolean value expected") # Connection properties self.instance = instance @@ -98,7 +109,7 @@ def _request(self, method, table, **kwargs): raise_on_empty=self.raise_on_empty, session=self.session, instance=self.instance, - host=self.host, + base_url=self.base_url, **kwargs) def query(self, table, **kwargs): diff --git a/pysnow/request.py b/pysnow/request.py index 6c78560..c68d302 100644 --- a/pysnow/request.py +++ b/pysnow/request.py @@ -16,7 +16,7 @@ class Request(object): - base = "api/now" + base_path = "api/now" def __init__(self, method, table, **kwargs): """Takes arguments used to perform a HTTP request @@ -27,7 +27,7 @@ def __init__(self, method, table, **kwargs): self.method = method self.table = table self.url_link = None # Updated when a linked request is iterated on - self.host = self.fqdn = kwargs.pop('host') # self.fqdn for backward compatibility + self.base_url = kwargs.pop('base_url') self.request_params = kwargs.pop('request_params') self.raise_on_empty = kwargs.pop('raise_on_empty') self.session = kwargs.pop('session') @@ -341,10 +341,10 @@ def _get_url(self, resource, item, sys_id=None): :return: url string """ - url_str = 'https://%(host)s/%(base)s/%(resource)s/%(item)s' % ( + url_str = '%(base_url)s/%(base_path)s/%(resource)s/%(item)s' % ( { - 'host': self.host, - 'base': self.base, + 'base_url': self.base_url, + 'base_path': self.base_path, 'resource': resource, 'item': item } diff --git a/tests/test_client.py b/tests/test_client.py index 3ccef42..ded31df 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -60,6 +60,28 @@ def test_client_invalid_request_params(self): self.assertRaises(InvalidUsage, Client, instance="test", user="foo", password="foo", request_params=True) self.assertRaises(InvalidUsage, Client, instance="test", user="foo", password="foo", request_params=2.89) + def test_client_invalid_use_ssl(self): + """ Invalid use_ssl type should fail """ + self.assertRaises(InvalidUsage, Client, instance="test", user="foo", password="foo", use_ssl="a string") + self.assertRaises(InvalidUsage, Client, instance="test", user="foo", password="foo", use_ssl=1) + + def test_client_use_ssl(self): + """ Client should construct base URL with correct scheme depending on use_ssl """ + instance = "foo" + host = "foo.bar.com" + + # Test with instance + c = Client(user="foo", password="foo", instance=instance, use_ssl=False) + self.assertEqual(c.base_url, "http://foo.service-now.com") + c = Client(user="foo", password="foo", instance=instance, use_ssl=True) + self.assertEqual(c.base_url, "https://foo.service-now.com") + + # Test with host + c = Client(user="foo", password="foo", host=host, use_ssl=False) + self.assertEqual(c.base_url, "http://foo.bar.com") + c = Client(user="foo", password="foo", host=host, use_ssl=True) + self.assertEqual(c.base_url, "https://foo.bar.com") + def test_client_valid_request_params(self): """Client `request_params` property should match what was passed as an argument""" params = {'foo': 'bar'}