Skip to content

Commit 53e5ff3

Browse files
committed
support http style hostnames
1 parent 3144e39 commit 53e5ff3

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Unreleased
66

77
**Changed**
88
- Added `replace` parameter to `sasctl.tasks.publish_model`
9+
- `Session` hostname's can now be specified in HTTP format: 'http://example.com'.
910

1011
**Bugfixes**
1112
- Renamed `microanalytic_store` service to `microanalytic_score`

src/sasctl/core.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from uuid import UUID, uuid4
1717

1818
import requests, requests.exceptions
19-
from requests.adapters import HTTPAdapter, DEFAULT_POOLBLOCK
19+
from requests.adapters import HTTPAdapter
2020
from six.moves.urllib.parse import urlsplit, urlunsplit
2121
from six.moves.urllib.error import HTTPError
2222

@@ -263,7 +263,8 @@ def __init__(self, hostname,
263263
swat.cas.rest.connection.REST_CASConnection):
264264
import base64
265265

266-
# Use the httpAddress action to retieve information about REST endpoints
266+
# Use the httpAddress action to retieve information about
267+
# REST endpoints
267268
httpAddress = hostname.get_action('builtins.httpAddress')
268269
address = httpAddress()
269270
domain = address.virtualHost
@@ -273,18 +274,21 @@ def __init__(self, hostname,
273274
domain = hostname._sw_connection._current_hostname
274275
protocol = address.protocol
275276
port = address.port
276-
277-
# protocol = hostname._protocol
278277
auth = hostname._sw_connection._auth.decode('utf-8').replace(
279278
'Basic ', '')
280-
username, password = base64.b64decode(auth).decode('utf-8').split(
281-
':')
282-
# domain = hostname._hostname
279+
username, password = base64.b64decode(auth).decode(
280+
'utf-8').split(':')
283281
else:
284-
raise ValueError(
285-
"A 'swat.CAS' session can only be reused when it's connected via the REST APIs.")
282+
raise ValueError("A 'swat.CAS' session can only be reused "
283+
"when it's connected via the REST APIs.")
286284
else:
287-
domain = str(hostname)
285+
url = urlsplit(hostname)
286+
287+
# Extract http/https from domain name if present and protocl not
288+
# explicitly given
289+
protocol = protocol or url.scheme
290+
291+
domain = url.hostname or str(hostname)
288292

289293
self._settings = {'protocol': protocol or 'https',
290294
'domain': domain,

tests/unit/test_session.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@
1414
from sasctl import Session, current_session
1515

1616

17+
def test_session_from_url():
18+
"""Ensure domains in the format http(s)://hostname.com are handled."""
19+
20+
# Initial session should automatically become the default
21+
with mock.patch('sasctl.core.Session.get_token'):
22+
s = Session('http://example.com', 'user', 'password')
23+
assert s.hostname == 'example.com'
24+
assert s._settings['protocol'] == 'http'
25+
26+
with mock.patch('sasctl.core.Session.get_token'):
27+
s = Session('http://example.com', 'user', 'password', protocol='https')
28+
assert s.hostname == 'example.com'
29+
assert s._settings['protocol'] == 'https'
30+
31+
with mock.patch('sasctl.core.Session.get_token'):
32+
s = Session('https://example.com', 'user', 'password', protocol='http')
33+
assert s.hostname == 'example.com'
34+
assert s._settings['protocol'] == 'http'
35+
1736
def test_new_session(missing_packages):
1837
HOST = 'example.com'
1938
USERNAME = 'user'

0 commit comments

Comments
 (0)