Skip to content

Commit

Permalink
Merge pull request #52 from rbw0/branch0.4.9
Browse files Browse the repository at this point in the history
URL builder fixes
  • Loading branch information
rbw authored Sep 9, 2017
2 parents e64e774 + 44c0bb5 commit a988ba6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pysnow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
from pysnow.exceptions import *

__author__ = "Robert Wikman <[email protected]>"
__version__ = "0.4.8"
__version__ = "0.4.9"
17 changes: 14 additions & 3 deletions pysnow/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 5 additions & 5 deletions pysnow/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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
}
Expand Down
22 changes: 22 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand Down

0 comments on commit a988ba6

Please sign in to comment.