Skip to content

Added support for RemoteLRS.timeout #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
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
1 change: 1 addition & 0 deletions examples/example_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
lrs = RemoteLRS(
version=lrs_properties.version,
endpoint=lrs_properties.endpoint,
timeout=lrs_properties.timeout,
username=lrs_properties.username,
password=lrs_properties.password,
)
Expand Down
7 changes: 7 additions & 0 deletions test/remote_lrs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
class RemoteLRSTest(unittest.TestCase):
def setUp(self):
self.endpoint = lrs_properties.endpoint
self.timeout = lrs_properties.timeout
self.version = lrs_properties.version
self.username = lrs_properties.username
self.password = lrs_properties.password
self.lrs = RemoteLRS(
version=self.version,
endpoint=self.endpoint,
timeout=self.timeout,
username=self.username,
password=self.password,
)
Expand Down Expand Up @@ -126,6 +128,7 @@ def test_instantiation(self):
lrs = RemoteLRS()
self.assertIsInstance(lrs, RemoteLRS)
self.assertIsNone(lrs.endpoint)
self.assertIsNone(lrs.timeout)
self.assertIsNone(lrs.auth)
self.assertEqual(Version.latest, lrs.version)

Expand All @@ -142,6 +145,10 @@ def test_about_failure(self):

self.assertFalse(response.success)

def test_operation_without_timeout(self):
self.lrs.timeout = None
self.test_about()

def test_save_statement(self):
statement = Statement(
actor=self.agent,
Expand Down
1 change: 1 addition & 0 deletions test/resources/lrs_properties.py.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Contains user-specific information for testing.


endpoint = "<endpoint>"
timeout = <timeout> # an optional timeout in seconds
version = "<valid_version>" # 1.0.1 | 1.0.0 | 0.95 | 0.9
username = "<username>"
password = "<password>"
28 changes: 26 additions & 2 deletions tincan/remote_lrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import http.client
import json
import base64
import socket


from urllib.parse import urlparse, urlencode
Expand Down Expand Up @@ -45,6 +46,7 @@ class RemoteLRS(Base):
_props_req = [
'version',
'endpoint',
'timeout',
'auth',
]

Expand All @@ -57,6 +59,8 @@ def __init__(self, *args, **kwargs):

:param endpoint: lrs endpoint
:type endpoint: str | unicode
:param timeout: Timeout (in seconds) used for lrs communication
:type timeout: float
:param version: Version used for lrs communication
:type version: str | unicode
:param username: Username for lrs. Used to build the authentication string.
Expand All @@ -69,6 +73,7 @@ def __init__(self, *args, **kwargs):

self._version = Version.latest
self._endpoint = None
self._timeout = None
self._auth = None

if "username" in kwargs \
Expand Down Expand Up @@ -115,10 +120,11 @@ def _send_request(self, request):

parsed = urlparse(url)

timeout = self.timeout or socket._GLOBAL_DEFAULT_TIMEOUT
if parsed.scheme == "https":
web_req = http.client.HTTPSConnection(parsed.hostname, parsed.port)
web_req = http.client.HTTPSConnection(parsed.hostname, parsed.port, timeout=timeout)
else:
web_req = http.client.HTTPConnection(parsed.hostname, parsed.port)
web_req = http.client.HTTPConnection(parsed.hostname, parsed.port, timeout=timeout)

path = parsed.path
if parsed.query or parsed.path:
Expand Down Expand Up @@ -854,6 +860,24 @@ def endpoint(self, value):

self._endpoint = value

@property
def timeout(self):
"""The timeout to use when connecting to the Remote LRS

:setter type: float
:rtype: float
"""
return self._timeout

@timeout.setter
def timeout(self, value):
if value:
if not isinstance(value, float):
value = float(value)
if value < 0:
raise ValueError("Timeout must be positive number", value)
self._timeout = value

@property
def version(self):
"""Version being used for remote LRS communication
Expand Down