Skip to content

feat(http_session): add opt. requests timeout parameter #128

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: main
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
6 changes: 4 additions & 2 deletions laceworksdk/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def __init__(self,
api_secret=None,
instance=None,
base_domain=None,
profile=None):
profile=None,
http_client_timeout=None):
"""
Initializes the Lacework Client object.

Expand Down Expand Up @@ -131,7 +132,8 @@ def __init__(self,
self._subaccount,
self._api_key,
self._api_secret,
self._base_domain
self._base_domain,
http_client_timeout
)

# API Wrappers
Expand Down
9 changes: 6 additions & 3 deletions laceworksdk/http_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from laceworksdk.exceptions import ApiError, MalformedResponse, RateLimitError

logger = logging.getLogger(__name__)
HTTP_REQUEST_DEFAULT_TIMEOUT_SEC = 30
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you feel like this is too short, feel free to ask me to change it.



class HttpSession:
Expand All @@ -30,7 +31,7 @@ class HttpSession:
_access_token = None
_access_token_expiry = None

def __init__(self, account, subaccount, api_key, api_secret, base_domain):
def __init__(self, account, subaccount, api_key, api_secret, base_domain, http_timeout=None):
"""
Initializes the HttpSession object.

Expand All @@ -39,6 +40,7 @@ def __init__(self, account, subaccount, api_key, api_secret, base_domain):
:param api_key: a Lacework API Key
:param api_secret: a Lacework API Secret
:param base_domain: a Lacework Domain (defaults to "lacework.net")
:param timeout: a client timeout for every HTTP request

:return HttpSession object.
"""
Expand All @@ -52,6 +54,7 @@ def __init__(self, account, subaccount, api_key, api_secret, base_domain):
self._api_key = api_key
self._api_secret = api_secret
self._base_domain = base_domain
self._timeout = http_timeout or HTTP_REQUEST_DEFAULT_TIMEOUT_SEC

self._base_url = f"https://{account}.{self._base_domain}"
self._account = account
Expand Down Expand Up @@ -164,7 +167,7 @@ def _get_access_token(self):
response = None

try:
response = self._session.post(uri, json=data, headers=headers)
response = self._session.post(uri, json=data, headers=headers, timeout=self._timeout)

# Validate the response
self._check_response_code(response, DEFAULT_SUCCESS_RESPONSE_CODES)
Expand Down Expand Up @@ -249,7 +252,7 @@ def _request(self, method, uri, **kwargs):
kwargs.pop("data")

# Make the HTTP request to the API endpoint
response = self._session.request(method, uri, headers=headers, **kwargs)
response = self._session.request(method, uri, headers=headers, timeout=self._timeout, **kwargs)

# Validate the response
self._check_response_code(response, DEFAULT_SUCCESS_RESPONSE_CODES)
Expand Down