Repository navigation
Increase HTTP timeout for API requests (TECH-3849) #17
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
Changes from all commits
3bf7b00
003f76d
ea775da
df192d1
9f51e96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| from sailthru.sailthru_client import SailthruClient | ||
| from sailthru import sailthru_client | ||
| from errors import SailthruApiError | ||
| # We need the SailthruClientError to be able to handle retries in api_get | ||
| from sailthru.sailthru_error import SailthruClientError | ||
|
|
||
| # for patched_sailthru_http_request | ||
| from sailthru.sailthru_response import SailthruResponse | ||
| from sailthru.sailthru_http import flatten_nested_hash | ||
| import requests | ||
| import platform | ||
| # other libraries | ||
| import datetime | ||
| import time | ||
| import re | ||
|
|
@@ -24,7 +29,40 @@ class DiveEmailTypes: | |
| Spotlight = "spotlight" | ||
|
|
||
|
|
||
| class DiveSailthruClient(SailthruClient): | ||
| # There is some skullduggery below in order to override the hardcoded 10 second timeout on HTTP requests | ||
| # per TECH-3849. First we copy/paste the sailthru_http_request() function that originally exists here: | ||
| # https://github.com/sailthru/sailthru-python-client/blob/521fdaa30890a29da8fbb02726e7d22ed174b878/sailthru/sailthru_http.py#L30 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this referenced version allowed for a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly just wrote my version before looking at theirs. I guess it's better to expose the things that anyone might want to change as parameters, but this is an internal function anyway so it's not like the end user could use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was worried that some higher yet still internal level method in sailthru client accepts |
||
| # Then we modify it to have a default timeout of 60 seconds and additionally to accept a timeout parameter | ||
| def timeout_patched_sailthru_http_request(url, data, method, file_data=None, timeout=60): | ||
| """ | ||
| Perform an HTTP GET / POST / DELETE request | ||
|
|
||
| This is a override of upstream sailthru_http_request with `timeout` added as a parameter and | ||
| with the default set to 60 instead of 10. | ||
| """ | ||
| data = flatten_nested_hash(data) | ||
| method = method.upper() | ||
| params, data = (None, data) if method == 'POST' else (data, None) | ||
|
|
||
| try: | ||
| headers = {'User-Agent': 'Sailthru API Python Client %s; Python Version: %s' % ('2.3.3-patched', platform.python_version())} | ||
| response = requests.request(method, url, params=params, data=data, files=file_data, headers=headers, timeout=timeout) | ||
| return SailthruResponse(response) | ||
| except requests.HTTPError as e: | ||
| raise SailthruClientError(str(e)) | ||
| except requests.RequestException as e: | ||
| raise SailthruClientError(str(e)) | ||
|
|
||
|
|
||
| # Now we need to patch the altered sailthru_http_request into a place where even functions defined in the upstream | ||
| # SailthruClient class that call it will call our new altered version. In the upstream class, the sailthru_http_request() | ||
| # function is `import`ed into the sailthru_client module (not the class), so we need to import the sailthru_client module | ||
| # and then redefine the sailthru_http_request that it had imported to instead point to our version. Then later we | ||
| # have to make sure we are subclassing SailthruClient by refering to it specifically as sailthru_client.SailthruClient | ||
| sailthru_client.sailthru_http_request = timeout_patched_sailthru_http_request | ||
|
|
||
|
|
||
| class DiveSailthruClient(sailthru_client.SailthruClient): # must import from sailthru_client.SailthruClient for patched HTTP timeout | ||
| """ | ||
| Our Sailthru client implementation that adds our own concepts. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| [flake8] | ||
| max-line-length = 128 | ||
| max-line-length = 140 | ||
| max-complexity = 12 | ||
| statistics = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://www.merriam-webster.com/dictionary/skulduggery
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for SAT word