From 4afcc877265618a812a49d4630ce0e1f8a0366ad Mon Sep 17 00:00:00 2001 From: Bradley Bell Date: Fri, 15 Dec 2017 15:17:24 -0800 Subject: [PATCH 1/5] fix incorrect wrap --- uw_r25/reservations.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/uw_r25/reservations.py b/uw_r25/reservations.py index 5675897..b7772e8 100644 --- a/uw_r25/reservations.py +++ b/uw_r25/reservations.py @@ -68,9 +68,8 @@ def reservations_from_xml(tree): namespaces=nsmap)[0].text try: anode = cnode.xpath("r25:address", namespaces=nsmap)[0] - reservation.contact_email = anode.xpath("r25:email", - namespaces=nsmap) - [0].text + reservation.contact_email = anode.xpath( + "r25:email", namespaces=nsmap)[0].text except IndexError: reservation.contact_email = None From 072e72156b813e403a98bf22be8c04b79c8abcb7 Mon Sep 17 00:00:00 2001 From: Bradley Bell Date: Fri, 15 Dec 2017 15:06:10 -0800 Subject: [PATCH 2/5] Add basic auth header --- uw_r25/dao.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/uw_r25/dao.py b/uw_r25/dao.py index 50689d2..50622b8 100644 --- a/uw_r25/dao.py +++ b/uw_r25/dao.py @@ -11,3 +11,8 @@ def service_name(self): def service_mock_paths(self): return [abspath(os.path.join(dirname(__file__), "resources"))] + + def _custom_headers(self, method, url, headers, body): + basic_auth = self.get_service_setting('BASIC_AUTH') + if basic_auth is not None: + return {"Authorization": "Basic %s" % basic_auth} From 5b9578bd1a61888ba00aaf7e2292694a7a8394a2 Mon Sep 17 00:00:00 2001 From: Bradley Bell Date: Fri, 15 Dec 2017 15:07:29 -0800 Subject: [PATCH 3/5] 25Live has a different base url scheme --- uw_r25/__init__.py | 7 +++++++ uw_r25/events.py | 6 +++--- uw_r25/reservations.py | 4 ++-- uw_r25/spaces.py | 4 ++-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/uw_r25/__init__.py b/uw_r25/__init__.py index 6f98712..ba040d6 100644 --- a/uw_r25/__init__.py +++ b/uw_r25/__init__.py @@ -12,6 +12,13 @@ def get_resource(url): Issue a GET request to R25 with the given url and return a response as an etree element. """ + + instance = R25_DAO().get_service_setting('INSTANCE') + if instance is not None: + url = "/r25ws/wrd/%s/run/%s" % (instance, url) + else: + url = "/r25ws/servlet/wrd/run/%s" % url + response = R25_DAO().getURL(url, {"Accept": "text/xml"}) if response.status != 200: raise DataFailureException(url, response.status, response.data) diff --git a/uw_r25/events.py b/uw_r25/events.py index 38434ff..bdb4d3b 100644 --- a/uw_r25/events.py +++ b/uw_r25/events.py @@ -8,12 +8,12 @@ def get_event_by_id(event_id): - url = "/r25ws/servlet/wrd/run/event.xml?event_id=%s" % event_id + url = "event.xml?event_id=%s" % event_id return events_from_xml(get_resource(url))[0] def get_event_by_alien_id(alien_id): - url = "/r25ws/servlet/wrd/run/event.xml?alien_uid=%s" % quote(alien_id) + url = "event.xml?alien_uid=%s" % quote(alien_id) event = events_from_xml(get_resource(url)) return event[0] if event else None @@ -24,7 +24,7 @@ def get_events(**kwargs): Supported kwargs are listed at http://knowledge25.collegenet.com/display/WSW/events.xml """ - url = "/r25ws/servlet/wrd/run/events.xml" + url = "events.xml" if len(kwargs): url += "?%s" % urlencode(kwargs) diff --git a/uw_r25/reservations.py b/uw_r25/reservations.py index b7772e8..b3516f4 100644 --- a/uw_r25/reservations.py +++ b/uw_r25/reservations.py @@ -8,7 +8,7 @@ def get_reservation_by_id(reservation_id): - url = "/r25ws/servlet/wrd/run/reservation.xml?rsrv_id=%s" % reservation_id + url = "reservation.xml?rsrv_id=%s" % reservation_id return reservations_from_xml(get_resource(url))[0] @@ -19,7 +19,7 @@ def get_reservations(**kwargs): http://knowledge25.collegenet.com/display/WSW/reservations.xml """ kwargs["scope"] = "extended" - url = "/r25ws/servlet/wrd/run/reservations.xml" + url = "reservations.xml" if len(kwargs): url += "?%s" % urlencode(kwargs) diff --git a/uw_r25/spaces.py b/uw_r25/spaces.py index aa2233d..98af2e3 100644 --- a/uw_r25/spaces.py +++ b/uw_r25/spaces.py @@ -7,7 +7,7 @@ def get_space_by_id(space_id): - url = "/r25ws/servlet/wrd/run/space.xml?space_id=%s" % space_id + url = "space.xml?space_id=%s" % space_id return spaces_from_xml(get_resource(url))[0] @@ -17,7 +17,7 @@ def get_spaces(**kwargs): Supported kwargs are listed at http://knowledge25.collegenet.com/display/WSW/spaces.xml """ - url = "/r25ws/servlet/wrd/run/spaces.xml" + url = "spaces.xml" if len(kwargs): url += "?%s" % urlencode(kwargs) From bc101f48dedbc20b938a4407750faeb525d64395 Mon Sep 17 00:00:00 2001 From: Bradley Bell Date: Fri, 15 Dec 2017 15:44:28 -0800 Subject: [PATCH 4/5] test for basic auth --- uw_r25/tests/test_dao.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 uw_r25/tests/test_dao.py diff --git a/uw_r25/tests/test_dao.py b/uw_r25/tests/test_dao.py new file mode 100644 index 0000000..03527c2 --- /dev/null +++ b/uw_r25/tests/test_dao.py @@ -0,0 +1,14 @@ +from unittest import TestCase +from uw_r25.dao import R25_DAO +from commonconf import override_settings + + +class R25TestDao(TestCase): + + def test_custom_headers(self): + self.assertFalse(R25_DAO()._custom_headers('GET', '/', {}, None)) + with override_settings(RESTCLIENTS_R25_BASIC_AUTH='b64here'): + self.assertEquals( + R25_DAO()._custom_headers('GET', '/', {}, None), + {'Authorization': 'Basic b64here'} + ) From f111d52ebd618eb16d1b3112fec0dc4103c61210 Mon Sep 17 00:00:00 2001 From: Jim Laney Date: Thu, 18 Jan 2018 09:30:58 -0800 Subject: [PATCH 5/5] use pycodestyle, and fix bare except warnings --- .travis.yml | 4 ++-- uw_r25/events.py | 2 +- uw_r25/reservations.py | 4 ++-- uw_r25/spaces.py | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 73f743e..88af98c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,13 +7,13 @@ python: - '3.6' before_script: - pip install -e . -- pip install pep8 +- pip install pycodestyle - pip install nose2 - pip install coverage - pip install commonconf - pip install python-coveralls script: -- pep8 uw_r25/ --exclude=uw_r25/tests +- pycodestyle uw_r25/ --exclude=uw_r25/tests - coverage run --source=uw_r25 uw_r25/test.py -v after_script: - coveralls diff --git a/uw_r25/events.py b/uw_r25/events.py index bdb4d3b..c311e0a 100644 --- a/uw_r25/events.py +++ b/uw_r25/events.py @@ -3,7 +3,7 @@ from uw_r25.reservations import reservations_from_xml try: from urllib import urlencode, quote -except: +except ImportError: from urllib.parse import urlencode, quote diff --git a/uw_r25/reservations.py b/uw_r25/reservations.py index b3516f4..8dab5d8 100644 --- a/uw_r25/reservations.py +++ b/uw_r25/reservations.py @@ -3,7 +3,7 @@ from uw_r25.spaces import space_reservation_from_xml try: from urllib import urlencode -except: +except ImportError: from urllib.parse import urlencode @@ -29,7 +29,7 @@ def get_reservations(**kwargs): def reservations_from_xml(tree): try: profile_name = tree.xpath("r25:profile_name", namespaces=nsmap)[0].text - except: + except Exception: profile_name = None reservations = [] diff --git a/uw_r25/spaces.py b/uw_r25/spaces.py index 98af2e3..ea879c4 100644 --- a/uw_r25/spaces.py +++ b/uw_r25/spaces.py @@ -2,7 +2,7 @@ from uw_r25 import nsmap, get_resource try: from urllib import urlencode -except: +except ImportError: from urllib.parse import urlencode